如何仅使用一个随用户输入而变化的命令将多个值扫描到二维数组中

问题描述 投票:0回答:1

我正在尝试创建一个扫雷游戏,用户可以选择要输入的地雷数量,并在将其插入数组时以点x列的方式列出其坐标,程序应如下所示。但是我不确定如何将多个坐标扫描到数组中。我到目前为止所获得的代码可在下面找到。

Welcome to minesweeper!
How many mines? 3
Enter pairs:
0 0
1 1
Game Started
2 1 1 1 1 1 1 1
1 2 1 1 1 1 1 1
1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1
int number_mines;
int x;
int y;

printf("Welcome to minesweeper!\n");
printf("How many mines? ");
scanf("%d\n", &number_mines);

// TODO: Scan in the number of pairs of mines.

printf("Enter pairs:\n");
scanf("%d %d\n", &x, &y);

printf("Game Started\n");

for(int i = 0; i < 8; i++){
    for(int j = 0; j< 8; j++){

        if (x == i || y == j){
            printf("2");
        }
        else{
            printf("1");
            }
    }

} 
c arrays multidimensional-array scanf minesweeper
1个回答
0
投票

我相信您要创建两个数组。数组之一将保存x坐标,另一个将保存y坐标。然后,您将在while或for循环中使用number_mines变量,并接受每个响应并将其附加到受尊重的数组中。尝试不使用scanf而是使用fgets。

© www.soinside.com 2019 - 2024. All rights reserved.