#include "game.h"
void print_board(char board[][COL+2],int row,int col)
{
int i = 1;
int j = 0;
printf(" ");
for(;i<=COL;i++)
printf("%3d",i);
printf("\n");
printf(" ");
for(i=0;i
printf("---");
printf("\n");
for(i = 1;i<=ROW;i++)
{
printf("%2d|",i);
for(j = 1;j<=COL;j++)
{
printf("%2c|",board[i][j]);
}
printf("\n");
}
}
void lay_mines(char mine[][COL+2],int row,int col,int a)
{
int x;
int y;
int count = 0;
srand((unsigned long)time(NULL));
do{
x = (rand() % (ROW))+1;
y = (rand() % (COL))+1;
if(mine[x][y] == '0')
{
mine[x][y] = '1';
count++;
}
}while(count < (a*MINE_NUM));
}
int get_mine_num(char mine[][COL+2],int row,int col,int x,int y)
{
int num = 0;
num = (mine[x-1][y-1] - '0') + (mine[x-1][y] - '0') + (mine[x-1][y+1] - '0') + (mine[x][y-1] - '0') + (mine[x][y+1] - '0') + (mine[x+1][y-1] - '0') + (mine[x+1][y] - '0') + (mine[x+1][y+1] - '0');
return num;
}
void game()
{
int i = 1;
int x,y;
int win = 0;
do
{
char mine[ROW+2][COL+2];
char show[ROW+2][COL+2];
memset(mine,'0',(ROW+2)*(COL+2));
memset(show,'*',(ROW+2)*(COL+2));
printf("第%d关\n",i);
lay_mines(mine,ROW+2,COL+2,i);
do{
system("cls");
print_board(show,ROW+2,COL+2);
printf("Please input your coordinate:");
scanf("%d%d",&x,&y);
if(x>=1&&x<=10&&y>=1&&y<=10)
{
if(mine[x][y] == '1')
{
printf("It's too bad!Game over!\n");
print_board(mine,ROW+2,COL+2);
goto END;
}
else
{
int count = get_mine_num(mine,ROW+2,COL+2,x,y);
show[x][y] = count + '0';
win++;
if(ROW*COL-i*MINE_NUM == win)
{
printf("Congratulations!You win!\n");
i++;
break;
}
}
}
}while(1);
}while(i<=4);
END:
printf("Do you want to play again?\n");
}