So far, we have mostly covered straightforward code that takes care of basic game mechanics. There is one problem, though. The game can produce a board that is impossible to make a move on. Worse, we have no way of knowing that there are no more moves remaining. We aim to correct that deficiency now, in a rather dense method called
checkMovesRemaining
. First, we should cover the basic concept of how this is accomplished.
If you recall, the
checkForMatchesOfType
method we reviewed earlier did a good job of finding any actual matches on the board. We could code this predictive method in that style, but it quickly gets confusing since you need to be able to determine matches up to five gems in a row to get an accurate count of moves remaining. Here, we take another approach by writing the gemType
values into a "C-style" array, so we can easily get a single view of the whole board without massive nested loops.
The challenge is determining all the possible ways a player can...