2017-09-25 1 views
0

私は掃海艇の試合をしようとしていましたが、爆弾に隣接するセルが見つかるまで、隣接するセルを再帰的に表示します。私は座標xとyを与えれば、それを取り巻く鉱山の数を計算するという方法があります。掃海掲示板に鉱山を再帰的にマッピングする

// Counts how many mines are adjacent to a given coordinate cell if any 
void board::mineCount(int x, int y) { 

// North 
if (y > 0) { 
    if (board[x][y - 1].hasMine) { 
     board[x][y].mineCount++; 
    } 
} 

// South 
if (y < dimensions[1] - 1) { 
    if (board[x][y + 1].hasMine) { 
     board[x][y].mineCount++; 

    } 
} 

// East 
if (x < dimensions[0] - 1) { 
    if (board[x + 1][y].hasMine) { 
     board[x][y].mineCount++; 

    } 
} 

// West 
if (x > 0) { 
    if (board[x - 1][y].hasMine) { 
     board[x][y].mineCount++; 
    } 
} 

// North East 
if (x < dimensions[0] - 1 && y > 0) { 
    if (board[x + 1][y - 1].hasMine) { 
     board[x][y].mineCount++; 

    } 
} 

// North West 
if (x > 0 && y > 0) { 
    if (board[x - 1][y - 1].hasMine) { 
     board[x][y].mineCount++; 
    } 
} 

// South East 
if (x < dimensions[0] - 1 && y < dimensions[1] - 1) { 
    if (board[x + 1][y + 1].hasMine) { 
     board[x][y].mineCount++; 

    } 
} 

// South West 
if (x > 0 && y < dimensions[1] - 1) { 
    if (board[x - 1][y + 1].hasMine) { 
     board[x][y].mineCount++; 
    } 
    } 
} 

各セルは1鉱山、それに隣接して見出されるたびにインクリメントされますmineCountフィールドを有する構造体です。私は再帰論理がどこに行くのか分かりません。私は次のようなことを試みました:

// North 
if (y > 0) { 
    if (board[x][y - 1].hasMine) { 
     board[x][y].mineCount++; 
    } else { 
     minecount(x, y-1); 
    } 
} 

各位置については役に立たない。任意のポインタが評価されるだろう。

+1

あなたが試した再帰の動作は何ですか?それについて何が間違っていましたか? – Tyler

+0

無関係:あなた自身の努力を惜しまないで、開始時に各グリッド座標のminecountを1回計算します。このロジックを大幅に削減し、バグや解決策を見つけやすくする必要があります。 – user4581301

答えて

0

再帰は、鉱山数自体を実行するコードの一部であってはなりません。それは近くのタイルを明らかにする役割を果たす関数の一部でなければなりません。

int get_adjacent_mine_count(point p) { 
    int mine_count = 0; 
    for(int i = -1; i <= 1; i++) { 
     for(int j = -1; j <= 1; j++) { 
      point this_point(p.x + i, p.y + j); 
      //is_inside_board checks to see if the point's coordinates are less than 0 
      //or greater than the board size 
      if(!is_inside_board(board, this_point)) continue; 
      //We ignore the center tile 
      if(i == 0 && j == 0) continue; 

      if(board(this_point).hasMine) 
       mine_count++; 
     } 
    } 
    return mine_count; 
} 

void reveal_tiles(point p) { 
    //We shouldn't throw if the recursion is correct 
    if(board(p).hasMine) throw Explosion("Stepped on a Mine!"); 
    //Single call to previously defined function 
    int num_of_adjacent_mines = get_adjacent_mine_count(p); 
    //I'm assuming this gets initialized to -1 beforehand 
    board(p).revealed = num_of_adjacent_mines; 
    if(num_of_adjacent_mines == 0) { 
     for(int i = -1; i <= 1; i++) { 
      for(int j = -1; j <= 1; j++) { 
       point this_point(p.x + i, p.y + j); 
       if(!is_inside_board(board, this_point)) continue; 
       if(i == 0 && j == 0) continue; 
       if(board(this_point).revealed == -1) 
        reveal_tiles(this_point); 
      } 
     } 
    } 
} 

は、私は強くそれはちょうど2次元配列と対話しようとするよりもはるかに堅牢なソリューションですので、あなたが、私のコードは、あなたがやった意味boardを、表現するために、単純なMatrixクラスを書くお勧めするつもりですあなたはそれをやっているCスタイルの方法。

関連する問題