When a new instance of the GameBoard
class is created, the constructor calls the ClearBoard()
helper method, which simply creates 80 empty game pieces and assigns them to each element in the array.
Tip
Helper methods
Why not simply put the two for loops that clear the board into the GameBoard
constructor? Splitting the work into methods that accomplish a single purpose greatly helps to keep your code both readable and maintainable. Additionally, by splitting ClearBoard()
out as its own method we can call it separately from the constructor. When we add increasing difficulty levels in Chapter 3, we will make use of this call when a new level starts.
The boardSquares
array in the GameBoard class is declared as a private member, meaning that the code that uses the GameBoard will not have direct access to the pieces contained on the board.
In order for code in our Game1 class to interact with a GamePiece, we will need to create public methods in the GameBoard class that expose the pieces in boardSquares
.