怎样才能给这个代码在删除最后一行EASY_COUNT的定义后,将排雷的数目改变为由输入的值决定的呢?
#define ROW 9
#define COL 9
#define ROWS ROW+2
#define COLS COL+2
#define EASY_COUNT 10
// Initialize
void init_board(char arr[ROWS][COLS], int rows, int cols, char set);
// Print
void show_board(char arr[ROWS][COLS], int row, int col);
// Lay out the thunder
void set_mine(char mine[ROWS][COLS],int row,int col);
// Concatenated expansion
void search_mine(char mine[ROWS][COLS], char show[ROWS][COLS], int x, int y);
// Troubleshoot mines
void find_mine(char mine[ROWS][COLS], char show[ROWS][COLS], int row, int col);
// Get the number of surrounding mines
int get_mine_count(char mine[ROWS][COLS], int x, int y);
//Implementation of mine clearance
void game()
{
//The mine array is used to hold information about the mines that have been deployed
char mine[ROWS][COLS] = { 0 };//'0'
//The show array is used to store information about the mines that were detected
char show[ROWS][COLS] = { 0 };//'*'
//Initialize the chessboard
init_board(mine, ROWS, COLS,'0');
init_board(show, ROWS, COLS,'*');
//Print chessboard
show_board(mine,ROW,COL);
//Lay out the mine
set_mine(mine, ROW,COL);
show_board(show, ROW, COL);
//troubleshoot the mine
find_mine(mine, show, ROW, COL);
}
int main()
{
game();
}
void init_board(char arr[ROWS][COLS], int rows, int cols,char set)
{
int i = 0;
int j = 0;
for (i = 0; i < rows; i++)
{
for (j = 0; j < cols; j++)
{
arr[i][j] = set;
}
}
}
void show_board(char arr[ROWS][COLS], int row, int col)
{
int i = 0;
int j = 0;
printf(" Come on \n");
for (i = 0; i <= col; i++)
{
printf("%d ", i);
}
printf("\n");
for (i = 1; i <= row; i++)
{
printf("%d ", i);
for (j = 1; j <= col; j++)
{
printf("%c ", arr[i][j]);
}
printf("\n");
}
printf("------------------\n");
}
//Lay out the mine
void set_mine(char mine[ROWS][COLS], int row, int col)
{
int count = EASY_COUNT;
int x = 0;
int y = 0;
while (count)
{
x = rand() % row + 1;
y = rand() % col + 1;
if (mine[x][y] == '0')
{
mine[x][y] = '1';//Lay out the mine
count--;
}
}
}
int get_mine_count(char mine[ROWS][COLS],int x,int y)
{
int n = 0;
for (int i = -1; i <= 1; i++)
{
for (int j = -1; j <= 1; j++)
{
n += mine[x + i][y + j] - '0';
}
}
return n;
}
void search_mine(char mine[ROWS][COLS], char show[ROWS][COLS], int x, int y)
{
if (show[x][y] != '*' )
{
return ;
}
int count = get_mine_count(mine, x, y);
if (count == 0)
{
show[x][y] = ' ';
}
else {
show[x][y] = count + '0';
return;
}
for (int i = -1; i <= 1; i++)
{
for (int j = -1; j <= 1; j++)
{
int a = x + i;
int b = y + j;
if (mine[a][b] != '1')
{
search_mine(mine, show, a, b);
}
}
}
}
//troubleshoot the mine
void find_mine(char mine[ROWS][COLS], char show[ROWS][COLS], int row, int col)
{
int x = 0;
int y = 0;
int win = 0;
while (win<row*col-EASY_COUNT)
{
printf("Please enter the coordinates you want to troubleshooting:>");
scanf("%d %d", &x, &y);
if (x >= 1 && x <= row && y >= 1 && y <= col)
{
if (mine[x][y] == '1')
{
printf("Oops! Got blown up!\n");
show_board(mine, ROW, COL);
break;
}
else {
search_mine(mine, show, x, y);
show_board(show, ROW, COL);
win++;
}
}
else {
printf("Invalid coordinates, retype\n");
break;
}
}
if (win == row * col - EASY_COUNT)
{
printf("Congratulations, you made it!\n");
show_board(mine, ROW, COL);
}
}