Help programming code problem TIC TAC TOE

Hello i was improvinsing a person's c++ program to make it have a single player option and a save function but after created the program it has some error that didnt show up in the compilers that i used.

When i run the 1 player mode it crashes in the middle of the game i think the error has to do whit the random function. Also when it saves into an output text file, it doesent save the date or the name of the player. Please tell me the solution to these problems thank you.(please keep it simple)

Code:









#include <iostream>
#include <fstream>
#include <time.h>
#include <cstdlib>
using namespace std;

char board[9] = { '1', '2', '3', '4', '5', '6', '7', '8', '9'};
char answere = 'x';
string P1 = " ";
string P2 = " ";
string Date = " ";
int P1W = 0;
int P2W = 0;
int CW = 0;
int D = 0;
int tempint = 0;
int Pnum = 0;
void drawBoard();
bool Check();
void movePlayer(bool);
void movePlayer2(bool);
void movecomputer(bool);
bool possibleMove(int);
void Twoplayer();
void Oneplayer();
void savegame();
void random();

int main()
{
cout<<"How many players are in this game[1-2]: ";
cin>>Pnum;
cout<<endl;
if(Pnum<2)
{
cout<<"Player one please enter your name: ";
cin>>P1;
cin.get();
cout<<endl;
cout<<"Please enter today's date: ";
cin>>Date;
cin.get();
cout<<endl;
}
if(Pnum>=2)
{
cout<<"Player one please enter your name: ";
cin>>P1;
cin.get();
cout<<endl;
cout<<"Player Two please enter your name: ";
cin>>P2;
cin.get();
cout<<endl;
cout<<"Please enter today's date: ";
cin>>Date;
cin.get();
cout<<endl;
}

if(Pnum>=2)
{
answere = 'x';
P1 = " ";
P2 = " ";
Date = "";
P1W = 0;
P2W = 0;
CW = 0;
D = 0;
tempint = 0;
Pnum = 0;
board[0]='1';
board[1]='2';
board[2]='3';
board[3]='4';
board[4]='5';
board[5]='6';
board[6]='7';
board[7]='8';
board[8]='9';
Twoplayer();
}
else
{
answere = 'x';
P1 = " ";
P2 = " ";
Date = "";
P1W = 0;
P2W = 0;
CW = 0;
D = 0;
tempint = 0;
Pnum = 0;
board[0]='1';
board[1]='2';
board[2]='3';
board[3]='4';
board[4]='5';
board[5]='6';
board[6]='7';
board[7]='8';
board[8]='9';
Oneplayer();
}

}
void drawBoard()
{
cout << "\n " << board[0] << " | " << board[1] << " | " << board[2] << endl
<< " ---------" << endl
<< " " << board[3] << " | " << board[4] << " | " << board[5] << endl
<< " ---------" << endl
<< " " << board[6] << " | " << board[7] << " | " << board[8] << endl;
}

bool Check()
{
if(board[0] == board[1] && board[2] == board[0] )
return true;

else if(board[3] == board[4] && board[5] == board[3])
return true;

else if(board[6] == board[7] && board[8] == board[6])
return true;

else if(board[0] == board[3] && board[6] == board[0])
return true;

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

else if(board[2] == board[5] && board[8] == board[2])
return true;

else if(board[0] == board[4] && board[8] == board[0])
return true;

else if(board[2] == board[4] && board[6] == board[2])
return true;
else
return false;
}

void movePlayer(bool whichPlayer)
{
int place;
cout << "\n Player one, make your move: ";
cin >> place;
if(possibleMove(place))
{
if(whichPlayer == true)
board[place-1] = 'X';
else
board[place-1] = 'O';
}
else
movePlayer(whichPlayer);
drawBoard();
}

void movePlayer2(bool whichPlayer)
{
int place;
cout << "\n Player two, make your move: ";
cin >> place;
if(possibleMove(place))
{
if(whichPlayer == true)
board[place-1] = 'X';
else
board[place-1] = 'O';
}
else
movePlayer2(whichPlayer);
drawBoard();
}

void movecomputer(bool whichPlayer)
{
int place;
random();
place = tempint;
if(possibleMove(place))
{
if(whichPlayer == true)
board[place-1] = 'X';
else
board[place-1] = 'O';
}
else
movePlayer2(whichPlayer);
drawBoard();
}


bool possibleMove(int place)
{
if(board[place-1] == 'X' || board[place-1] == 'O')
return false;
else
return true;
}

void Oneplayer()
{
bool player = false; // true = X , false = O
int i= 0;

drawBoard();

while(!Check())
if ( i==0 || i==2 || i==4 || i==6 || i==8 )
{
i++;
{
if(player == true)
player = false;
else
player = true;
movePlayer(player);
}
}
else if ( i==1 || i == 3 || i==5 || i==7 )
{
i++;
{
if(player == true)
player = false;
else
player = true;
movecomputer(player);
}
}
else
{
cout << " It's a TIE !!! " << endl;
cin.get();
D = D+1;
savegame();
}


if(player == true)
{
cout << "Player one WINS !!!" << endl;
P1W = P1W+1;
savegame();
}
else
{
cout << "Computer WINS !!!" << endl;
CW = CW+1;
savegame();
}
}

void Twoplayer()
{
bool player = false; // true = X , false = O
int i= 0;

drawBoard();

while(!Check())
if ( i==0 || i==2 || i==4 || i==6 || i==8 )
{
i++;
{
if(player == true)
player = false;
else
player = true;
movePlayer(player);
}
}
else if ( i==1 || i == 3 || i==5 || i==7 )
{
i++;
{
if(player == true)
player = false;
else
player = true;
movePlayer2(player);
}
}
else
{
cout << " It's a TIE !!! " << endl;
cin.get();
D = D+1;
savegame();
}


if(player == true)
{
cout << "Player one WINS !!!" << endl;
P1W = P1W+1;
savegame();
}
else
{
cout << "Player two WINS !!!" << endl;
P2W = P2W+1;
savegame();
}
}

void random()
{
do
{
srand(time(0));
tempint = ((rand()%9)+1);
}while(board[tempint]='O'or'X');
}

void savegame()
{
cout<<"Do you want to save the game?[Y or N]"<<endl;
cin>>answere;
if(answere = 'Y' || 'y')
{
if(Pnum<2)
{
ofstream savegame;
savegame.open("save.txt");
savegame << "Player one Vs Computer"<< endl;
savegame << "Date: " << Date << endl;
savegame << "Player one's name: " << P1 << endl;
savegame << "Player one Wins: " << P1W << endl;
savegame << "Computer Wins:" << CW << endl;
savegame.close();
}
if(Pnum>=2)
{
ofstream savegame;
savegame.open("save.txt");
savegame << "Player one Vs Player two"<< endl;
savegame << "Date: " << Date << endl;
savegame << "Player one's name: " << P1 << endl;
savegame << "Player Two's name: " << P2 << endl;
savegame << "Player one Wins: " << P1W << endl;
savegame << "Player Two Wins: " << P2W << endl;
savegame.close();
}
}
if(answere = 'N' || 'n')
{
main();
}
}
Topic archived. No new replies allowed.