I am writing a tic tac toe game for 1 player vs computer. While placing the random numbers from generating from RandomNumber function which I have created. I know the issue like some time RandomNumber is generating the number which player is giving as spot. So somethime the code is working and showing me winner or looser some time not. Can you guys please help me in completing this project?? any-help will be appreciated. Thanks
//global variable
//variable for array for board
char board[9];
//varible for entering number of spot
int spot;
string player_name1,player_name2,player_name3;
//prototype of functions
void display();
void fill_board();
int RandomNumber();
int winning_condition();
//tic toe game
int main(){
//variables to enter the player number and names
int num_player;
char spot;
//display at the start of the screen
cout<<"*** Welcome to world of Tic Tac Toe game ***"<<endl;
cout<<" *** This is a computer vs human game ***"<<endl;
cout<<" *** Enter 1 for 1 player and 2 for players ***"<<endl;
cin>>num_player;
//loop to fill the number in the 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';
//condition for taking numbers to enter from the screen
if(num_player==2){
cout<<"Enter the name of the first player"<<endl;
cin>>player_name2;
cout<<"Enter the name of the first player"<<endl;
cin>>player_name3;
cout<<"Sorry "<<player_name2<<" and "<<player_name3<<" we can't play with 2 players"<<endl;
}
if(num_player==1){
cout<<"Player1: Please enter your name you want to display on screen"<<endl;
cin>>player_name1;
cout<<endl;
}
//calling display function
display();
fill_board();
system("pause");
return 0;
}
//function decalartion
//function to fill the board with X and O
void fill_board(){
int no_choice;
label1:
cout<<player_name1<<":: Enter the number of the spot where you'd like to place the number"<<endl;
cin>>spot;
cout<<endl;
switch(spot){
case 0:
{
board[0]='X';
//board[1] = '1'; board[2] ='2';board[3] = '3';board[4] = '4'; board[5] ='5'; board[6] ='6'; board[7] ='7';board[8] = '8';
display();
cout<<endl;
break;
}
case 1:
{
board[1]='X';
//board[0] = '0'; board[2] ='2';board[3] = '3';board[4] = '4'; board[5] ='5'; board[6] ='6'; board[7] ='7';board[8] = '8';
display();
cout<<endl;
break;
}
case 2:
{
board[2]='X';
//board[0] = '0'; board[1] ='1';board[3] = '3';board[4] = '4'; board[5] ='5'; board[6] ='6'; board[7] ='7';board[8] = '8';
display();
cout<<endl;
break;
}
case 3:
{
board[3]='X';
//board[0] = '0'; board[1] ='1';board[2] = '2';board[4] = '4'; board[5] ='5'; board[6] ='6'; board[7] ='7';board[8] = '8';
display();
cout<<endl;
break;
}
case 4:
{
board[4]='X';
//board[0] = '0'; board[1] ='1';board[3] = '3';board[2] = '2'; board[5] ='5'; board[6] ='6'; board[7] ='7';board[8] = '8';
display();
cout<<endl;
break;
}
case 5:
{
board[5]='X';
//board[0] = '0'; board[1] ='1';board[3] = '3';board[4] = '4'; board[2] ='2'; board[6] ='6'; board[7] ='7';board[8] = '8';
display();
cout<<endl;
break;
}
case 6:
{
board[6]='X';
//board[0] = '0'; board[1] ='1';board[3] = '3';board[4] = '4'; board[5] ='5'; board[2] ='2'; board[7] ='7';board[8] = '8';
display();
cout<<endl;
break;
}
case 7:
{
board[7]='X';
//board[0] = '0'; board[1] ='1';board[3] = '3';board[4] = '4'; board[5] ='5'; board[6] ='6'; board[1] ='1';board[8] = '8';
display();
cout<<endl;
break;
}
case 8:
{
board[8]='X';
//board[0] = '0'; board[1] ='1';board[3] = '3';board[4] = '4'; board[5] ='5'; board[6] ='6'; board[7] ='7';board[1] = '1';
display();
cout<<endl;
break;
}
}
cout<<"Turn for the computer to put O:"<<endl;
cout<<endl;
if(number==1){
cout<<player_name1<<": Congratulations!!! You won the game"<<endl;
}
else
if(number==2){
cout<<": Genius Computer has won the game"<<endl;
}
else
if(number==0)
{
cout<<"It's a tie!!!"<<endl;
}
else{
goto label1;
}
}
}
//function to generate random number
int RandomNumber()
{
//function for winning the game
// there are 8 conditions
int winning_condition(){
//1st condition
if(board[0]==board[1] && board[1]==board[2]){
if((board[0]=='X') && (board[1]=='X') && (board[2]=='X')){
return 1;