gpt4 book ai didi

java - 递归扫雷 "0-fill"

转载 作者:行者123 更新时间:2023-12-02 06:41:19 25 4
gpt4 key购买 nike

我正在创建一个 Java 的 Java 实现来娱乐,并且当您单击其中的一个池时,我尝试填充全零。 (玩扫雷看看我在说什么)

这是我的递归调用:

private void revealZeros(int x, int y) {

if (board[y][x].revealed)
return;
board[y][x].revealed = true;
if (y > 0) {
if (x > 0)
if (!board[y - 1][x - 1].revealed && board[y - 1][x - 1].b == 0)
revealZeros(y - 1, x - 1);
if (x < 15) {
if (!board[y - 1][x + 1].revealed && board[y - 1][x + 1].b == 0)
revealZeros(y - 1, x + 1);
}
if (!board[y - 1][x].revealed && board[y - 1][x].b == 0)
revealZeros(y - 1, x);
}
if (x > 0)
if (!board[y][x - 1].revealed && board[y][x - 1].b == 0)
revealZeros(y, x - 1);
if (x < 15)
if (!board[y][x + 1].revealed && board[y][x + 1].b == 0)
revealZeros(y, x + 1);
if (y < 15) {
if (x > 0)
if (!board[y + 1][x - 1].revealed && board[y + 1][x - 1].b == 0)
revealZeros(y + 1, x - 1);
if (x < 15)
if (!board[y + 1][x + 1].revealed && board[y + 1][x + 1].b == 0)
revealZeros(y + 1, x + 1);
if (!board[y + 1][x].revealed && board[y + 1][x].b == 0)
revealZeros(y + 1, x);
}

}

通话无法正常进行。它揭示了除 0 以外的 block ,并且不揭示所有 0 block 。

Space.b = 周围的炸弹数量
Space.revealed = 空间是否显露?

最佳答案

哇!!我能够想到一个解决方案。很简单。这是我的最终代码:

private void revealZeros(int x, int y) {
if (x < 0 || x > 15 || y < 0 || y > 15) return; // check for bounds

if ( board[y][x].b == 0 && !board[y][x].revealed) {
board[y][x].revealed = true;
revealZeros( x+1, y );
revealZeros( x-1, y );
revealZeros( x, y-1 );
revealZeros( x, y+1 );
} else {
return;
}
}

关于java - 递归扫雷 "0-fill",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19106911/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com