Java 7 for Absolute Beginners

(nextflipdebug5) #1

CHAPTER 7 ■ WRITING A USER INTERFACE


void setMine() {
hasMine = true;
}

boolean isCleared() {
return isCleared;
}

void clear() {
isCleared = true;
}

flagState getFlagState() {
return flag;
}

flagState setFlagState() {
if (flag == flagState.UNKNOWN) {
flag = flagState.MINE;
return flagState.MINE;
}
if (flag == flagState.MINE) {
flag = flagState.SUSPECT;
return flagState.SUSPECT;
}
if (flag == flagState.SUSPECT) {
flag = flagState.UNKNOWN;
return flagState.UNKNOWN;
}
return flagState.UNKNOWN;
}
}

The Mine class keeps track of whether a location has a mine, whether the location has been cleared,
and whether the user has flagged the location as having a mine, being a suspect (usually marked by a
question mark icon in minesweeper games), or being unknown (which means not having an icon in the
game). The only complexity is in the setFlagState method, which toggles through the states rather than
accepting an argument to set the flag state. That behavior models the user's behavior of cycling through
flags by right-clicking on a minefield location.
Now we can move on to the MineSweeperHelper class, which contains miscellaneous methods that
the game needs. Though not as large as the MineSweeper and MineField classes, it's still a large class when
compared to earlier work. Again, read through it and do your best to understand it (now that you know
how the rest of the program works, you can probably do pretty well with it), and then we examine it in
detail. Listing 7-20 shows the MineSweeperHelper class.

Listing 7-20. The MineSweeperHelper class

package com.apress.java7forabsolutebeginners.examples.MineSweeper;

import java.awt.Color;
Free download pdf