Ghua975 2023-01-04 13:31 采纳率: 83.3%
浏览 23
已结题

将定义常量改为可以输入的变量

怎样才能给这个代码在删除最后一行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);
    }

}

  • 写回答

2条回答 默认 最新

  • 快乐鹦鹉 2023-01-04 13:38
    关注

    改成全局变量,然后main中scanf输入这个变量值就好了。
    #define EASY_COUNT 10 改为
    int EASY_COUNT = 0;
    main中增加
    scanf("%d",&EASY_COUNT);

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论 编辑记录
查看更多回答(1条)

报告相同问题?

问题事件

  • 系统已结题 1月12日
  • 已采纳回答 1月4日
  • 创建了问题 1月4日

悬赏问题

  • ¥20 机器学习能否像多层线性模型一样处理嵌套数据
  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效