0

我在完成这项任务时遇到了麻烦。我应该创建扫雷阵列。它读入一个文件:

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;
}
}
4

2 回答 2

2

fcloseand不应该在returnwhile 循环中——这就是它只创建一个板的原因。

于 2012-11-08T03:09:38.653 回答
0

我不明白你关于打印多块板的问题。要打印 a*而不是 a 9

if ( board[i][j] == 9 ) printf("* ");
else printf("%d ",board[i][j]);
于 2012-11-08T01:26:45.080 回答