title: Tic Tac Toe Programming
body: <p>I am pretty new in C++ programming. I am coding for Tic Tac Toe game to understand how its algorithm works. My question is about I have two functions. One of them is clearBoard function and the other one is drawBoard function. I got little help about them but I couldnt figure it out the way those functions works. Can anyone simply explain me what is exactly what? I have been trying to solve it and understand it but I think I got more confused. It might be a piece of cake but I really want to understand what's exactly going on. Thanks for your time and attention...</p>
  
  <p>Here clearBoard function: </p>
  
  <pre><code>void clearBoard(int board[])
  {
   int i;
    for(i = 0; i &lt; board_size; i++) {
     board[i] = -i - 1;
   }
  }
  </code></pre>
  
  <p>And here is my drawBoard function.</p>
  
  <pre><code> void drawBoard(int board[])
   {
    int i, j;
    int n = 0;
    for(i = 0; i &lt;= 6; i = i+3) {
     for(j = 0; j &lt; 3; ++j) {
      if(board[i + j] == 2)
      cout &lt;&lt; "|X|";
       else if(board[i + j] == 1)
      cout &lt;&lt; "|O|";
       else
      cout &lt;&lt; "|" &lt;&lt; n &lt;&lt; "|";
       n = n+1;
      }
     cout &lt;&lt; endl;
     }
    }
  </code></pre>
  
  <p>This is my main.cpp file. I just wanted to post my entire work at least it will be easier to see complete picture.</p>
  
  <pre><code> #include &lt;iostream&gt;
   #include &lt;ctime&gt;
   #include&lt;cstdlib&gt;
   #include "Define.h"
   using namespace std;
  
  
   int main()
   {
    int board[board_size];
    int turn = 0;
    int p = 0;
    int move = 10;
    srand(time(NULL));
    clearBoard(board);
  
  
    cout &lt;&lt; "This turn is randomly chosen!" &lt;&lt; endl;
    p = random(2) + 1;
    cout &lt;&lt; "The first move goes to Player: " &lt;&lt; p &lt;&lt; endl;
    turn = p;
  
    do {
     if(p== 2 &amp;&amp; 2 == turn)
    {
      drawBoard(board);
      move = getPlayerMove(turn);
    }
    else
      move = makeRandMove(turn);
    } while(!isMoveValid(board, move));
      board[move] = turn;
  
   while(!isWin(board, move)){
   drawBoard(board);
    if(2 == turn)
      turn = 1;
    else
      turn = 2;
    do {
     if(2 == turn)
        move = getPlayerMove(turn);
    else
      move = makeRandMove(turn);
    } while(!isMoveValid(board, move));
     board[move] = turn;
    }
    drawBoard(board);
    cout &lt;&lt; "Player " &lt;&lt; turn &lt;&lt; " wins." &lt;&lt; endl;
    return 0;
    }
  </code></pre>
  
  <p>And this is the functions.cpp file that has the functions that I was talking about above.</p>
  
  <pre><code>#include &lt;iostream&gt;
  #include&lt;cstdlib&gt;
  using namespace std;
  
  
  const int board_size = 9;
  
  void clearBoard(int board[])
  {
   int i;
    for(i = 0; i &lt; board_size; i++) {
    board[i] = -i - 1;
   }
   }
  
   int random(int x)
   {
   return rand() % x;
   }
  
   void drawBoard(int board[])
   {
   int i, j;
   int n = 0;
   for(i = 0; i &lt;= 6; i = i+3) {
    for(j = 0; j &lt; 3; ++j) {
      if(board[i + j] == 2)
   cout &lt;&lt; "|X|";
    else if(board[i + j] == 1)
  cout &lt;&lt; "|O|";
    else
  cout &lt;&lt; "|" &lt;&lt; n &lt;&lt; "|";
  n = n+1;
  }
  cout &lt;&lt; endl;
   }
   }
  
  
  int getPlayerMove(int player)
  {
  int move;
  cout &lt;&lt; "Player " &lt;&lt; player &lt;&lt; " enter move: ";
  cin &gt;&gt; move;
  return move;
  }
  
  
  int makeRandMove(int player)
  {
  cout &lt;&lt; "Computer (player " &lt;&lt; player &lt;&lt; ") moving." &lt;&lt; endl;
  return rand() % board_size;
  }
  
  
   bool isMoveValid(int board[], int move)
   {
    if(board[move] &lt; 0)
    return true;
    return false;
    }
  
  
    bool isWin(int board[], int move)
    {
    if((board[0] == board[1] &amp;&amp; board[0] == board[2]) ||
   (board[3] == board[4] &amp;&amp; board[3] == board[5]) ||
   (board[6] == board[7] &amp;&amp; board[6] == board[8]) ||
   (board[0] == board[3] &amp;&amp; board[0] == board[6]) ||
   (board[1] == board[4] &amp;&amp; board[1] == board[7]) ||
   (board[2] == board[5] &amp;&amp; board[2] == board[8]) ||
   (board[0] == board[4] &amp;&amp; board[0] == board[8]) ||
   (board[2] == board[4] &amp;&amp; board[2] == board[6]))
    return true;
    return false;
   }
  </code></pre>
  
  <p>And here is my define.h header file has all the protypes...</p>
  
  <pre><code>#ifndef formula
  #define formula
  
  const int board_size = 9;
  int random(int x);
  void clearBoard(int board[]);
  void drawBoard(int board[]);
  int getPlayerMove(int player);
  int makeRandMove(int player);
  bool isMoveValid(int boardp[], int move);
  bool isWin(int board[], int move);
  
  #endif
  </code></pre>