title: Using arrays in a Tic Tac Toe program
body: <p>I've written (with a bit of help) a tic tac toe program. However the program does not utilise arrays to hold the moves but uses char variables. This means that there is a lot of repetitive code. I would like to use arrays but although I understand its concept, I am having difficulty in figuring out how to implement it. What type of array do I use? And I am also assuming that the bulk of the code (checking for valid moves, checking for wins, etc...) will change. </p>
  
  <pre><code>#include &lt;iostream&gt;
  
  using namespace std;
  
  
  int main()
  {
      char cell1 = '1';
      char cell2 = '2';
      char cell3 = '3';
      char cell4 = '4';
      char cell5 = '5';
      char cell6 = '6';
      char cell7 = '7';
      char cell8 = '8';
      char cell9 = '9';
      char player = '1';
      char mark;
      bool gameover = false;
  
      do
      {
          cout &lt;&lt; cell1 &lt;&lt;"|"&lt;&lt; cell2 &lt;&lt; "|" &lt;&lt; cell3 &lt;&lt; endl;
          cout &lt;&lt;"-+-+-"&lt;&lt; endl;
          cout &lt;&lt; cell4 &lt;&lt;"|"&lt;&lt; cell5 &lt;&lt; "|" &lt;&lt; cell6 &lt;&lt; endl;
          cout &lt;&lt;"-+-+-"&lt;&lt; endl;
          cout &lt;&lt; cell7 &lt;&lt;"|"&lt;&lt; cell8 &lt;&lt; "|" &lt;&lt; cell9 &lt;&lt; endl;
          cout &lt;&lt;"-+-+-"&lt;&lt; endl;
  
          //printBoard();
  
          //Handle player marker
          if (player == '1')
          {
              mark = 'X';
          }
          else
          {
              mark = 'O';
          }
  
          bool invalid; 
          do
          {
              cout &lt;&lt; "Player "&lt;&lt;player&lt;&lt; ": \n";
              char move;
              cin &gt;&gt; move;
              invalid = false;
  
  
              //Validate move
  
              if (move == '1' &amp;&amp; cell1 == '1')
              {
                  cell1 = mark;
              }
  
              else if (move == '2' &amp;&amp; cell2 == '2')
              {
                  cell2 = mark;
              }
  
              else if (move == '3' &amp;&amp; cell3 == '3')
              {
                  cell3 = mark;
              }
  
              else if (move == '4' &amp;&amp; cell4 == '4')
              {
                  cell4 = mark;
              }
  
              else if (move == '5' &amp;&amp; cell5 == '5')
              {
                  cell5 = mark;
              }
  
              else if (move == '6' &amp;&amp; cell6 == '6')
              {
                  cell6 = mark;
              }
  
              else if (move == '7' &amp;&amp; cell7 == '7')
              {
                  cell7 = mark;
              }
  
              else if (move == '8' &amp;&amp; cell8 == '8')
              {
                  cell8 = mark;
              }
  
              else if (move == '9' &amp;&amp; cell9 == '9')
              {
                  cell9 = mark;
              }
  
              else
              {
                  cout &lt;&lt;" Invalid Move"&lt;&lt;endl;;
                  invalid = true;
              }
          }
          while (invalid);                             
  
          //Check for winning &amp; draw conditions
  
          bool draw = false;
          if (cell1 != '1')
          {
              if (cell1 == cell2 &amp;&amp; cell1 == cell3)
              {
                  gameover = true;
              }
  
              else if (cell1 == cell4 &amp;&amp; cell1 == cell7)
              {
                  gameover = true;
              }
          }
  
          if (cell9 != '9')
          {
              if (cell9 == cell3 &amp;&amp; cell9 == cell6)
              {
                  gameover = true;
              }
  
              else if (cell9 == cell8 &amp;&amp; cell9 == cell7)
              {
                  gameover = true;
              }
          }
  
          if (cell5 != '5')
          {
              if (cell5 == cell2 &amp;&amp; cell5 == cell8)
              {
                  gameover = true;
              }
  
              else if (cell5 == cell4 &amp;&amp; cell5 == cell6)
              {
                  gameover = true;
              }
  
              else if (cell5 == cell1 &amp;&amp; cell5 == cell9)
              {
                  gameover = true;
              }
  
              else if (cell5 == cell3 &amp;&amp; cell5 == cell7)
              {
                  gameover = true;
              }
  
          }
  
          // Check for draw conditions
          if ( cell1 != '1' &amp;&amp; cell2 != '2' &amp;&amp; cell3 != '3' &amp;&amp; 
              cell4 != '4' &amp;&amp; cell5 != '5' &amp;&amp; cell6 != '6' &amp;&amp;
              cell7 != '7' &amp;&amp; cell8 != '8' &amp;&amp; cell9 != '9')
          {
              cout &lt;&lt;" Its a draw"&lt;&lt;endl;
              gameover = true;
              draw = true; 
          }
  
          if (gameover)
          {
              if (draw == false)
              {
                  cout &lt;&lt; "Player "&lt;&lt; player &lt;&lt; " wins!"&lt;&lt;endl;
                  cout &lt;&lt;endl;
              }
  
              //printBoard();
              cout &lt;&lt; cell1 &lt;&lt;"|"&lt;&lt; cell2 &lt;&lt; "|" &lt;&lt; cell3 &lt;&lt; endl;
              cout &lt;&lt;"-+-+-"&lt;&lt; endl;
              cout &lt;&lt; cell4 &lt;&lt;"|"&lt;&lt; cell5 &lt;&lt; "|" &lt;&lt; cell6 &lt;&lt; endl;
              cout &lt;&lt;"-+-+-"&lt;&lt; endl;
              cout &lt;&lt; cell7 &lt;&lt;"|"&lt;&lt; cell8 &lt;&lt; "|" &lt;&lt; cell9 &lt;&lt; endl;
              cout &lt;&lt;"-+-+-"&lt;&lt; endl;
              cout &lt;&lt;endl;
  
              cout&lt;&lt; "Play again? (y/n)"&lt;&lt; endl;
              char playAgain;
              cin &gt;&gt; playAgain;
  
              //Reset board if user wants to play again
              if (playAgain == 'y')
              {
                  gameover = false;
                  cell1 = '1';
                  cell2 = '2';
                  cell3 = '3';
                  cell4 = '4';
                  cell5 = '5';
                  cell6 = '6';
                  cell7 = '7';
                  cell8 = '8';
                  cell9 = '9';
  
                  player = '1';
              }
          }
  
          else
          {
  
              //Alternate players
              if (player == '1')
              {
                  player = '2';
              }
              else
              {
                  player = '1';
              }
          }
  
  
      }
      while (gameover == false);                          
      return 0;
  }
  </code></pre>