Time for action – initialize the game board

  1. Add a constructor to the GameBoard class:
      public GameBoard()
      {
          ClearBoard();
      }
  2. Add the ClearBoard() helper method to the GameBoard class:
      public void ClearBoard()
      {
          for (int x = 0; x < GameBoardWidth; x++)
              for (int y = 0; y < GameBoardHeight; y++)
                  boardSquares[x, y] = new GamePiece("Empty");
      }

What just happened?

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 ...

Get XNA 4.0 Game Development by Example Beginner's Guide now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.