我在完成这项任务时遇到了麻烦。我应该创建minesweeper-ish数组。它读入一个文件:
100
3
0 0
2 1
7 7
10
0 0
0 1
0 2
0 3
0 4
0 5
0 6
0 7
7 0
7 1
第一行是板数(100),第二行是板上炸弹的数量。 2个整数线分别是炸弹位置的行和列。
我的问题是它只能打开一块板子而我的第二个问题是如何将9切换到板子中的*?
谢谢你!#include <stdio.h>
#define BOARD_SIZE 8
#define BOMB 9
int main() {
FILE *fp;
fp = fopen("mine.txt","r");
int numberBoards = 0;
int numberMines = 0;
int col;
int row;
int currentBoard = 0;
// first row is going to be the number of boards
fscanf(fp,"%d",&numberBoards);
while ( fscanf(fp,"%d",&numberMines) > 0 ) {
int i,j;
// start board with all zeros
int board[BOARD_SIZE][BOARD_SIZE] = { {0} };
currentBoard++;
printf("Board #%d:\n",currentBoard);
//Read in the mines and set them
for (i=0; i<numberMines; i++) {
fscanf(fp,"%d %d",&col,&row);
board[col-1][row-1] = BOMB;
}
//mine proximity
for (i=0; i<BOARD_SIZE; i++) {
for (j=0; j<BOARD_SIZE; j++) {
if ( board[i][j] == BOMB ) {
//Square to the left
if (j > 0 && board[i][j-1] != BOMB) {
board[i][j-1]++;
}
//Square to the right
if (j < 0 && board [i][j+1] !=BOMB){
board [i][j+1]++;
}
//Square to the top
if (i > 0 && board [i-1][j] !=BOMB) {
board [i-1][j]++;
}
//Square to the bottom
if ( i < 0 && board [i+1][j] !=BOMB){
board [i+1][j]++;
}
}
}
//Print out the minesweeper board
for (i=0; i<BOARD_SIZE; i++) {
for (j=0; j<BOARD_SIZE; j++) {
printf("%d ",board[i][j]);
}
printf("\n");
}
printf("\n");
}
//Close the file.
fclose(fp);
return 0;
}
}
答案 0 :(得分:2)
fclose
和return
不应该在while循环中 - 这就是为什么它只创建一个主板。
答案 1 :(得分:0)
我不明白你关于打印多个电路板的问题。要打印*
而不是9
:
if ( board[i][j] == 9 ) printf("* ");
else printf("%d ",board[i][j]);