if语句抛出不兼容的类型:意外的返回值



我目前正在创建一个扫雷游戏,目前我刚刚完成一个计算相邻水雷的方法,它还有行、列和水雷数量的参数,我还声明了一个maxMines变量,该变量不允许网格上有超过20个水雷。。。现在,我想让这个方法在正方形被成功挖掘的情况下返回true,如果超过了最大地雷数量,或者正方形已经被挖掘,则返回false。

此外,我希望这种方法使用参数来检查地雷是否在这些坐标上,我不知道从哪里开始,如果有人能给我一个起点,我将不胜感激。

这是我的代码(错误位于mineTile的最底部;if语句(:

import java.util.Random;
public class Minefield extends Minesweeper{
boolean[][] minefield;
int[][] minedNeighbour;
Random random = new Random();//random number generator
int row;
int column;
int mines;
public int emptySpaces = column*row;
int maxMines = 20;
public Minefield(int row, int column, int mines) {
this.row = row;
this.column = column;
this.mines = mines;
int x,y;
minedNeighbour = new int[10][10];
//initializeMines();   //fill with zero's
//placeMines();  //get the random numbers and place 10 mines
//fillNoOfSurroundingNeighbours();     //based on the mines 8 boxes surronding the mine will be calculated and shown to the player
//startBoard();   //This fills the actual board
}
public void mineTile(int x, int y){
int i,j; //loop variables
if(minedNeighbour[x][y]!= -1)return; //already used
minedNeighbour[x][y] = 0;//square used
emptySpaces--;//decreases the emptyspaces counter
for (i = x-1 ;i<=x+1 ; i++)
{
for(j = y-1 ;j<=y+1 ; j++)
{
if(minedNeighbour[i][j] == 99)
{
minedNeighbour[x][y]++;
}
if(i >= 0 && j >= 0 && i < 5 && j < 5)
{
if(minedNeighbour[i][j] == 99)
{
minedNeighbour[x][y]++;
}
}
}
}
if(mines > maxMines)
{
return false;
}
else {
return true;
}
}
}

您的方法被设置为返回一个void,这意味着您不能返回任何内容。如果您想返回布尔值,请更改方法头以反映这一点,即将其更改为

public boolean mineTile(int x, int y)

最新更新