I am wanting to provide the player of my game with an option to play the game again once the end screen has appeared or to quit the application(tic-tac-toe game) as alls I currently have is once someone has won the application closes.
while (1)
{
b++;
Input();
Display();
//win function if player has won
if (Win() == 'X')
{
system("cls");
cout << "X has Won!" << endl;
break;
}
elseif (Win() == 'O')
{
system("cls");
cout << "O has Won!" << endl;
break;
}
//if players tie function
elseif (Win() == '/' && b == 9)
{
system("cls");
cout << "It's a Tie!" << endl;
break;
}
TogglePlayer();
}
The easiest way to do this (I think) is to put your hole program (under the line with main()) in a do...while loop. It's just like a while loop but it completes the code 1 time before checking the conditions
Int main ()
do
Your code here....
While (x<5)
This will do your cone one time, than if x is greater than 5 will loop back to "do". Knowing this you should be able to use the do....while loop for replaying your game.
int main()
{
Names();
b = 0;
Display();
//loop that will run until a player has won or tied
while (1)
{
b++;
Input();
Display();
//win function if player has won
if (Win() == 'X')
{
system("cls");
cout << X << " has Won!" << endl;
cout << "Would you like to Play again? or Quit?" << endl;
cout << "'Play' to play again, or 'Quit' to Quit." << endl;
string choice2;
cin >> choice2;
if (choice2 == "Play")
{
/*go to start*/
}
elseif (choice2 == "Quit")
{
return 0;
}
}
elseif (Win() == 'O')
{
system("cls");
cout << O << " has Won!" << endl;
cout << "Would you like to Play again? or Quit?" << endl;
cout << "'Play' to play again, or 'Quit' to Quit." << endl;
string choice2;
cin >> choice2;
if (choice2 == "Play")
{
/*go to start*/
}
elseif (choice2 == "Quit")
{
return 0;
}
}
//if players tie function
elseif (Win() == '/' && b == 9)
{
system("cls");
cout << "It's a Tie!" << endl;
cout << "Would you like to Play again? or Quit?" << endl;
cout << "'Play' to play again, or 'Quit' to Quit:";
string choice2;
cin >> choice2;
if (choice2 == "Play")
{
/*go to start*/
}
elseif (choice2 == "Quit")
{
return 0;
}
}
TogglePlayer();
}
system("PAUSE");
return 0;
}
where are you getting this code? this doesn't look rite to me at all. you need to specify an output type for your functions. I can't really work out your code, but i'll provide a simple program that demonstrates a do....while loop.
Games usually use while loops and seldom use do{}while();. The best way I know of is to make a bool variable (from looking at the code).
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
int main()
{
bool quit = false;
while (!quit){
// play game
if (Win() == 'X'){ // same for other conditions
char choice;
cout << "Would you like to play again? (y/n) ";
cin >> choice;
if(choice =='y'){
// clear the board and go again
}else{ // player said no and want to quit
quit = true;
}
}
}
return 0;
}
#include <iostream>
usingnamespace std;
int main()
{
// doing counting just to show
bool quit = false;
int count = 0;
char choice;
while (!quit){
count++;
cout << "Count is : " << count << endl
<< "Want to go again? (y/n)";
cin >> choice;
if(choice == 'y'){
cout << "If this was a complex game we would reset everything "
<< "to a clean board and begin agian.\n";
}else{ // they don't want to play anymore
cout << "Well good-bye!\n";
quit = true;
}
}
cout << "Hope you enjoyed the game!\n";
return 0;
}
Count is : 1
Want to go again? (y/n)y
If this was a complex game we would reset everything to a clean board and begin agian.
Count is : 2
Want to go again? (y/n)y
If this was a complex game we would reset everything to a clean board and begin agian.
Count is : 3
Want to go again? (y/n)y
If this was a complex game we would reset everything to a clean board and begin agian.
Count is : 4
Want to go again? (y/n)n
Well good-bye!
Hope you enjoyed the game!
Press any key to continue . . .
Doing so results in undefined behavior. For the same reason we don't recommend dereferencing pointers to random memory locations we shouldn't recommend recursively calling main, even though sometimes it appears to work.
Many C++ compilers with their default settings compile a language that is close to, but is not standard C++.
So it might have compiled cleanly if conformance to C++ was not specifically asked for.
For instance, clang++, g++ without -pedantic-errors: http://coliru.stacked-crooked.com/a/fa75618787da0ffd