//Array used to store data on the board
char square[10] = { 'o','1','2','3','4','5','6','7','8','9' };
int systemcheck();
void board();
int main()
{
//Defining variables
int player = 1, i, choice;
//playermark is used for switching the slot number with an X or O
char playermark;
do
{
board();
player = (player % 2) ? 1 : 2;
cout << "Player " << player << ", enter a number: ";
cin >> choice;
square[9] = playermark;
else
{
//if the input does not equal any integer listed in the strings, the if statement will register the character as invalid
cout << "Invalid character || somebody has already played that slot!";
player--;
cin.ignore();
cin.get();
//after move is performed
}
i = systemcheck();
//After move system is checking for a win
player++;
//Game win/tie loop
} while (i == -1);
board();
if (i == 1)
cout << "==>\aPlayer " << --player << " win ";
else
cout << "==>\aPlayer 1 & Player 2 has tied";
cin.ignore();
cin.get();
return 0;
}
//Board drawn with console outputs, will return no value
void board()
{
cout << "\n\n\tTICTACTOE\n\n";
cout << "Player 1 - X - Player 2 - O -" << endl << endl;
cout << endl;
//Defines where a specific slot is placed increasing in a row
cout << " | | " << endl;
cout << " " << square[1] << " | " << square[2] << " | " << square[3] << endl;
//Will check if a given player has a winning board
int systemcheck()
{
if (square[1] == square[2] && square[2] == square[3])
//Check win verifying if any given slot has a matching slot, otherwise performing three slots in a row or column
return 1;
else if (square[4] == square[5] && square[5] == square[6])
set up a table of the locations and when either side uses a location, mark it used.
eg
bool table[9] = {false};
then you can use <random> to get a value from 0-8 in a loop until you get an empty location. This is woefully inefficient when there is only 1 slot left, but computers are fast enough that it still only takes a few ms to do it this way. Its slightly harder to do it without the brute force; you can generate a unique random list (permutation really) of the values 0-8 and just iterate that, if a location is used, go to next one.
Another way is, determining how many fields are empty and randomizing over that number. Lets' assume the random number is named 'r', then you could use the 'r'-th free field.