мой цикл печатает мою функцию PlayAgain дважды - PullRequest
0 голосов
/ 02 мая 2019

так что у меня есть основа угадать игру чисел в моем int main у меня есть три функции, используемые для его воспроизведения. У меня есть игровой цикл, окружающий эти функции, с bool = false. возвращаемое значение установлено равным моей функции PlayAgain. все работает хорошо и хорошо, но когда вы угадываете правильное число, он спрашивает, хотите ли вы сыграть снова дважды по какой-то причине.

Я попытался удалить один из случаев, когда я вызываю функцию в главном

#include <iostream>
#include <cstdlib>
#include <ctime>
#include<string>


void PrintIntro();
void PlayGame();
bool PlayAgain();

int main() {

    bool bPlayAgain = false;
    do {
        PrintIntro();
        PlayGame();
        PlayAgain();     //I've tried removing this line
        bPlayAgain = PlayAgain(); //Ive also played around with this one
    } while (bPlayAgain);

    return (0);
}

void PrintIntro()
{
    std::cout << "Guess a number between 1-100, fool!\n";
}

void PlayGame()
{
    srand(static_cast<unsigned int> (time(0)));
    int HiddenNumber = rand();
    int Number = (HiddenNumber % 100) + 1;
    int Guess;

    do {
        std::cin >> Guess;
        if (Guess > Number) {
            std::cout << "You are too high bro!\n\n";
        }
        else if (Guess < Number) {
            std::cout << "You need to get higher bro!\n\n";
        }
        else if (Guess = Number) {
            std::cout << "You are just high enough, you win!\n\n";
        }
    } while (Guess != Number);
}

bool PlayAgain()
{
    std::string Response = "";
    std::cout << "Would you like to play again? yes or no." << std::endl;
    std::getline(std::cin, Response);
    std::cout << std::endl;

    return (Response[0] == 'y') || (Response[0] == 'Y');
}

1 Ответ

1 голос
/ 03 мая 2019

здесь исправлен код.Я добавил новую булеву функцию Game, которая играет в игру и возвращает true, если игрок хочет повторно сыграть.

#include <iostream>
#include <cstdlib>
#include <ctime>
#include<string>


void PrintIntro();
void PlayGame();
bool PlayAgain();
bool Game();

int main() {

    bool bPlayAgain = Game();
    while(bPlayAgain == true)
    {
        bPlayAgain = Game();
    }
    return (0);
}

void PrintIntro()
{
    std::cout << "Guess a number between 1-100, fool!\n";
}

void PlayGame()
{
    srand(static_cast<unsigned int> (time(0)));
    int HiddenNumber = rand();
    int Number = (HiddenNumber % 100) + 1;
    int Guess;

    do {
        std::cin >> Guess;
        if (Guess > Number) {
            std::cout << "You are too high bro!\n\n";
        }
        else if (Guess < Number) {
            std::cout << "You need to get higher bro!\n\n";
        }
        else if (Guess = Number) {
            std::cout << "You are just high enough, you win!\n\n";
        }
    } while (Guess != Number);
}

bool PlayAgain()
{
    char Response;
    std::cout << "Would you like to play again? yes or no." << std::endl;
    std::cin >> Response;
    std::cout << std::endl;

    return (Response == 'y') || (Response == 'Y');
}

bool Game()
{
    PrintIntro();
    PlayGame();
    bool selection = PlayAgain();

    return selection;
}
...