How can i have player two choose a random slot in tic tac toe

#include "pch.h"
#include <iostream>
#include <string>
#include <ctime>
using namespace std;

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

playermark = (player == 1) ? 'X' : 'O';

if (choice == 1 && square[1] == '1')

square[1] = playermark;
else if (choice == 2 && square[2] == '2')

square[2] = playermark;
else if (choice == 3 && square[3] == '3')

square[3] = playermark;
else if (choice == 4 && square[4] == '4')

square[4] = playermark;
else if (choice == 5 && square[5] == '5')

square[5] = playermark;
else if (choice == 6 && square[6] == '6')

square[6] = playermark;
else if (choice == 7 && square[7] == '7')

square[7] = playermark;
else if (choice == 8 && square[8] == '8')

square[8] = playermark;
else if (choice == 9 && square[9] == '9')

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;

cout << "_____|_____|_____" << endl;
cout << " | | " << endl;

cout << " " << square[4] << " | " << square[5] << " | " << square[6] << endl;

cout << "_____|_____|_____" << endl;
cout << " | | " << endl;

cout << " " << square[7] << " | " << square[8] << " | " << square[9] << endl;

cout << " | | " << endl << 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])

return 1;
else if (square[7] == square[8] && square[8] == square[9])

return 1;
else if (square[1] == square[4] && square[4] == square[7])

return 1;
else if (square[2] == square[5] && square[5] == square[8])

return 1;
else if (square[3] == square[6] && square[6] == square[9])

return 1;
else if (square[1] == square[5] && square[5] == square[9])

return 1;
else if (square[3] == square[5] && square[5] == square[7])

return 1;
else if (square[1] != '1' && square[2] != '2' && square[3] != '3'
&& square[4] != '4' && square[5] != '5' && square[6] != '6'
&& square[7] != '7' && square[8] != '8' && square[9] != '9')

return 0;
else
return -1;
}


Last edited on
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.
Topic archived. No new replies allowed.