Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <ctime>
- #include <string>
- #include <cstdlib>
- using namespace std;
- int tooHigh(int& n);
- int tooLow(int& n);
- int computerGuess(string highOrLow, int& max, int& min, int guess);
- int checkGuess(int guess, int randm, int& tries);
- int randomNum();
- int main()
- {
- int guess = 50;
- int tries = 0;
- int selection = 99;
- int randm = 0;
- cout<<"\t\t\tWELCOME TO THE GUESSING GAME!\n\n"<<endl;
- do
- {
- cout<<"\n\t 1. Human Vs. Computer - human Guesses."<<endl;
- cout<<"\n\t 2. Computer Vs. Human - Computer Guesses."<<endl;
- cout<<"\nPlease choose one of the play types (0 to exit): ";
- cin>>selection;
- switch(selection)
- {
- case 1:
- randm = randomNum();
- cout<<"\nThe computer has guessed a number between 0 and 100."<<endl;
- cout<<"Please enter your guess: ";
- do
- {
- cin>>guess;
- cin.ignore();
- checkGuess(guess, randm, tries);
- }while(guess != randm);
- cout<<"Thank you! You got it in "<<tries<<" tries."<<endl;
- tries = 0;
- cin.ignore();
- break;
- case 2:
- string highOrLow = "ERROR"; //user will enter wether computer guessed too high or too low
- int max = 101;
- int min = 0;
- cout<<"\nPlease Think of a number between 0 - 100"<<endl;
- cout<<"The computer will attempt to guess your number."<<endl;
- cout<<"Type in \"high\" if the number printed is too high."<<endl;
- cout<<"Type in \"low\" if the number printed is too low."<<endl;
- cout<<"Type in \"done\" if the number printed is what you guessed."<<endl;
- do
- {
- guess = computerGuess(highOrLow, max, min, guess);
- cout<<"\nGuess number "<<tries+1<<" is "<< guess<<endl;
- //cout<<"Max: "<<max<<endl;
- //cout<<"Min :"<<min<<endl;
- cin>>highOrLow;
- tries++;
- }while(highOrLow != "done");
- cout<<"Glad thats over!"<<endl;
- tries = 0;
- break;
- }
- }while(selection != 0);
- }
- int computerGuess(string highOrLow, int& max, int& min, int guess)
- {
- if(highOrLow == "high")
- {
- max = guess;
- guess = (min+max)/2;
- return guess;
- }
- else if(highOrLow == "low")
- {
- min = guess;
- guess = (min+max)/2;
- return guess;
- }
- else
- {
- guess = (min+max)/2;
- return guess;
- }
- }
- int checkGuess(int guess, int randm, int& tries)
- {
- if(guess < randm)
- {
- cout<<"Too Low!"<<endl;
- }
- else if(guess > randm)
- {
- cout<<"Too High!"<<endl;
- }
- else
- {
- cout<<"Perfect!"<<endl;
- }
- return tries++;
- }
- int randomNum()
- {
- srand(time(NULL));
- int randm = (rand() % 100) + 1;
- return randm;
- }
Advertisement
Add Comment
Please, Sign In to add comment