CHAPTER 7 ■ WRITING A USER INTERFACE
return count;
}
boolean getMineCleared(int x, int y) {
return mineField[x][y].isCleared();
}
Mine.flagState getMineFlag(int x, int y) {
return mineField[x][y].getFlagState();
}
boolean isMine(int x, int y) {
return mineField[x][y].hasMine();
}
}
Let's start at the top, with the fields (remember, a variable at the class level is called a field). Listing
7-13 shows the fields in the MineField class.
Listing 7-13. The MineField class's fields
private Mine[][] mineField;
private int rows;
private int columns;
private int mines;
private int minesFound = 0;
private int minesRemaining;
private int emptiesRemaining;
enum gameState {WIN, LOSE, CONTINUE};
First, we have a two-dimensional array that holds the locations of the mines. Then we get a bunch of
integer values to tell us how many rows, columns, and mines this instance of the game should have.
Then we get more int values to tell us the state (number of mines found, number of mines remaining,
and empty spaces remaining) of the current game. We add a minesRemaining field to avoid having to type
mines - minesFound repeatedly. The emptiesRemaining field is more interesting, because it lets us know
when the user has won. Finally, we have a tri-state enumeration that tells us the current state of the
whole game—specifically, whether the player has won, the player has lost, or the player can continue the
current game.
■ Note The gameState enumeration is not private (meaning it is visible to all the other classes in the package)
because the other classes use its values in various places, as we see later.
The MineField class has a constructor (used by the init method in the MineSweeper class). Listing 7-
14 shows the MineField constructor.