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

관련 없음 : 많은 노력을 덜고 시작시 한 번씩 각 그리드 좌표의 최소값을 계산하십시오. 이 논리를 극적으로 축소시켜 버그/솔루션을 쉽게 찾을 수 있도록해야합니다. – 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 스타일.

관련 문제