[英]MineSweeper reveal neighbors help needed java
我目前正在研究扫雷程序,需要一些帮助来揭示扫雷程序中的邻居。 目前,我的程序可以做的显示如下
1将是被选择的按钮,这些行是我需要填写的内容。当前所选按钮周围的按钮是我可以填写的内容。
如果需要,我可以发布代码。
我在这里先向您的帮助表示感谢。
1是地雷,4是阵列上的标记点
public int findneighbors(int row, int col) {
int count = 0;
if (board[row][col] == 2)
try {
if (board[row][col + 1] == 1 || board[row][col + 1] == 4)
count ++;
}
catch( ArrayIndexOutOfBoundsException e)
{
}
try {
if (board[row + 1][col + 1] == 1 || board[row + 1][col + 1] == 4)
count ++;
}
catch( ArrayIndexOutOfBoundsException e)
{
}
try {
if (board[row + 1][col - 1] == 1 || board[row + 1][col - 1] == 4)
count ++;
}
catch( ArrayIndexOutOfBoundsException e)
{
}
try {
if (board[row - 1][col - 1] == 1 || board[row - 1][col - 1] == 4)
count ++;
}
catch( ArrayIndexOutOfBoundsException e)
{
}
try {
if (board[row][col + 1] == 1 || board[row][col + 1] == 4)
count ++;
}
catch( ArrayIndexOutOfBoundsException e)
{
}
try {
if (board[row + 1][col] == 1 || board[row + 1][col] == 4)
count ++;
}
catch( ArrayIndexOutOfBoundsException e)
{
}
try {
if (board[row - 1][col] == 1 || board[row - 1][col] == 4)
count ++;
}
catch( ArrayIndexOutOfBoundsException e)
{
}
try {
if (board[row][col - 1] == 1 || board[row][col - 1] == 4)
count ++;
}
catch( ArrayIndexOutOfBoundsException e)
{
}
try {
if (board[row - 1][col + 1] == 1 || board[row - 1][col + 1] == 4)
count ++;
}
catch( ArrayIndexOutOfBoundsException e)
{
}
return count;
}
public int buttonFloodFill(int r, int c)
{
int loopCount = 0;
int rowCount = 1;
int colCount = 1;
while (loopCount < 1)
{
try {
if (g.getFloodValue(r,c + colCount) == true) {
board[r][c + colCount].setText(Integer.toString(g.findneighbors(r,c + colCount)));
board[r][c + colCount].setEnabled(false);
}
}
catch( ArrayIndexOutOfBoundsException e)
{
}
try {
if (g.getFloodValue(r,c - colCount) == true) {
board[r][c - colCount].setText(Integer.toString(g.findneighbors(r,c - colCount)));
board[r][c - colCount].setEnabled(false);
}
}
catch( ArrayIndexOutOfBoundsException e)
{
}
try {
if (g.getFloodValue(r + rowCount,c + colCount) == true) {
board[r + rowCount][c + colCount].setText(Integer.toString(g.findneighbors(r + rowCount,c + colCount)));
board[r + rowCount][c + colCount].setEnabled(false);
}
}
catch( ArrayIndexOutOfBoundsException e)
{
}
try {
if (g.getFloodValue(r + rowCount,c - colCount) == true) {
board[r + rowCount][c - colCount].setText(Integer.toString(g.findneighbors(r + rowCount,c - colCount)));
board[r + rowCount][c - colCount].setEnabled(false);
}
}
catch( ArrayIndexOutOfBoundsException e)
{
}
try {
if (g.getFloodValue(r - rowCount,c - colCount) == true) {
board[r - rowCount][c - colCount].setText(Integer.toString(g.findneighbors(r - rowCount,c - colCount)));
board[r - rowCount][c - colCount].setEnabled(false);
}
}
catch( ArrayIndexOutOfBoundsException e)
{
}
try {
if (g.getFloodValue(r - rowCount,c + colCount) == true) {
board[r - rowCount][c + colCount].setText(Integer.toString(g.findneighbors(r - rowCount,c + colCount)));
board[r - rowCount][c + colCount].setEnabled(false);
}
}
catch( ArrayIndexOutOfBoundsException e)
{
}
try {
if (g.getFloodValue(r - rowCount,c) == true) {
board[r - rowCount][c].setText(Integer.toString(g.findneighbors(r - rowCount,c)));
board[r - rowCount][c].setEnabled(false);
}
}
catch( ArrayIndexOutOfBoundsException e)
{
}
try {
if (g.getFloodValue(r + rowCount,c) == true) {
board[r + rowCount][c].setText(Integer.toString(g.findneighbors(r+ rowCount,c)));
board[r + rowCount][c].setEnabled(false);
}
}
catch( ArrayIndexOutOfBoundsException e)
{
}
rowCount ++;
colCount ++;
loopCount ++;
}
return 0;
}
好吧,在看完您的代码后,我认为最好为您提供一些一般性准则,而不是尝试解决此特定问题。
Java是为面向对象编程设计的语言。 您所做的更多是一种过程方法,这种方法当然也可以使用,但是由于您使用Java,所以我假设您想利用语言功能来发挥自己的优势。
让我们看看我们可以在您的项目中找到什么样的“对象”。 您已经有一块板子,即电池阵列。 您显示的代码将是该板的一部分,您可以将其与与该板无关的代码分开。
当前,您的面板由代表该单元状态的整数组成。 现在,将单元格也变成对象不是很有趣吗? 这样一来,一块板上就会有一个有状态的单元格阵列。 现在您可以说这只会增加您必须推理的级别数。 在某种程度上,这也是正确的,但是,每个级别都是一个明确定义的概念,您可以单独进行推理。 让我们看一些示例代码:
class Cell {
private int state; //whatever your default is
public Cell(int state) {
this.state = state;
}
现在我们可以添加一些方法来检查状态:
public boolean hasMine() {
return state == 1;
}
public boolean isFlagged() {
return state == 4;
}
同样,您可以添加方法来更改状态:
public void flag() {
state = 4;
}
我只列出了一些方法,我认为应该清楚如何编写其他方法。 (此外,我现在仍将状态保留为整数。一旦您对OO编程有了更高级的了解,您可能希望查看Java枚举或状态模式)
现在让我们看一下董事会。 当前,您有一个名为findneighbours的方法,该方法返回相邻地雷的数量。 就个人而言,我会将此方法称为更清晰的名称,例如getAdjecentMineCount。 但是,我希望还可以使用getNeighbours方法,该方法返回单元格的所有相邻单元格。 然后,您可以使用此getNeighbours方法轻松找到相邻地雷的数量,如下所示:
public int getAdjecentMineCount(int row, int col) {
int count=0;
for (Cell c : getNeighbours(row, col)) //this iterates over the neighbours that are returned by the getNeighbours function
if (c.hasMine())
count++;
return count;
}
现在,让我们看一下显示一个单元格。 让我们不费吹灰之力,制作一个名为revealCell的方法:
public void revealCell(int row, int col) {
if (board[row][col].hasMine())
System.out.println("BOOM"); //whatever has to happen when you click on a bomb
//now we also want to reveil any non-diagonal neighbour that doesn't have a mine
for (Cell c : getNonDiagonalNeighbours(row, col))
if (!c.hasMine() && !c.isRevealed())
reveilCell(rowOf(c), columnOf(c));
}
注意再次递归调用相同的方法。 这将导致细胞链被改版。
我故意在代码中留下了一些漏洞,例如查找邻居的方法。 我希望我的解释会以正确的方式推动您,并且您可以自己进一步了解该语言。 如果您还有其他问题,请随时与我联系。
(免责声明:我绝对不会说我在这里提供给您的解决方案是理想的解决方案。我旨在做的就是指导您编写更简洁,更面向对象的代码。)
原谅我重新实现您的整个代码,但这比尝试理解您的代码要快...
public class Minefield {
int mx, my;
/** whether a mine is present */
boolean[][] mined;
/** the number of mines in neighboring cells */
int[][] mines;
/** whether this cell is revealed */
boolean[][] revealed;
public Minefield() {
Random chaos = new Random();
mx = 10;
my = 10;
for (int x = 0; x < mx; x++) {
for (int y = 0; y < my; y++) {
mined[x][y] = chaos.nextFloat() < 0.2;
}
}
for (int x = 0; x < mx; x++) {
for (int y = 0; y < my; y++) {
mines[x][y] = 0;
for (int nx = max(x - 1, 0); nx < mx && nx <= x + 1; nx++) {
for (int ny = max(y - 1, 0); ny < my && ny <= y + 1; ny++) {
if (mined[nx][ny]) {
mines[x][y]++;
}
}
}
}
}
}
void stepOn(int x, int y) {
reveal(x, y);
if (mined[x][y]) {
throw new GameOverException();
}
}
void reveal(int x, int y) {
if (!revealed[x][y]) {
revealed[x][y] = true;
if (mines[x][y] == 0) {
for (int nx = max(x - 1, 0); nx < mx && nx <= x + 1; nx++) {
for (int ny = max(y - 1, 0); ny < my && ny <= y + 1; ny++) {
reveal(nx, ny);
}
}
}
}
}
注意:我尚未测试该代码,但希望您能理解。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.