Well done !!! I played it a while and it worked for me Took me a while to figure out that right click was reset.
Waiting for your middle mouse implementation, that would make it interesting. A seconds timer would make it more exciting. I didnt understand the reason to have a code block in line 90, 97, 114. The variables modified in them are instance level so scoping isnt benefiting here (Unless I missed something)
Some nitpicks about the code:
Everything is done in a single class.
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE) instead of dispose
I think you kinda shot yourself in the foot by making the State class' memory representation so dense. It looks like you packed all of the following properties in a single integer value:
Whether the square is empty.
Whether the square contains a mine.
Whether the square has adjacent mines.
How many other mines the square is adjacent to.
Whether the player put a flag on the square.
Whether the square is revealed.
Whether the square is selected.
Not only is the handling of these flags tricky, the meaning of some of them are not disjoined. For instance, EMPTY is redundant because it is implied when QTY, MINE and FLAG are not set. QTY is redundant because you can infer it from the most significant three bytes. VISIBLE and SELECTED have no place in the square at all, because they are properties of the user interface, not the game model.
As I see it, squares have two definite properties: Whether they contain a mine and whether they are concealed, flagged or revealed. A third property can be either how many mines they are adjacent to, or a list of neighboring squares. Keeping track of how many mines a square is adjacent to probably makes for a simpler model, so let's go with that. I would then write a basic Square class like this:
Now, I can use this Square class to make a MineField:
As you can see, there is no UI code in here. One can easily write a Swing user interface in a separate package that reacts to events on the model by adding a SquareChangeListener to the MineField.
Run away! Run away! Here, take this tiny ad with you: