Sorry if this is obvious, I'm new to coding.
Whenever the game ends and I type "y" to play again it displays what is already on the screen over and over again. I know that this is because whenever I type "y" it runs the active game instead of restarting, my question is, how do I get it to restart?
This is specifically about void playgame() and int main().
#include <iostream>
#include <string>
std::string correcans = "coding";
std::string placeholder = "------";
std::string wrongans = "";
int amountguess = 6;
void title()
{
std::cout << "##########################################" << std::endl;
std::cout << "# HANGMAN #" << std::endl;
std::cout << "##########################################\n" << std::endl;
}
void hangdraw(int amountguess)
{
if (amountguess == 6)
{
std::cout << " |-----------------|" << std::endl;
std::cout << " | " << std::endl;
std::cout << " |" << std::endl;
std::cout << " |" << std::endl;
std::cout << "_|__________________" << std::endl;
}
else if(amountguess == 5)
{
std::cout << " |-----------------|" << std::endl;
std::cout << " | 0" << std::endl;
std::cout << " | " << std::endl;
std::cout << " |" << std::endl;
std::cout << "_|__________________" << std::endl;
}
else if (amountguess == 4)
{
std::cout << " |-----------------|" << std::endl;
std::cout << " | 0" << std::endl;
std::cout << " | |" << std::endl;
std::cout << " | " << std::endl;
std::cout << "_|__________________" << std::endl;
}
else if (amountguess == 3)
{
std::cout << " |-----------------|" << std::endl;
std::cout << " | 0" << std::endl;
std::cout << " | |" << std::endl;
std::cout << " | /" << std::endl;
std::cout << "_|__________________" << std::endl;
}
else if (amountguess == 2)
{
std::cout << " |-----------------|" << std::endl;
std::cout << " | 0" << std::endl;
std::cout << " | |" << std::endl;
std::cout << " | / \\" << std::endl;
std::cout << "_|__________________" << std::endl;
}
else if (amountguess == 1)
{
std::cout << " |-----------------|" << std::endl;
std::cout << " | 0 /" << std::endl;
std::cout << " | |" << std::endl;
std::cout << " | / \\" << std::endl;
std::cout << "_|__________________" << std::endl;
}
else if (amountguess == 0)
{
std::cout << " |-----------------|" << std::endl;
std::cout << " | \\ 0 /" << std::endl;
std::cout << " | |" << std::endl;
std::cout << " | / \\" << std::endl;
std::cout << "_|__________________" << std::endl;
}
}
void guessdisplay()
{
std::cout << std::endl;
std::cout << placeholder;
std::cout << std::endl;
std::cout << "\n------------------------------------------" << std::endl;
std::cout << "| Wrong Letters |";
std::cout << "\n------------------------------------------" << std::endl;
std::cout << wrongans;
std::cout << "\n------------------------------------------" << std::endl;
std::cout << "Guesses Left: " << amountguess << std::endl;
}
char input()
{
std::cout << "Guess Letter: ";
char letter;
std::cin >> letter;
std::cin.clear();
std::cin.ignore();
return letter;
}
void guess(char letter)
{
bool sameletter = false;
for (int i = 0; i < placeholder.size(); i )
{
if (correcans[i] == letter)
{
placeholder[i] = correcans[i];
sameletter = true;
}
}
if (sameletter == false)
{
amountguess -= 1;
wrongans = letter;
}
}
void winorlose()
{
title();
hangdraw(amountguess);
guessdisplay();
if (placeholder == correcans)
{
std::cout << "\n------------------------------------------" << std::endl;
std::cout << "| YOU WIN!!! |";
std::cout << "\n------------------------------------------" << std::endl;
}
else
{
std::cout << "\n------------------------------------------" << std::endl;
std::cout << "| YOU LOSE!!! |";
std::cout << "\n------------------------------------------" << std::endl;
}
}
void playgame()
{
while (placeholder != correcans && amountguess > 0)
{
title();
hangdraw(amountguess);
guessdisplay();
char letter = input();
guess(letter);
system("cls");
}
winorlose();
}
int main()
{
bool isRunning = true;
while (isRunning)
{
playgame();
std::cout << "Would you like to play again (y/n)? ";
char userChoice;
std::cin >> userChoice;
if (userChoice == 'y' || userChoice == 'Y')
{
isRunning = true;
}
else
{
isRunning = false;
}
}
return 0;
}
CodePudding user response:
you just need to reset variables after play
use this in int main() method
if (userChoice == 'y' || userChoice == 'Y')
{
placeholder = "------";
wrongans = "";
amountguess = 6;
isRunning = true;
}
else
{
isRunning = false;
}