Tic Tac toe game: issue with random number generator function which is generating number as player inputs.

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

//header files decalration
#include<iostream>
#include<iomanip>
#include<string>
#include<cmath>

using namespace std;

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

no_choice=RandomNumber();
if(no_choice==spot){
RandomNumber();
}
else
if(no_choice!=spot){
goto label2;
}

label2:
cout<<"llll "<<spot<<endl;
cout<<"hhhh "<<no_choice<<endl;
if((board[no_choice]!='O') && (board[no_choice]!='X') ){
board[no_choice]='O';
display();
int number=winning_condition();

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()
{

srand (time(0));
return (rand() % (8));

}


//function to display the board
void display(){

cout<<setw(2)<<board[0]<<setw(2)<<"|"<<setw(2)<<board[1]<<setw(2)<<"|"<<setw(2)<<board[2]<<endl;
cout<<"---"<<"+"<<"---"<<"+"<<"---"<<endl;
cout<<setw(2)<<board[3]<<setw(2)<<"|"<<setw(2)<<board[4]<<setw(2)<<"|"<<setw(2)<<board[5]<<endl;
cout<<"---"<<"+"<<"---"<<"+"<<"---"<<endl;
cout<<setw(2)<<board[6]<<setw(2)<<"|"<<setw(2)<<board[7]<<setw(2)<<"|"<<setw(2)<<board[8]<<endl;
cout<<"---"<<"+"<<"---"<<"+"<<"---"<<endl;
cout<<endl;
}

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

}
else
if((board[0]=='O') && (board[1]=='O') && (board[2]=='O')){
return 2;
}

else{
return 0;
}

}
//2nd condition
if(board[3]==board[4] && board[4]==board[5]){
if((board[3]=='X') && (board[4]=='X') && (board[5]=='X')){
return 1;

}
else
if((board[3]=='O')&& (board[4]=='O') && (board[5]=='O')){
return 2;
}
else{
return 0;
}


}
//3rd condition

if(board[6]==board[7] && board[7]==board[8]){

if((board[6]=='X') && (board[7]=='X') && (board[8]=='X')){
return 1;

}
else
if((board[6]=='O') && (board[7]=='O')&&(board[8]=='O')){
return 2;
}
else{
return 0;
}


}

//4th condition

if(board[0]==board[3] && board[3]==board[6]){
if((board[0]=='X')&&(board[3]=='X')&&(board[6]=='X')){
return 1;

}
else
if((board[0]=='O')&&(board[3]=='X')&&(board[6]=='X')){
return 2;
}
else{
return 0;
}



}
//5th condition


if(board[1]==board[4] && board[4]==board[7]){
if((board[1]=='X')&&(board[4]=='X')&&(board[7]=='X')){
return 1;

}
else
if((board[1]=='O')&&(board[4]=='O')&&(board[7]=='O')){
return 2;
}
else{
return 0;
}



}


//6th condition


if(board[2]==board[5] && board[5]==board[8]){

if((board[2]=='X')&&(board[5]=='X')&&(board[8]=='X')){
return 1;

}
else
if((board[2]=='O')&&(board[5]=='O')&&(board[8]=='O')){
return 2;
}
else{
return 0;
}

}


//7th condition

if(board[0]==board[4] && board[4]==board[8]){

if((board[0]=='X')&& (board[4]=='X')&&(board[8]=='X')){
return 1;

}
else
if((board[0]=='O')&& (board[4]=='O')&&(board[8]=='O')){
return 2;
}

else{
return 0;
}


}




//8th condition

if(board[2]==board[4] && board[4]==board[6]){

if((board[2]=='X')&&(board[4]=='X')&&(board[6]=='X')){
return 1;

}
else
if((board[2]=='O')&&(board[4]=='O')&&(board[6]=='O')){
return 2;
}

else{
return 0;
}

}

}
Topic archived. No new replies allowed.