C++ Guessing Game

I did not see anywhere to post the Guessing Game, but this was a fun assignement.


#include <iostream>

using namespace std;

int main()
{
   // string name;
   // cout << "...and what is your name?";
   // cin >> name;
   // cout << createGreeting(name);

   // game -create random number - pl2 guess random number
   // too high or too low - guess again
   // count tries

   int randomnumber = rand() % 100 + 1; // create a random number from 1-100
   cout << "Welcome the the Ivan guess a number game. Please guess a number from 1 to 100!" << endl;
   int tries = 1;
   int guess = 0;


   while (guess != randomnumber){
        cout << "Guess! ";
        cin >> guess;
            if (guess < randomnumber) {
                cout << "Woops - too low. Try again." << endl;
            }else if (guess > randomnumber) {
                cout << "Too high. Try again. "  << endl;
            }
        tries ++;
        }
        // added sass
        string comment;
        if (tries < 10) {
            comment = " - you are a superstar!";
        } else if (tries > 30) {
            comment = "Uhm.. That took you a long time.";
        }
        cout << "Great job you only took " << tries << " attempts!" << comment;

    return 0;
}
2 Likes

Nice. Yah that was a fun one. Your solution is considerably more compact than mine. I think I tried to over engineer it a bit :smile:

  • Supports 2 players
    • First player picks the number
    • Second player guesses
  • Sets the max number of guesses to 10
    • If player 2 guesses the number before they run out of guesses they win
    • Else player 1 wins
Guessing game solution
#include <iostream>

using namespace std;

void hideTheNumber() {
    for(int i=0;i<100;i++) {
        cout << "\n";
    }
    cout << "OK the number is hidden! Player 2 can open their eyes now.\n\n";
}

int pickNumber() {
    cout << "\nTHE RULES\n"
        << "1) Player 1 selects a random number (player 2 must not look!)\n"
        << "2) Player 2 needs to guess the number\n"
        << "3) The game will tell Player 2 if the guess is too high or too low\n"
        << "4) Play continues until Player 2 guesses the correct number\n\n";

    int n = 0;
    cout << "Player 1 ask Player 2 to close their eyes!\n"
        << "When their eyes are closed, enter the number to guess: ";
    cin >> n;

    hideTheNumber();

    return n;
}

int getGuess() {
    int guess = 0;
    cout << "\nPlayer 2, enter your guess: ";
    cin >> guess;
    return guess;
}

void giveHint(int theNumber, int guess, int numGuesses, int maxGusses) {
    if (theNumber > guess) {
        cout << "Your gues is too low!";
    } else {
        cout << "Your guess was too high!";
    }

    if (numGuesses < maxGusses) {
        cout << " Try again. You have " << maxGusses - numGuesses << " guesses remaining.\n";
    }
}

void playerTwoWins(int theNumber, int numGuesses) {
    cout << "\nPLAYER 2 WINS! " << theNumber << " was the number!\n"
        << "It took you " << numGuesses << " guesses.\n";
}

void playerOneWins(int maxGuesses) {
    cout << "PLAYER 1 WINS! You are only allowed " << maxGuesses << " guesses.\n";
}

bool playAgain() {
    string response;
    cout << "\nPlay again? (Y/N) ";
    cin >> response;

    if (response == "Y" || response == "y") {
        return true;
    }

    return false;
}

int main()
{
    cout << "Welcome to the Number Gussing Game!\n\n";

    do {
        int numGuesses = 0;
        int maxGuesses = 10;
        int theNumber = pickNumber();
        do {
            int guess = getGuess();
            numGuesses++;

            if (guess == theNumber) {
                playerTwoWins(theNumber, numGuesses);
                break;
            } else if (numGuesses >= maxGuesses) {
                playerOneWins(maxGuesses);
                break;
            } else {
                giveHint(theNumber, guess, numGuesses, maxGuesses);
            }
        } while (true);
    } while(playAgain());

    cout << "\nThanks for playing! See you next time.";
    return 0;
}

2 Likes

The solution provided by Ivan in the video does not included the code how to count the number of attempts. So this is my solution.

#include < iostream >

using namespace std;

int main()
{
/* Game : The rule
1. Player 1 selects a random numbers
2. Player 2 needs to guess the number
3. The game will tell player 2 whether the guess is too low or too high
4. The game will count the number of attempts player 2 made */

int selectNumber = 0;
int currentNumber = 0;
int numberOfAttempts = 0;
cout<<"Player 1 please enter the random number : "<<endl;
cin>> selectNumber;

while(true){
    numberOfAttempts++;
    cout<<"Player 2 please guess the number : "<<endl;
    cin>> currentNumber;
    if(currentNumber > selectNumber){
    cout<< currentNumber <<" is too high"<< endl;
    }
    else if(currentNumber < selectNumber){
    cout<< currentNumber <<" is too low"<< endl;
    }
    else{
        cout<< currentNumber<<" is the right answer"<< endl;
        break;
    }
}
cout<<"Player 2 you made "<<numberOfAttempts<<" Attemps!"<<endl;
return 0;

}

1 Like

Excellent sir, congratulations by completing it by yourself! :muscle:
I have tested the code and it’s runs perfect, you are counting the number of attempts of the player 2 perfectly!

Carlos Z.

1 Like