title: How store or generate a list of 2d indexes
body: <p>For a tic tac toe game I have a <code>Board</code> class which stores the game board in two-dimensional array. Now that I'm working on the AI portion of my project I'm thinking I need to keep a list of unoccupied cells in the <code>Board</code> class or generate the list in the <code>AIPlayer</code> class. However, since I'm using a 2d array I'm unsure how to go about this. </p>
  
  <p>What would be the best method of doing this?</p>
  
  <p>Here is my Board class:
  header file:</p>
  
  <pre><code>#pragma once // include guard
  #include &lt;iostream&gt;
  
  class Board
  {
      public:
          Board();
          void displayBoard() const;
          char getCell(int row, int column) const;
          void setCell(int row, int column, char player);
          bool isWon(char token) const;
          bool isDraw()const;
  
      private:
          char board[3][3];
          int occupiedCells;  
  };
  </code></pre>
  
  <p>implementation file:</p>
  
  <pre><code>#include "Board.h"
  #include &lt;iostream&gt;
  
  Board::Board()
  {
      occupiedCells = 0;
      for (int row = 0; row &lt; 3; row++)
      {
          for (int column = 0; column &lt; 3; column++)
              board[row][column] = ' ';
      }
  }
  
  void Board::displayBoard() const
  {
      std::cout &lt;&lt; "\n-------------" &lt;&lt; std::endl;
  
      for (int row = 0; row &lt; 3; row++)
      {
          std::cout &lt;&lt; "| " ;
          for (int column = 0; column &lt; 3; column++)
              std::cout &lt;&lt; board[row][column] &lt;&lt; " | ";
          std::cout &lt;&lt; "\n-------------" &lt;&lt; std::endl;
      }
  }
  
  char Board::getCell(int row, int column) const
  {
      return board[row][column];
  }
  
  void Board::setCell(int row, int column, char player)
  {
      board[row][column] = player;
      occupiedCells ++;
  }
  
  bool Board::isWon(char token) const
  {
      // Check rows
      for (int i = 0; i &lt; 3; i++)
          if (token == board[i] [0] &amp;&amp; token == board[i] [1] &amp;&amp; token == board[i] [2]) return true;
  
      // Check columns
      for (int j = 0; j &lt; 3; j++)
          if (token == board[0] [j] &amp;&amp; token == board[1] [j] &amp;&amp; token == board[2] [j]) return true;
  
      // Check diagonals
      if (token == board[0] [0] &amp;&amp; token == board[1] [1] &amp;&amp; token == board[2] [2]) return true;
  
      if (token == board[0] [2] &amp;&amp; token == board[1] [1] &amp;&amp; token == board[2] [0]) return true;
  
      return false;   
  }
  
  bool Board::isDraw() const
  {
      if (occupiedCells == 9)
      {
          return true;
      }
      return false;
  }
  </code></pre>
  
  <p>And here is my AIPlayer class:
  header file:</p>
  
  <pre><code>#pragma once // include guard
  #include "Player.h"
  
  class AIPlayer: public Player
  {
      public:
          AIPlayer(char token);
          virtual void makeAMove(Board &amp;myBoard);
  };
  </code></pre>
  
  <p>implementation file:</p>
  
  <pre><code>#include &lt;stdlib.h&gt; // rand, srand
  #include &lt;time.h&gt; // time
  #include "AIPlayer.h"
  
  AIPlayer::AIPlayer(char token) : Player(token)
  {
  }
  
  void AIPlayer::makeAMove(Board &amp;myBoard)
  {
      int row;
      int column;
      srand (time(0));    
  
      bool done = false;
      do
      {   
          row = rand() % 3;
          column = rand() % 3;
  
          if (myBoard.getCell(row, column) == ' ')
          { 
              myBoard.setCell(row, column, getToken());
              done = true;
          }
      }
      while (!done);
  
      std::cout &lt;&lt;    "\nComputer move (" &lt;&lt; getToken() &lt;&lt; ")\n"
                      "row " &lt;&lt; row &lt;&lt; ", column " &lt;&lt; column;
  }
  </code></pre>