Search code examples
c++algorithmtic-tac-toe

random number picker between 2 numbers


I am working on a Tic-Tac-Toe game using C++ and I was wondering if it is possible to randomize a number but it could only choose either 2 or 5.

I made random start between user and the computer in order to make a fair start with these blocks of codes:

 srand(time(NULL));
 cout << "The First Person to go is randomly chosen!" << endl;
 FirsttoGo = (rand()%2) + 1;
 cout << "The first move goes to Player: " << FirsttoGo << endl;
 turn = FirsttoGo;

The feature that I would like to add is whoever starts the game will be able to pick either X or O but if the computer starts first, the computer should be able to pick between X or O. I also created constant values for X and O

static const int O = 5;
static const int X = 2;

Is there anyway to do this?


Solution

  • (rand() > RAND_MAX/2) ? 2 : 5;