I made a simple ticktacktoe game for assignment. Everything works except when the user want's to play again, the game starts as normal with the previous board, I've tried different ways to clear the board but none seem to work. I've tried to re input the characters in the again function but they do not carry over. Thank you.
#include <iostream>
using namespace std;
//Board variables
char s1('1');
char s2('2');
char s3('3');
char s4('4');
char s5('5');
char s6('6');
char s7('7');
char s8('8');
char s9('9');
int checkifwin();
int again = 0;
void createboard();
void playagain();
void endpro();
void createboard()
{
//This function draws the board
system("clear");
cout << "-----------------" << endl;
cout << " TIC TAC TOE v0.2 " << endl;
cout << "-----------------" << endl;
cout << " P1(X) - P2(O)" << endl;
cout << "-----------------" << endl;
cout << "Choices are numbered" << endl;
cout << "1 to 9, from left to right" << endl;
cout << "Press a # to place your marker" << endl;
cout << "____________________" << endl;
cout << "| || || |" << endl;
cout << "| " << s1 << " || " << s2 << " || " << s3 << " |" << endl;
cout << "|_____||_____||_____|" << endl;
cout << "| || || |" << endl;
cout << "| " << s4 << " || " << s5 << " || " << s6 << " |" << endl;
cout << "|_____||_____||_____|" << endl;
cout << "| || || |" << endl;
cout << "| " << s7 << " || " << s8 << " || " << s9 << " |" << endl;
cout << "|_____||_____||_____|" << endl;
}
int main()
{
int player = 1;
int i;
int choice;
char mark = ' ';
do
{
createboard();
cout << "Player # " << player << ", please enter a number: ";
cin >> choice;
if(player == 1)
{
mark = 'x';
player++;
}
else if(player == 2)
{
mark = 'o';
player--;
}
if (choice == 1 && s1 == '1')
s1 = mark;
else if (choice == 2 && s2 == '2')
s2 = mark;
else if (choice == 3 && s3 == '3')
s3 = mark;
else if (choice == 4 && s4 == '4')
s4 = mark;
else if (choice == 5 && s5 == '5')
s5 = mark;
else if (choice == 6 && s6 == '6')
s6 = mark;
else if (choice == 7 && s7 == '7')
s7 = mark;
else if (choice == 8 && s8 == '8')
s8 = mark;
else if (choice == 9 && s9 == '9')
s9 = mark;
else
{
cout << "Not a valid move, try again please. ";
cin.ignore();
cin.get();
}
i = checkifwin();
} while (i == -1);
if (i == 1)
if(player = 2)
cout << "Player #1 is the winner!" << endl;
else if(player = 1)
cout << "Player #2 is the winner!" << endl;
else if (i == 0)
cout << "So close! It's a tie!" << endl;
playagain();
}
int checkifwin()
{
//This function returns the game status, 1 is for game over with results,
//-1 is for game in progress, and 0 is for gameover and no result(tie)
if (s1 == s2 && s2 == s3)
return 1;
else if (s1 == s4 && s4 == s7)
return 1;
else if (s1 == s5 && s5 == s9)
return 1;
else if (s2 == s5 && s5 == s8)
return 1;
else if (s3 == s6 && s6 == s9)
return 1;
else if (s3 == s5 && s5 == s7)
return 1;
else if (s4 == s5 && s5 == s6)
return 1;
else if (s7 == s8 && s8 == s9)
return 1;
else if (s1 != '1' && s2 != '2' && s3 != '3' && s4 != '4' && s5 != '5' && s6 != '6' && s7 != '7' && s8 != '8' && s9 != '9')
return 0;
else
return -1;
}
void playagain()
{
//Asks the player if they
//want to play again
cout << "Would you like to play again?" << endl;
cout << "1 = Yes, 2 = No." << endl;
cin >> again;
if(again == 1)
{
s1 == '1';
s2 == '2';
s3 == '3';
s4 == '4';
s5 == '5';
s6 == '6';
s7 == '7';
s8 == '8';
s9 == '9';
main();
}
else if(again == 2)
{
cout << "Press ENTER to confirm..." << endl;
endpro();
}
}
void endpro()
{
//Ends the program
return;
}