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;
}