I'm attempting to make a Tic-Tac-Toe against the computer program, I'm pretty much done, but i can't figure out how to stop the computer from making the exact same move as the human does.
Thank You.

// Include the libraries
#include <iostream>
#include <string>

// Use standerd namespaces.
using namespace std;

// Declare Global variables
char Board[9];

// Declare functions
void showBoard ();
bool moveIsValid (int m );
int whoWon (); // Returns 0 if nobody won, 1 in player 1 has won, and 2 if player 2 has won
void main ( )
{

// Declare the local variables
	string Player_1_Name;
	string 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 players wants to move
	int Total_Moves = 0;
	
// 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 << " Playing against Computer" << endl;
	


	while (whoWon () == 0 && Total_Moves < 9)
	{


	// Do this until the move is valid
	do
	{


	// Show the board
		showBoard ();
		
		// Show which player to move.
		if (Whose_Turn == 1)
		{
			cout << Player_1_Name << " Its your turn." << endl;	
		}
		else
		{
			cout << Computer << " Its the Computers turn." << endl;	
			
			
		}
		// Get the move
		if (Whose_Turn == 1)
		{
		cout << " Enter the number of the spot where you would like to move" << endl;
		cin >> Move;
		}
		else (Whose_Turn == 2);
		{
			cout << "Computer Moves" << endl;
			int move = rand () % 8;
			break;
		}


	} 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[Move] = 'o';
		Whose_Turn = 1;
		}
	}
	}
// Show the board
	showBoard ( );


	if (whoWon () == 1)
	{
		cout << Player_1_Name << "Has won the game!" << endl;
	}
	else if (whoWon () == 2)
	{
		cout << Computer << "Has won the game!" << endl;
	}
	else
	{
	cout << "It's a tie!" << endl;
	}

	system ("PAUSE");
}


void showBoard( )

 {
	 cout << Board[0] << " | " << Board[1] << " | " << Board[2] << " | " <<endl;
	 cout << "--+---+--" << endl;
	 cout << Board[3] << " | " << Board[4] << " | " << Board[5] << " | " <<endl;
	 cout << "--+---+--" << endl;
	 cout << Board[6] << " | " << Board[7] << " | " << Board[8] << " | " <<endl;
 
 }

bool moveIsValid (int m )
{
	if (Board[m] != 'x' && Board[m] != 'o') 
   { 
	return true; 

   }
	else 
   { 
    return false; 
   } 


	}
int whoWon ( )
{
	if (Board[0] == Board[1] && Board[1] == Board[2])
	{
		if (Board[0] == 'x')
		{
			return 1;
		}
		else
		{
			return 2;
		}
	}
	if (Board[3] == Board[4] && Board[4] == Board[5])
	{
		if (Board[3] == 'x')
		{
			return 1;
		}
		else
		{
			return 2;
		}
	}
	if (Board[6] == Board[7] && Board[7] == Board[8])
	{
		if (Board[6] == 'x')
		{
			return 1;
		}
		else
		{
			return 2;
		}
	}
	if (Board[0] == Board[3] && Board[3] == Board[6])
	{
		if (Board[0] == 'x')
		{
			return 1;
		}
		else
		{
			return 2;
		}
	}
	if (Board[1] == Board[4] && Board[4] == Board[7])
	{
		if (Board[1] == 'x')
		{
			return 1;
		}
		else
		{
			return 2;
		}
	}
	if (Board[2] == Board[5] && Board[5] == Board[8])
	{
		if (Board[2] == 'x')
		{
			return 1;
		}
		else
		{
			return 2;
		}
	}
	if (Board[0] == Board[4] && Board[4] == Board[8])
	{
		if (Board[0] == 'x')
		{
			return 1;
		}
		else
		{
			return 2;
		}
	}
	if (Board[2] == Board[4] && Board[4] == Board[6])
	{
		if (Board[2] == 'x')
		{
			return 1;
		}
		else
		{
			return 2;
		}
	}
	return 0;
}

Recommended Answers

All 11 Replies

The error is around lines 70 and 75. The user input changes the value of "Move" and the computer random value writes to the value of "move". Notice the difference?
Now look at lines 88 to 98. This is where you execute the moves. They both use "Move", which will always be the user input. Change line 75 to:

Move = rand () % 8;

Also:
http://www.cplusplus.com/reference/clibrary/cstdlib/rand/
Here is a reference to rand.
Have a look through.
You should reseed the random value:

#include <time.h>
//...............
//Between current lines 74 and 75
srand (time(NULL));

This should hopefully sort it :)

The computer puts different answers now, but it prints endlessly. How do I get it to stop?

Please upvote my post that helped you. Thanks.
Which part is it that gets printer constantly?
PS. I'll be back later I'm going to the fish and chip shop :D

Please upvote my post that helped you. Thanks.
Which part is it that gets printer constantly?
PS. I'll be back later I'm going to the fish and chip shop :D

The computers answer is posting endlessly. I tried putting a break in there(thats the only code that I knew that might have worked) and it stopped it. but then the computer wouldn't print any more answers.

Which line is the print statement on? Please repost the code.

I believe it's 72-76.

// Include the libraries
#include <iostream>
#include <string>
#include <time.h>
// Use standerd namespaces.
using namespace std;

// Declare Global variables
char Board[9];

// Declare functions
void showBoard ();
bool moveIsValid (int m );
int whoWon (); // Returns 0 if nobody won, 1 in player 1 has won, and 2 if player 2 has won
void main ( )
{

// Declare the local variables
	string Player_1_Name;
	string 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 players wants to move
	int Total_Moves = 0;
	int move = rand () % 8;
// 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 << " Playing against Computer" << endl;
	


	while (whoWon () == 0 && Total_Moves < 9)
	{


	// Do this until the move is valid
	do
	{


	// Show the board
		showBoard ();
		
		// Show which player to move.
		if (Whose_Turn == 1)
		{
			cout << Player_1_Name << " Its your turn." << endl;	
		}
		else
		{
			cout << Computer << " Its the Computers turn." << endl;	
			
			
		}
		// Get the move
		if (Whose_Turn == 1)
		{
		cout << " Enter the number of the spot where you would like to move" << endl;
		cin >> Move;
		}
		else (Whose_Turn == 2);
		{
			cout << "Computer Moves" << endl;
			srand (time(NULL));
			int move = rand () % 8;
			
			
		}



	} 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[move] = 'o';
		Whose_Turn = 1;
		
		}
	}
	}
// Show the board
	showBoard ( );


	if (whoWon () == 1)
	{
		cout << Player_1_Name << " Has won the game!" << endl;
	}
	else if (whoWon () == 2)
	{
		cout << Computer << " The Computer has won the game!" << endl;
	}
	else
	{
	cout << "It's a tie!" << endl;
	}

	system ("PAUSE");
}


void showBoard( )

 {
	 cout << Board[0] << " | " << Board[1] << " | " << Board[2] << " | " <<endl;
	 cout << "--+---+--" << endl;
	 cout << Board[3] << " | " << Board[4] << " | " << Board[5] << " | " <<endl;
	 cout << "--+---+--" << endl;
	 cout << Board[6] << " | " << Board[7] << " | " << Board[8] << " | " <<endl;
 
 }

bool moveIsValid (int m )
{
	if (Board[m] != 'x' && Board[m] != 'o') 
   { 
	return true; 

   }
	else 
   { 
    return false; 
   } 


	}
int whoWon ( )
{
	if (Board[0] == Board[1] && Board[1] == Board[2])
	{
		if (Board[0] == 'x')
		{
			return 1;
		}
		else
		{
			return 2;
		}
	}
	if (Board[3] == Board[4] && Board[4] == Board[5])
	{
		if (Board[3] == 'x')
		{
			return 1;
		}
		else
		{
			return 2;
		}
	}
	if (Board[6] == Board[7] && Board[7] == Board[8])
	{
		if (Board[6] == 'x')
		{
			return 1;
		}
		else
		{
			return 2;
		}
	}
	if (Board[0] == Board[3] && Board[3] == Board[6])
	{
		if (Board[0] == 'x')
		{
			return 1;
		}
		else
		{
			return 2;
		}
	}
	if (Board[1] == Board[4] && Board[4] == Board[7])
	{
		if (Board[1] == 'x')
		{
			return 1;
		}
		else
		{
			return 2;
		}
	}
	if (Board[2] == Board[5] && Board[5] == Board[8])
	{
		if (Board[2] == 'x')
		{
			return 1;
		}
		else
		{
			return 2;
		}
	}
	if (Board[0] == Board[4] && Board[4] == Board[8])
	{
		if (Board[0] == 'x')
		{
			return 1;
		}
		else
		{
			return 2;
		}
	}
	if (Board[2] == Board[4] && Board[4] == Board[6])
	{
		if (Board[2] == 'x')
		{
			return 1;
		}
		else
		{
			return 2;
		}
	}
	return 0;
}

So it keeps printing "Computer Moves"?
It would appear that your block of "if" statements are constantly going to the "else" option. Try this:
At line 72:
Change

else (Whose_Turn == 2);

To

else if (Whose_Turn == 2);

However, if that is the case, it will probably keep reverting to the "else if" and we will have another problem.

It's printing where the computer moves endlessly I think its at that line, but im not sure, just started learning about C++. Also the else if didn't work :\

Could you copy and paste the output (just one line of the repeating bit)? Even if the "else if" didn't work, it's still more conventional and would be better if you had more if statements.

Its printing

cout << Computer << " Its the Computers turn." << endl;
cout << "Computer Moves" << endl;

And its also printing the board itself endlessly.

cout << Board[0] << " | " << Board[1] << " | " << Board[2] << " | " <<endl;
	 cout << "--+---+--" << endl;
	 cout << Board[3] << " | " << Board[4] << " | " << Board[5] << " | " <<endl;
	 cout << "--+---+--" << endl;
	 cout << Board[6] << " | " << Board[7] << " | " << Board[8] << " | " <<endl;

So you don't get any chance to enter input or anything in between the prints, and it's just wildly looping? It appears that your main game loop will always be true. You should use couts to check the values of variables and the expected output as you go. Use "system("pause")" [if your on windows] to pause the program after each loop and see what has happened.

You also appear to have something funny going on at line 83 (in your latest full code). You have

while (moveIsValid (Move) != true);

and then all the bits for changing who's turn it is. This would suggest that if the move is not valid, you change turn, therefore you will keep changing turn as the input is not valid.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.