# Help With Simple C++ Program



## lqcorsa (Sep 23, 2006)

I'm taking a C++ course in school, and the instructor can't actually teach anything useful, so hopefully someone here can help. 

I'm creating a simple tic-tac-toe console program in Visual Basic 2008 Express. The assignment was to convert a 2 player tic-tac-toe game into a single player one, against the computer.

Here's the (important) code:


```
// Include the libraries 
#include <iostream> 
#include <string> 
#include <ctime>
 
//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 ( ) 
{ 
   srand (time (NULL) );

   // Declare local variables 
   string Player_1_Name; 
   string The_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 Total_Moves = 0; 
   int ai = rand ( ) % 9;
    
   //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 << "Player 1: Please enter your name." << endl; 
   cin >> Player_1_Name; 
   //cout << "Player 2: Please enter your name." << endl; 
   //cin >> Player_2_Name; 
 
   while (whoWon ( ) == 0 && Total_Moves < 9) 
   { 
      // Do this until the player chooses a valid move 
      do 
      { 
         // Show the board 
         showBoard ( ); 
 
         // Tell which player to move 
         if (Whose_Turn == 1) 
         { 
            cout << Player_1_Name << ": It's your turn." << endl; 
         } 
         
         // Get the move 
         cout << "Enter the number of the spot where you'd like to move." << endl; 
         cin >> Move; 
      } while (moveIsValid (Move) != true); 
 
      // Add 1 to Total_Moves 
      Total_Moves++; 
 
      // Change whose turn it is 
      switch (Whose_Turn) 
      { 
      case (1): 
         { 
            Board[Move] = 'x'; 
            Whose_Turn = 2; 
            //break; 
		 }
			  
      case (2): 
         { 
            Board[ai] = 'o'; 
            Whose_Turn = 1;
			//break; 
         } 
      } 
   } 
   // Show the board 
   showBoard ( ); 
 
   if (whoWon ( ) == 1) 
   { 
      cout << Player_1_Name << " has won the game!" << endl; 
   } 
   else if (whoWon ( ) == 2) 
   { 
      cout << " The computer has won the game!" << endl; 
   } 
   else 
   { 
      cout << "It's a tie game!" << endl; 
   } 
 
   system ("PAUSE"); 
}
```


I seem to almost have it, but the computer only works for it's first turn. Every other turn after that, the computer stops picking a random number. Can anyone help me pick out the bug? The assignment is past-due :sigh:


----------



## Ninjaboi (Dec 27, 2010)

*Revising, give me a moment...*


----------



## Ninjaboi (Dec 27, 2010)

```
#include <iostream>
#include <ctime>

int main()
{

    int count = 0;

    while(count < 10)
    {
    srand ( time (NULL) );
    int ai = rand ( ) % 9;
    std::cout << ai << std::endl;
    }

}
```
That code above shows an example of using the rand() function using time as a seed for each second. Now, in your code ( correct me if I'm wrong ) but it seems you have your seed as well as your rand() outside of a loop ( excluding main of course ). Therefore, once rand() has been called and the seed has collected a random number based on time ( say 7 is the number ) it proceeds to your other loops. However, when rand() is called again, your not getting another seed due to the loop not re-seeding. You'd have to put your seed in the game's do loop, not the main loop. Try that and see if you get a different seeded number each time. Now, you may also try copying and compiling the code I provided above to give an example of proper seeding.


----------



## lqcorsa (Sep 23, 2006)

Ninjaboi, thanks for letting me take your time.

If I'm reading you right, I need to have my rand function and seed in the do loop of the game instead, in order for it to correctly loop. 

I did this, but that left me with ai being undeclared. 

What I then did was attempted to declare ai as a local variable, and allocated it with [9] since it's being called by an array. (Without allocating it a size of 9, I received Run-Time Check Error 3).

Now I'm getting a build error...

Error	2	error C2107: illegal index, indirection not allowed - Line 84

I'll post my revised code and highlight line 84 in red.

Thanks for helping. I feel like I'm trying to drive blind here... :sad:


```
// Tic Tac Toe
 
// Include the libraries 
#include <iostream> 
#include <string> 
#include <ctime>
 
//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 ( ) 
{ 
   
   // Declare local variables 
   string Player_1_Name; 
   string The_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 Total_Moves = 0; 
   int ai[9];
    
   //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 << "Player 1: Please enter your name." << endl; 
   cin >> Player_1_Name; 
   //cout << "Player 2: Please enter your name." << endl; 
   //cin >> Player_2_Name; 
 
   while (whoWon ( ) == 0 && Total_Moves < 9) 
   { 
      // Do this until the player chooses a valid move 
      do 
      { 
         // Show the board 
         showBoard ( ); 
 
         // Tell which player to move 
         if (Whose_Turn == 1) 
         { 
            cout << Player_1_Name << ": It's your turn." << endl; 
         } 
         
         // Get the move 
         cout << "Enter the number of the spot where you'd like to move." << endl; 
         cin >> Move; 
		    srand (time (NULL) );
			int ai = rand ( ) % 9;
      }     while (moveIsValid (Move) != true); 
 
      // Add 1 to Total_Moves 
      Total_Moves++; 
 
      // Change whose turn it is 
      switch (Whose_Turn) 
      { 
      case (1): 
         { 
            Board[Move] = 'x'; 
            Whose_Turn = 2; 
            //break; 
		 }
			  
      case (2): 
         { 
            [B]Board[ai] = 'o';[/B] 
            Whose_Turn = 1;
			//break; 
         } 
      } 
   } 
   // Show the board 
   showBoard ( ); 
 
   if (whoWon ( ) == 1) 
   { 
      cout << Player_1_Name << " has won the game!" << endl; 
   } 
   else if (whoWon ( ) == 2) 
   { 
      cout << " The computer has won the game!" << endl; 
   } 
   else 
   { 
      cout << "It's a tie game!" << endl; 
   } 
 
   system ("PAUSE"); 
}
```

As before...more irrelevant lines exist below this, and probably aren't of any use to help solve this issue.


----------



## Ninjaboi (Dec 27, 2010)

Oh, we'll I'll have to check this early tomorrow. Right now I'm signing off, but do check back tomorrow, as I'll be replying within 4-7 hours.


----------

