Time for action – initializing the game board
Add a constructor to the
GameBoard
class:Public Sub New(pieceTexture As Texture2D, emptyArea As Rectangle) playingPieces = pieceTexture emptyPiece = emptyArea ClearBoard() End Sub
Add the
ClearBoard()
helper method to theGameBoard
class:Public Sub ClearBoard() Dim x, y As Integer For x = 0 To GameBoardWidth For y = 0 To GameBoardHeight boardSquares(x, y) = New GamePiece("Empty") Next Next End Sub
What just happened?
When a new instance of the GameBoard
class is created, we store the texture and rectangle values that we will need for drawing, and 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 work up into methods that accomplish a single purpose greatly helps to keep your code both readable and maintainable. Additionally...