if statement throws incompatible types: unexpected return value

Joey2Socks :

I'm currently creating a minesweeper game, at the moment I'm just finishing up a method that counts neighboring mines, it also has parameters for the number of rows, columns and mines, I also declared a maxMines variable that doesn't allow more than 20 mines on the grid... Now, I want to make this method return true if the square was successfully mined, and false if the maximum number of mines was exceeded, or the square has already been mined.

Also I want this method to use the parameters to check whether a mine would be on those coordinates and I have no idea where to begin, if anyone can give me a starting point, it would be greatly appreciated.

Here is my code (The error lies at the very bottom of mineTile; if statement):

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

    }
}
Nicolas Schapeler :

Your method is set to return a void, so that means you can't return anything. If you want to return a boolean, change your method header to reflect that, i.e. by changing it to

public boolean mineTile(int x, int y)

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=420030&siteId=1