title: Simple TicTacToe. Program is in Infinite Loop
body: <p>For school I am required to make a TicTacToe console program that has the computer choose a random place to set its "o". </p>
  
  <p>I have tried different things (switch case statements, if else statements, checking and rechecking my functions) however,after the human player makes his second move, the computer will not make any moves, which in my program, causes the "Make your move" printed line repeat indefinitely. </p>
  
  <p>I have tried and tried again to solve it, but I am only a beginner (just started this month.) Please help!</p>
  
  <p>Here is my code:</p>
  
  <pre><code> // Tic_Tac_Toe.cpp : Defines the entry point for the console application.
  //
  
  #include "stdafx.h"
  
  
  int _tmain(int argc, _TCHAR* argv[])
  {
      return 0;
  }
  
  // Tic Tac Toe, by Your Name
  
  // Include the libraries
  #include &lt;iostream&gt;
  #include &lt;string&gt;
  #include &lt;ctime&gt;
  
  //Use the standard namespace
  using namespace std;
  
  // Declare global variables
  char Board[9];
  
  // Declare functions
  void showBoard ( );
  bool moveIsValid (int m);
  int whoWon ( ); // Returns 0 if no one has won, 1 if player 1 has won, and 2 if player 2 has won
  
  void main ( )
  {
      // Seed the rand function
      srand(time(NULL));
  
     // Declare local variables
     string Player_1_Name;
     string Player_2_Name = "Computer";
     int Whose_Turn = 1; // 1 means it's player 1's turn, 2 means it's player 2's turn
     int Move; // Stores where the player wants to move
     int totalMoves = 0;
     int aiMove = rand () % 10 - 1;
  
  
     //Assign values to the playing board
     Board[0] = '0';
     Board[1] = '1';
     Board[2] = '2';
     Board[3] = '3';
     Board[4] = '4';
     Board[5] = '5';
     Board[6] = '6';
     Board[7] = '7';
     Board[8] = '8';
  
     // Get player names
     cout &lt;&lt; "Player 1: Please enter your name." &lt;&lt; endl;
     cin &gt;&gt; Player_1_Name;
     cout &lt;&lt; "Computer: I will be playing you today." &lt;&lt; endl;
  
  
     while (whoWon ( ) == 0 &amp;&amp; totalMoves &lt; 9)
     {
  
        // Do this until the player chooses a valid move
        do
        {
  
           // Show the board
           showBoard ( );
  
           // Tell which player to move
          switch (Whose_Turn)
          {
          case(1):
              {
              cout &lt;&lt; Player_1_Name &lt;&lt; " , It's your turn." &lt;&lt; endl;
                  cin &gt;&gt; Move;
                  break;
              }
          case(2):
              {
                  cout &lt;&lt; Player_2_Name &lt;&lt; " , It's your turn." &lt;&lt; endl;
                  if (aiMove == 0)
                  {
                      Move = 0;
                  }
                  else if (aiMove == 1)
                      Move = aiMove;
              }
          }
  
  
        } while (moveIsValid (Move) != true);
  
        // Add a move to the totalMoves
        totalMoves++;
  
        // Change whose turn it is
        switch (Whose_Turn)
        {
        case (1):
           {
              Board[Move] = 'x';
              Whose_Turn = 2;
              break;
           }
        case (2):
           {
              Board[Move] = 'o';
              Whose_Turn = 1;
           }
        }
     }
  
     // Show the board
     showBoard ( );
  
     //Tell the user who won
     if (whoWon ( ) == 1) 
     {
         cout &lt;&lt; Player_1_Name &lt;&lt; " has won." &lt;&lt; endl;
  
     }
  
     else if (whoWon ( ) == 2) 
     {
         cout &lt;&lt; Player_2_Name &lt;&lt; " has won." &lt;&lt; endl;
     }
  
     else 
     {
         cout &lt;&lt;"It's a tie game!" &lt;&lt; endl;
     }
     system("PAUSE");
  }
  
  void showBoard ( )
  {
     cout &lt;&lt; endl;
     cout &lt;&lt; Board[0] &lt;&lt; " | " &lt;&lt; Board[1] &lt;&lt; " | " &lt;&lt; Board[2] &lt;&lt; endl;
     cout &lt;&lt; "--+---+--" &lt;&lt; endl;
     cout &lt;&lt; Board[3] &lt;&lt; " | " &lt;&lt; Board[4] &lt;&lt; " | " &lt;&lt; Board[5] &lt;&lt; endl;
     cout &lt;&lt; "--+---+--" &lt;&lt; endl;
     cout &lt;&lt; Board[6] &lt;&lt; " | " &lt;&lt; Board[7] &lt;&lt; " | " &lt;&lt; Board[8] &lt;&lt; endl;
     cout &lt;&lt; endl;
  }
  
  bool moveIsValid (int m)
  {
     if (Board[m] != 'x' &amp;&amp; Board[m] != 'o')
     {
        return true;
     }
     else
     {
        return false;
     }
  }
  
  int whoWon ( )
  {
     if (Board[0] == Board[1] &amp;&amp; Board[1] == Board[2])
     {
        if (Board[0] == 'x')
        {
           return 1;
        }
        else
        {
           return 2;
        }
     }
      if (Board[3] == Board[4] &amp;&amp; Board[4] == Board[5])
     {
        if (Board[3] == 'x')
        {
           return 1;
        }
        else
        {
           return 2;
        }
     }
       if (Board[6] == Board[7] &amp;&amp; Board[7] == Board[8])
     {
        if (Board[6] == 'x')
        {
           return 1;
        }
        else
        {
           return 2;
        }
     }
        if (Board[0] == Board[3] &amp;&amp; Board[3] == Board[6])
     {
        if (Board[0] == 'x')
        {
           return 1;
        }
        else
        {
           return 2;
        }
     }
         if (Board[1] == Board[4] &amp;&amp; Board[4] == Board[7])
     {
        if (Board[1] == 'x')
        {
           return 1;
        }
        else
        {
           return 2;
        }
     }
          if (Board[2] == Board[5] &amp;&amp; Board[5] == Board[8])
     {
        if (Board[2] == 'x')
        {
           return 1;
        }
        else
        {
           return 2;
        }
     }
           if (Board[0] == Board[4] &amp;&amp; Board[4] == Board[8])
     {
        if (Board[0] == 'x')
        {
           return 1;
        }
        else
        {
           return 2;
        }
     }
            if (Board[2] == Board[4] &amp;&amp; Board[4] == Board[6])
     {
        if (Board[2] == 'x')
        {
           return 1;
        }
        else
        {
           return 2;
        }
     }
     return 0;
  }
  </code></pre>