【问题标题】:Recursively mapping mines on a minesweeper board在扫雷板上递归地映射地雷
【发布时间】:2017-09-25 20:24:26
【问题描述】:

我一直在尝试制作一个扫雷游戏,其中给定一个单元格的坐标,它将递归地显示相邻的单元格,直到找到与炸弹相邻的单元格。我有一个方法,给定坐标 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++;
    }
  }
}

每个单元格都是一个结构体,它有一个mineCount 字段,每次在它附近发现一个地雷时,该字段就会增加1。我无法弄清楚我的递归逻辑会去哪里。我尝试做类似的事情:

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

对于每个职位,但无济于事。任何指针将不胜感激。

【问题讨论】:

  • 您尝试的递归行为是什么,有什么问题?
  • 不相关:节省大量精力并在开始时计算每个网格坐标的 minecount。它应该可以让你大大减少这个逻辑,让你更容易发现你的错误/解决方案。

标签: c++ multidimensional-array minesweeper


【解决方案1】:

递归不应该是执行地雷计数本身的代码的一部分。它应该是负责显示附近瓷砖的函数的一部分。

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

我强烈建议你编写一个简单的 Matrix 类来表示 board,我的代码暗示你已经完成了,因为这是一个比仅仅尝试与二维数组交互更强大的解决方案C 风格的方式。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-03-25
    • 1970-01-01
    • 1970-01-01
    • 2017-04-03
    • 2023-04-10
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多