|
 |
a35f64 |
|
|
 |
a35f64 |
#include <time.h>
|
|
 |
a35f64 |
#include <stdio.h>
|
|
 |
a35f64 |
#include <stdlib.h>
|
|
 |
a35f64 |
|
|
 |
a35f64 |
int map[1024][1024];
|
|
 |
a35f64 |
char screen[1024][1024];
|
|
 |
a35f64 |
int end = 0;
|
|
 |
a35f64 |
int botState = 0;
|
|
 |
a35f64 |
int botx = 0;
|
|
 |
a35f64 |
int boty = 0;
|
|
 |
a35f64 |
int botMines = 0;
|
|
 |
a35f64 |
int bolocks = 0;
|
|
 |
a35f64 |
char botCom[1];
|
|
 |
a35f64 |
int width = 8;
|
|
 |
a35f64 |
int height = 8;
|
|
 |
a35f64 |
int mines = 10;
|
|
 |
a35f64 |
|
|
 |
a35f64 |
|
|
 |
a35f64 |
void generatemap() {
|
|
 |
a35f64 |
end = 0;
|
|
 |
a35f64 |
for (int a = 0; a < width; a++) {
|
|
 |
a35f64 |
for (int c = 0; c < height; c++) {
|
|
 |
a35f64 |
screen[a][c]='-';
|
|
 |
a35f64 |
}
|
|
 |
a35f64 |
}
|
|
 |
a35f64 |
for (int b = 0; b < mines; b++) {
|
|
 |
a35f64 |
int x = rand()%width;
|
|
 |
a35f64 |
int y = rand()%height;
|
|
 |
a35f64 |
if (map[x][y] != 1){
|
|
 |
a35f64 |
map[x][y] = 1;
|
|
 |
a35f64 |
}else{
|
|
 |
a35f64 |
b--;
|
|
 |
a35f64 |
}
|
|
 |
a35f64 |
}
|
|
 |
a35f64 |
}
|
|
 |
a35f64 |
|
|
 |
a35f64 |
|
|
 |
a35f64 |
void printmap() {
|
|
 |
da60f5 |
printf("\e[1;1H\e[2J");
|
|
 |
a35f64 |
for (int d = 0; d < height; d++){
|
|
 |
a35f64 |
for (int e = 0; e < width; e++){
|
|
 |
da60f5 |
if(screen[e][d] == '-'){
|
|
 |
da60f5 |
printf("\x1b[90;47m\033[%d;%dH%c",e,d,screen[e][d]);
|
|
 |
da60f5 |
}
|
|
 |
da60f5 |
if(screen[e][d] == '0'){
|
|
 |
da60f5 |
printf("\x1b[90;47m\033[%d;%dH%c",e,d,screen[e][d]);
|
|
 |
da60f5 |
}
|
|
 |
da60f5 |
if(screen[e][d] == '1'){
|
|
 |
da60f5 |
printf("\x1b[94;47m\033[%d;%dH%c",e,d,screen[e][d]);
|
|
 |
da60f5 |
}
|
|
 |
da60f5 |
if(screen[e][d] == '2'){
|
|
 |
da60f5 |
printf("\x1b[32;47m\033[%d;%dH%c",e,d,screen[e][d]);
|
|
 |
da60f5 |
}
|
|
 |
da60f5 |
if(screen[e][d] == '3'){
|
|
 |
da60f5 |
printf("\x1b[91;47m\033[%d;%dH%c",e,d,screen[e][d]);
|
|
 |
da60f5 |
}
|
|
 |
da60f5 |
if(screen[e][d] == '4'){
|
|
 |
da60f5 |
printf("\x1b[34;47m\033[%d;%dH%c",e,d,screen[e][d]);
|
|
 |
da60f5 |
}
|
|
 |
da60f5 |
if(screen[e][d] == '5'){
|
|
 |
da60f5 |
printf("\x1b[31;47m\033[%d;%dH%c",e,d,screen[e][d]);
|
|
 |
da60f5 |
}
|
|
 |
da60f5 |
if(screen[e][d] == '6'){
|
|
 |
da60f5 |
printf("\x1b[36;47m\033[%d;%dH%c",e,d,screen[e][d]);
|
|
 |
da60f5 |
}
|
|
 |
da60f5 |
if(screen[e][d] == '7'){
|
|
 |
da60f5 |
printf("\x1b[30;47m\033[%d;%dH%c",e,d,screen[e][d]);
|
|
 |
da60f5 |
}
|
|
 |
da60f5 |
if(screen[e][d] == '8'){
|
|
 |
da60f5 |
printf("\x1b[90;47m\033[%d;%dH%c",e,d,screen[e][d]);
|
|
 |
da60f5 |
}
|
|
 |
da60f5 |
if(screen[e][d] == '^'){
|
|
 |
da60f5 |
printf("\x1b[91;47m\033[%d;%dH%c",e,d,screen[e][d]);
|
|
 |
da60f5 |
}
|
|
 |
da60f5 |
if(screen[e][d] == '*'){
|
|
 |
da60f5 |
printf("\x1b[30;47m\033[%d;%dH%c",e,d,screen[e][d]);
|
|
 |
da60f5 |
}
|
|
 |
a35f64 |
}
|
|
 |
da60f5 |
//printf("\n");
|
|
 |
a35f64 |
}
|
|
 |
da60f5 |
printf("\x1b[0m\n");
|
|
 |
a35f64 |
}
|
|
 |
a35f64 |
|
|
 |
a35f64 |
|
|
 |
a35f64 |
|
|
 |
a35f64 |
void gameover() {
|
|
 |
a35f64 |
for (int a = 0; a < width; a++) {
|
|
 |
a35f64 |
for (int c = 0; c < height; c++) {
|
|
 |
a35f64 |
if (map[a][c] == 1) {
|
|
 |
a35f64 |
screen[a][c]='*';
|
|
 |
a35f64 |
}
|
|
 |
a35f64 |
}
|
|
 |
a35f64 |
}
|
|
 |
a35f64 |
printmap();
|
|
 |
da60f5 |
printf("GAME OVER!!!\n");
|
|
 |
a35f64 |
end=1;
|
|
 |
a35f64 |
}
|
|
 |
a35f64 |
|
|
 |
a35f64 |
|
|
 |
a35f64 |
void opencell(int x,int y){
|
|
 |
a35f64 |
if (screen[x][y] != '-') return;
|
|
 |
a35f64 |
if (map[x][y] != 1) {
|
|
 |
a35f64 |
int miness = 0;
|
|
 |
a35f64 |
for (int f = -1; f <= 1; f++){
|
|
 |
a35f64 |
for (int g = -1; g <= 1; g++){
|
|
 |
a35f64 |
if (x+f < 0 || x+f >= width || y+g < 0 || y+g >= height) continue;
|
|
 |
a35f64 |
if (map[x+f][y+g])miness++;
|
|
 |
a35f64 |
}
|
|
 |
a35f64 |
}
|
|
 |
a35f64 |
screen[x][y] = '0' + miness;
|
|
 |
a35f64 |
if (miness == 0) {
|
|
 |
a35f64 |
for (int f = -1; f <= 1; f++){
|
|
 |
a35f64 |
for (int g = -1; g <= 1; g++){
|
|
 |
a35f64 |
if (x+f < 0 || x+f >= width || y+g < 0 || y+g >= height) continue;
|
|
 |
a35f64 |
opencell(x+f, y+g);
|
|
 |
a35f64 |
}
|
|
 |
a35f64 |
}
|
|
 |
a35f64 |
}
|
|
 |
a35f64 |
}else{
|
|
 |
a35f64 |
gameover();
|
|
 |
a35f64 |
}
|
|
 |
a35f64 |
}
|
|
 |
a35f64 |
|
|
 |
a35f64 |
void bot(){
|
|
 |
a35f64 |
botx = 0;
|
|
 |
a35f64 |
boty = 0;
|
|
 |
a35f64 |
botMines = 0;
|
|
 |
a35f64 |
bolocks = 0;
|
|
 |
a35f64 |
botCom[1] = 0;
|
|
 |
a35f64 |
for(int x = 0; x < width; x++){
|
|
 |
a35f64 |
for(int y = 0; y < height; y++){
|
|
 |
a35f64 |
int j = screen[x][y] - '0';
|
|
 |
a35f64 |
if (j < 0 || j > 8) continue;
|
|
 |
a35f64 |
|
|
 |
a35f64 |
int marks = 0;
|
|
 |
a35f64 |
int unknowns = 0;
|
|
 |
a35f64 |
for(int xx = x-1; xx <= x+1; xx++){
|
|
 |
a35f64 |
for (int yy = y-1; yy <= y+1; yy++){
|
|
 |
a35f64 |
if (xx < 0 || xx >= width || yy < 0 || yy >= height) continue;
|
|
 |
a35f64 |
if (screen[xx][yy] == '-') {
|
|
 |
a35f64 |
++unknowns;
|
|
 |
a35f64 |
botx = xx;
|
|
 |
a35f64 |
boty = yy;
|
|
 |
a35f64 |
}
|
|
 |
a35f64 |
if (screen[xx][yy] == '^') ++marks;
|
|
 |
a35f64 |
}
|
|
 |
a35f64 |
}
|
|
 |
a35f64 |
if (unknowns == 0) continue;
|
|
 |
a35f64 |
|
|
 |
da60f5 |
|
|
 |
da60f5 |
if (unknowns+marks == j){
|
|
 |
a35f64 |
botCom[0] = 'm';
|
|
 |
a35f64 |
return;
|
|
 |
a35f64 |
}
|
|
 |
a35f64 |
|
|
 |
a35f64 |
if (marks == j) {
|
|
 |
a35f64 |
botCom[0] = 'o';
|
|
 |
a35f64 |
return;
|
|
 |
a35f64 |
}
|
|
 |
a35f64 |
}
|
|
 |
a35f64 |
}
|
|
 |
a35f64 |
|
|
 |
da60f5 |
|
|
 |
a35f64 |
printf("random!\n");
|
|
 |
a35f64 |
botCom[0] = 'o';
|
|
 |
a35f64 |
while(1) {
|
|
 |
a35f64 |
botx = rand()%width;
|
|
 |
a35f64 |
boty = rand()%height;
|
|
 |
a35f64 |
if (screen[botx][boty] == '-') break;
|
|
 |
a35f64 |
}
|
|
 |
a35f64 |
}
|
|
 |
a35f64 |
|
|
 |
a35f64 |
int main(){
|
|
 |
a35f64 |
srand(time(NULL));
|
|
 |
a35f64 |
printf("Width, Height, Quantity of mines: ");
|
|
 |
a35f64 |
scanf("%d %d %d",&width, &height, &mines);
|
|
 |
a35f64 |
if (width > 1024){
|
|
 |
a35f64 |
width = 1024;
|
|
 |
da60f5 |
printf("Error Max Size is 1024\n");
|
|
 |
a35f64 |
}
|
|
 |
a35f64 |
if (height > 1024){
|
|
 |
a35f64 |
height = 1024;
|
|
 |
da60f5 |
printf("Error Max Size is 1024\n");
|
|
 |
a35f64 |
}
|
|
 |
a35f64 |
if (mines > width*height){
|
|
 |
a35f64 |
mines = width*height;
|
|
 |
da60f5 |
printf("Quantity of mines more than quantity of cell's\n");
|
|
 |
a35f64 |
}
|
|
 |
a35f64 |
generatemap();
|
|
 |
a35f64 |
printf("On Bot(0/1)?: ");
|
|
 |
a35f64 |
scanf("%d",&botState);
|
|
 |
a35f64 |
end = 0;
|
|
 |
a35f64 |
while(end == 0){
|
|
 |
a35f64 |
printmap();
|
|
 |
a35f64 |
|
|
 |
a35f64 |
char cmd[100] = {};
|
|
 |
a35f64 |
int x = 0, y = 0;
|
|
 |
a35f64 |
|
|
 |
a35f64 |
if (botState == 0){
|
|
 |
a35f64 |
printf("enter command (<o|m> <x> <y>): ");
|
|
 |
a35f64 |
scanf("%s %d %d", cmd, &y, &x);
|
|
 |
a35f64 |
}else{
|
|
 |
a35f64 |
bot();
|
|
 |
a35f64 |
cmd[0] = botCom[0];
|
|
 |
a35f64 |
y = boty;
|
|
 |
a35f64 |
x = botx;
|
|
 |
a35f64 |
printf("%s %d %d\n", cmd, x, y);
|
|
 |
a35f64 |
}
|
|
 |
a35f64 |
|
|
 |
a35f64 |
if (x < 0 || x >= width || y < 0 || y >= height) {
|
|
 |
a35f64 |
printf("bad coordinates\n");
|
|
 |
a35f64 |
} else
|
|
 |
a35f64 |
if (cmd[0] == 'm'){
|
|
 |
a35f64 |
if (screen[x][y] == '^') break;
|
|
 |
a35f64 |
screen[x][y] = '^';
|
|
 |
a35f64 |
} else
|
|
 |
a35f64 |
if (cmd[0] == 'o'){
|
|
 |
a35f64 |
if (screen[x][y] != '-') break;
|
|
 |
a35f64 |
opencell(x,y);
|
|
 |
a35f64 |
} else {
|
|
 |
a35f64 |
printf("unknown command\n");
|
|
 |
a35f64 |
break;
|
|
 |
a35f64 |
}
|
|
 |
a35f64 |
|
|
 |
a35f64 |
if (end == 0) {
|
|
 |
a35f64 |
end = 1;
|
|
 |
a35f64 |
for(int x = 0; x < 8; ++x) {
|
|
 |
a35f64 |
for(int y = 0; y < 8; ++y) {
|
|
 |
a35f64 |
if (screen[x][y] == '-') end = 0;
|
|
 |
a35f64 |
if (map[x][y] && screen[x][y] != '^') end = 0;
|
|
 |
a35f64 |
if (!map[x][y] && screen[x][y] == '^') end = 0;
|
|
 |
a35f64 |
}
|
|
 |
a35f64 |
}
|
|
 |
a35f64 |
if (end) {
|
|
 |
a35f64 |
printmap();
|
|
 |
a35f64 |
printf("WIN!\n");
|
|
 |
a35f64 |
}
|
|
 |
a35f64 |
}
|
|
 |
a35f64 |
}
|
|
 |
a35f64 |
}
|