I have this code for a tic tac toe game, everything works fine, but only two player can play, I`d like to make it so the user can enter the number of players, and if there`s only one player, the user can play agains the computer, how do I get the computer to fill in a box after each time the user plays?
this is the code I have right now.
// Laelzio Mosca
// 12/09/09
// Tic Tac Toe game
#include <iostream>
#include <iomanip>
#include <string>
using std::cout;
using std::cin;
using std::endl;
using std::string;
using std::setw;
//global variables
const int row = 3; //size of array
const int column = 3; // size of array
string BLANK = " ";
string ticTac[row][column];
static int nspots = 9; // number of possible spots
void ticTacToe(const string ticTacToe[][column]); //Function prototypes
void player1(string ticTacToe[][column]);
void player2(string ticTac[][column]);
void checkWinner(string ticTac[][column]);
// program execution begins
int main()
{
string ticTac[row][column] = {{BLANK,BLANK,BLANK}, {BLANK,BLANK,BLANK}, {BLANK,BLANK,BLANK}}; //number display for rows and columns
cout << "Welcome to the tic tac toe game "<<endl << endl; // welcome message
ticTacToe( ticTac ); // function call
while (nspots > 0)
{
player1(ticTac);
checkWinner(ticTac);
if (nspots < 0) break;
if (nspots == 0) {
cout << "DRAW" << endl;
break;
}
player2(ticTac);
checkWinner(ticTac);
// the following should never happen
if (nspots == 0) {
cout << "DRAW" << endl;
break;
}
// cout << "nspots: " << nspots << endl;
}
cout << endl;
fflush(stdin); // clear the buffer, and wait to exit.
cout << "\nPress enter to exit " << endl;
getc(stdin);
return 0;
}
//Function ticTacToe
void ticTacToe(const string ticTacToe[][column])
{
cout << setw(5)<<"|"<<setw(5)<< "|" <<endl;
//for loop-continuation condition and increment
for (int i = 0; i < row; i ++)
{
for (int j = 0; j < column; j++)
//check to output '|' only in between the numbers, not at the end
if ( j < column - 1)
{
cout << setw(3)<< ticTacToe[i][j] << setw(2)<<"|";
}
else
cout << setw(3)<<ticTacToe[i][j];
cout << endl;
// check to make sure the horizontal line in only printed in between
// the 3 rows, not on the bottom fo the picture
if ( i < row -1)
{
cout << "____|____|____"<<endl;
cout << setw(5) << "|" << setw(5) << "|" << endl;
}
else
cout << setw(5) << "|" << setw(5) << "|" << endl;
}
}
void player1(string ticTac[][column])
{
int x = 0;
int y = 0;
string p1 = "X";
cout << "Player one can enter your coordinates for X." << endl;
cin >> x >> y ;
x--, y--;
while (x >= row || y >= column)
{
cout << "Invalit entry, try a number from 1 to 3.";
cin >> x >> y;
x--, y--;
}
if (ticTac[x][y] != BLANK)
{
cout << "That spot is taken." << "\nEnter different coordinates for 'X'." << endl;
player1(ticTac);
}
else {
ticTac[x][y] = p1;
nspots--;
}
ticTacToe( ticTac );
}
void player2(string ticTac[][column])
{
int x = 0;
int y = 0;
string p2 = "O";
cout << "Player two can enter you coordinates for 'O'. ";
cin >> x >> y;
x--,y--;
while (x >= row || y >= column)
{
cout << "Invalit entry, try a number from 1 to 3.";
cin >> x >> y;
x--, y--;
}
if (ticTac[x][y] != BLANK)
{
cout << "That spot is taken." << "\nEnter different coordinates for 'O'." << endl;
player2(ticTac);
}
else {
ticTac[x][y] = p2;
nspots--;
}
ticTacToe( ticTac );
}
void checkWinner(string ticTac[][column])
{
// for (int i = 0; i < row; i++)
// {
// for (int j = 0; j < column; j++)
// {
//check columns for winner // column 1
if ((ticTac[0][0] == ticTac[1][0]) && (ticTac[1][0] == ticTac[2][0]) && (ticTac[0][0] != BLANK))
{
cout << "YOU ARE THE WINNER!";
nspots = -1;
} // column 2
else if (ticTac[0][1] == ticTac[1][1] && ticTac[1][1] == ticTac[2][1] && (ticTac[0][1] != BLANK))
{
cout << "YOU ARE THE WINNER!";
nspots = -1;
} // column 3
else if (ticTac[0][2] == ticTac[1][2] && ticTac[1][2] == ticTac[2][2] && (ticTac[0][2] != BLANK))
{
cout << "YOU ARE THE WINNER!";
nspots = -1;
}
//check for winner diagnally
else if (ticTac[2][0] == ticTac[1][1] && ticTac[1][1] == ticTac[0][2] && (ticTac[2][0] != BLANK))
{
cout << "YOU ARE THE WINNER!";
nspots = -1;
}
else if (ticTac[0][0] == ticTac[1][1] && ticTac[1][1] == ticTac[2][2] && (ticTac[0][0] != BLANK))
{
cout << "YOU ARE THE WINNER!";
nspots = -1;
}
//check rows for winner // row 1
else if (ticTac[0][0] == ticTac[0][1] && ticTac[0][1] == ticTac[0][2] && (ticTac[0][0] != BLANK))
{
cout << "YOU ARE THE WINNER!";
nspots = -1;
} // row 2
else if (ticTac[1][0] == ticTac[1][1] && ticTac[1][1] == ticTac[1][2] && (ticTac[1][0] != BLANK))
{
cout << "YOU ARE THE WINNER!";
nspots = -1;
} // row 3
else if (ticTac[2][0] == ticTac[2][1] && ticTac[2][1] == ticTac[2][2] && (ticTac[2][0] != BLANK))
{
cout << "YOU ARE THE WINNER!";
nspots = -1;
}
// }
// }
}
thanks