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;
cout<<“Please guess again…”<<endl;
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;
}