As part of my Uni course i have been given code to edit to create a MineFinder (mine sweeper) game. I have 3 seperate classes to create this game. But for now, my problem lies with this "MineFinderModel class...

the tasks i am struggling to complete is as follows:

"Now add two public methods to get and set the values of this new array:
public void uncover(int thisCol, int thisRow)
The uncover method changes the state of the specified square to false. Otherwise, if the input co-ordinates are outside the minefield or the square is already uncovered, it does nothing.

public boolean isCovered(int thisCol, int thisRow)
The isCovered method returns true if the specified square is covered. Otherwise, if the input co-ordinates are outside the minefield or the square is not covered, it returns false."

i have tried to implement these but given my little knowledge in java i am doing something wrong with these methods. The error states "java:94: error: missing return statement"

please can someone help me in correcting this issue? I cannot continue until this is resolved. If you need me to explain any part of my program then just ask.

class MineFinderModel
{
    public static int MINE_SQUARE = 10;     // variable for a square containing a mine
    public static int EMPTY_SQUARE = 0;     // variable for an empty square

    int num_of_cols;
    int num_of_rows;
    int[][] the_minefield;      // 2 Dimensional array for the minefield
    boolean[][] is_hidden;      // 2 Dimensional array for covered/uncovered mines

    public  MineFinderModel(int n_cols, int n_rows)     // constructor method
    {
        num_of_rows = n_rows;
        num_of_cols = n_cols;
        the_minefield = new int[num_of_cols][num_of_rows];
        is_hidden = new boolean[num_of_cols][num_of_rows];

        for (int i = 0;i<num_of_cols;i++)
        {
            for(int j = 0;j<num_of_rows;j++)
            {
                is_hidden[i][j] = true;
            }
        }
    }

    public boolean addMine(int thisCol, int thisRow)
    {
        if (thisCol >num_of_cols || thisRow > num_of_rows)
            return false;
        if (thisCol < 0 || thisRow < 0)
            return false;
        if (the_minefield[thisCol][thisRow] == MINE_SQUARE)
            return false;
        else
        {
            the_minefield[thisCol][thisRow] = MINE_SQUARE;
            return true;
        }
    }

    public int getValue(int thisCol, int thisRow)
    {
        if (thisCol >num_of_cols || thisRow > num_of_rows)
            return 0;
        if (thisCol < 0 || thisRow < 0)
            return 0;
        return the_minefield[thisCol][thisRow];
    }

    public void addMinesToCorners()
    {
        addMine(0, 0);
        addMine(num_of_cols-1, 0);
        addMine(0, num_of_rows-1);
        addMine(num_of_cols-1, num_of_rows-1);
    }

    public boolean addRandomMines(int m)
    {
        int num_mines = 0;

        while (num_mines < m)
        {
            int x = (int)(Math.random() * num_of_cols);
            int y = (int)(Math.random() * num_of_rows);

            if (addMine(x,y))
            {
                num_mines++;
            }

        }
        return true;
    }

    public void uncover(int thisCol, int thisRow)
    {
        is_hidden[thisCol][thisRow] = false;

    }

    public boolean isCovered(int thisCol, int thisRow)
    {
        if (thisCol >num_of_cols || thisRow > num_of_rows)
            return false;
        if (thisCol < 0 || thisRow < 0)
            return false;
        else
        {
            if (is_hidden[thisCol][thisRow] == true)
            return true;
        }                // java:94: error: missing return statement
    }

    public static void main (String[] args)
    {

    }
}

Recommended Answers

All 3 Replies

Don't worry, this one often catches beginners out!
You declare the method as returning a boolean, so the compiler checks your code to ensure that, no matter what happens in the if tests, you always return a boolean.
In your code, if all three if tests are false, you will get to line 94 without returning a value. You will need to add one more return statement to guarantee that you always return something.

thats brilliant! i knew it had to be something simple. Thanks alot :)

When your question has been answered please mark your thread "solved"; don't just leave it pending forever. Thanks

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.