2012-11-08 52 views
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; 
} 
} 

回答

0

我不明白你關於打印多個電路板的問題。要打印的*代替9

if (board[i][j] == 9) printf("* "); 
else printf("%d ",board[i][j]); 
+0

所以由於在輸入文件中的第一行是100,100塊板應打印。我只打印一塊電路板。 – user1676480

+0

但是,一旦你讀它,你永遠不會使用numberBoards! –

2

fclosereturn不應該在while循環 - 這就是爲什麼它是創建只有一個板。