# Little Game Project - Solution C++

This is a place for creative solutions to Ivan’s challenge to create a simple C++ game. The game had the requirements of the following:

THE RULES:

1. Player 1 selects a random number
2. Player 2 needs to guess that number
3. The game will tell Player 2 whether the guess is too high or too low
4. The game will count the number of attempts Player 2 made
1 Like

Multi-player or Single-player option:

I misunderstood the original question and thus came up with the following code for a solution to Ivan’s Little Game Project for C++. Hope it is at least interesting if not helpful. Enjoy (after posting I notice that the iostream included first does not display)

#include
#include<stdlib.h>
#include<time.h>

using namespace std;

int getNumber(int gametype){
//Choose a random number and return it.
int x = 0;
if(gametype==1){ //One player game
srand(time(0));
x = rand()%100+1;
} else {//Two player game
cout<<"Player 1, enter a secret number for Player 2 to guess: ";
cin>>x;
}

``````    return x;
}
``````

int guessOnce(){

``````int guess = 0;
cout<<"Can you guess the number between 1-100 player 2?"<<" Enter a number to guess:";
cin>>guess;
return guess;
``````

}

int guessAgain(){
int guess = 0;
cin>>guess;
return guess;

}

int main()
{
int gameType= 0;
//Play against the computer or a person?
cout<<"To play a one-player game type ‘1’. "<<"To play a two-player game type ‘2’. ";
cin>>gameType;
int x = 0;
//CHECK --> cout<<“x has just been initialized to “<<x<<” in main()”<<endl;

``````x = getNumber(gameType);
//CHECK --> cout<<"x has just been received back from getNumber as "<<x<<endl;
int guess = 0;
int attempts = 0;

//Player two guesses
guess = guessOnce();

//Response of "Go Higher", "Go Lower" WHILE not the same...
while(x!=guess){

if(guess<x){
cout<<"Go Higher"<<endl;
}
else {cout<<"Go Lower"<<endl;}
guess = guessAgain();
attempts++;
}
//Counting the number of attempts to success
cout<<"You succeeded with "<<attempts<<" attempts."<<endl;

return 0;
``````

}

1 Like
``````#include <iostream>

using namespace std;

int main()
{
//The rules:
// 1) Player 1 selects a random number
// 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 number = 0;
cout<< "Player One, please select a number for player two to guess"<<endl;
cin >> number;
int guess = 0;
cout<< "Player Two, Please try and guess the number"<<endl;
cin>> guess;
int i = 0;
while(guess!= number){ // I used a while loop to use the integer i as global scope.
i++;
if (guess < number) {
cout<<"Your guess was less than the actual number"<<endl;
}else if (guess>number){
cout<<"Your guess was more than the actual number"<<endl;
}
if (i!=0){
cout<< "Player Two, Please try again"<<endl;
cin>> guess;
}
}
// by using i in a global scope I can call upon it after the loop
cout<<" Correct, you got the right answer within "<< i + 1 << " attempts"<<endl;
return 0;
}``````

#include

using namespace std;

int main()
{
int player1Number = 0;
int player2Number = 0;
int numberOfAttempts = 0;
bool notGuessed = true;

``````cout << "Player 1 choose number" << endl;
cin >> player1Number;

while (notGuessed)
{
cout << "Player 2 choose number" << endl;
cin >> player2Number;

numberOfAttempts++;

if (player2Number == player1Number)
{
cout << "Player 2 Guessed Correctly With " << numberOfAttempts << " Guesses" << endl;
notGuessed = false;

}
else if(player2Number < player1Number){

cout << "Player 2 Guessed Wrong, The number Is Higher" << endl;

}
else{
cout << "Player 2 Guessed Wrong, The number Is Lower" << endl;

}
}

return 0;
``````

}

My solution:

``````#include<iostream>

using namespace std;

int main() {
int number;
int guess;
int attempt = 0;
while(1) {
// Infinitely loop until the game is complete
if(attempt == 0){
cout << "Player 1, enter a number other than 0: " << endl;
cin >> number;
attempt++;
} else {
cout << "Player 2, enter a guess: (or 0 to exit) " << endl;
cin >> guess;
if(guess == 0){
cout << "You quit after " << attempt << " guesses! For Shame..." << endl;
break;
} else if(guess == number){
cout << "You have guessed the number after " << attempt << " attempts!" << endl;
break;
} else {
if(guess>number){
cout << "You have guessed higher than the number you seek." << endl;
} else {
cout << "You have guessed lower than the number you seek." << endl;
}
}
attempt++;
}
}
}
``````

Thinking about adding in a private terminal for player one to enter their number on so it isn’t in the console.

Hi,

I’ve tried to extend the exercise and make sure player 1 and 2 only can type numbers. This works fine by using the code at the end of the message. What I can’t wrap my head around is:

If player one types 5 and player two types 5½ it says “YOU GUESSED IT”. How can I prevent that from happening? it should be too high

Here is the code for making sure they type a number:

#include

do{
cout << “Player one please type a random number” << endl;
cin >> numberTyping;

``````    bFail = cin.fail();
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(), '\n');
if(bFail== true){cout << "This is not a valid input. Try again!" << endl;}

} while (bFail == true);
``````

If the this generally works for other cases but not the 5 & 5.5 case mentioned, the problem may be that you initialized the variables as int (or integer). This means that even if the person types in something like 5.5 it will be truncated to the integer value of 5 and therefore be a successful match. Try using a float type number. This will not truncate the value to an integer. The problem that may exist with this is that it may be too particular and allow people to type in something like 5.12023 and then never match.

Play around and let me know if this helped… Good Luck!

Thanks for the reply. I’ll test that out.

I see the complexity in making the player able to type 5.12023. I just through there would be a way for the system to say that 5 <> 5.5 and still not type such complex numbers, but I can see that might contradict with one another.

My solution is this

``````#include <iostream>
#include <string>

using namespace std;

int main()
{

int randomNumber = 0;
int guessNumber = 0;
int numberOfTries = 0;

cout << "Welcome to the Guessing-Game!" << endl << endl;
cout << "Player 1, choose a random number: ";
cin >> randomNumber;
cout << "Player 2, guess the number that Player 1 picked! ";
cin >> guessNumber;

while(guessNumber!=randomNumber){

if (guessNumber>randomNumber){
numberOfTries = numberOfTries + 1;
cout << "Oh no, you are wrong!! The Number you picked is higher than the guessed one!" << endl;
cout << "This was try number " << numberOfTries << "." << endl;
cout << "Try again: ";
cin >> guessNumber;
}

else if (guessNumber<randomNumber){
numberOfTries = numberOfTries + 1;
cout << "Oh no, you are wrong!! The Number you picked is lower than the guessed one!" << endl;
cout << "This was try number " << numberOfTries << "." << endl;
cout << "Try again: ";
cin >> guessNumber;
}
}

if (guessNumber == randomNumber){

if (numberOfTries==0){
cout << "Congratulations, you found the number after " << numberOfTries+1 << " try!!!";
}
else{
cout << "Congratulations, you found the number after " << numberOfTries+1 << " tries!!!";
}
}

return 0;
}

``````

Also, @ivan you forgot in your solution video to create a line where the program counts how many times the user tried to guess, since that was the rule, i think.

1 Like

Hi, here’s my solution for the exercise.

``````
#include <iostream>

using namespace std;

int main()
{
int originalNumber = 0 ;
int numberPlayerTwo = 0 ;
int numberOfTries = 0;

cout<<"Player 1, please input a number." << endl;
cout << "Number: " ;
cin >> originalNumber;

while (originalNumber > 11) {
cout << "Please enter a number between 1 - 10 only." <<endl;
cin >> originalNumber;
}

cout << "Number added!" << endl;
cout << "Please let Player 2 guess the number." << endl;
cout << "Are you Player 2? Type '5' if yes." <<endl;
cout << "Please let Player 2 play." <<endl;
}

cout << "Okay then. Please guess the number." << endl;
cin >> numberPlayerTwo;
numberOfTries++;

while (numberPlayerTwo != originalNumber ) {
if (numberPlayerTwo < originalNumber){
cout << "Too low! Please try again." <<endl;
}
if (numberPlayerTwo > originalNumber){
cout << "Too high! Please try again." <<endl;
}
cin >> numberPlayerTwo;
numberOfTries++;
}

cout << "Correct! You got it in "<<numberOfTries<<" tries" << endl;

return 0;
}
``````