meissner61

Guessing Game / Bracketing Search

Jul 21st, 2011
164
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.60 KB | None | 0 0
  1. #include <iostream>
  2. #include <ctime>
  3. #include <string>
  4. #include <cstdlib>
  5. using namespace std;
  6.  
  7.  
  8. int tooHigh(int& n);
  9. int tooLow(int& n);
  10. int computerGuess(string highOrLow, int& max, int& min, int guess);
  11. int checkGuess(int guess, int randm, int& tries);
  12. int randomNum();
  13.  
  14.  
  15.  
  16.  
  17.  
  18.  
  19. int main()
  20. {
  21.     int guess = 50;
  22.     int tries = 0;
  23.     int selection = 99;
  24.     int randm = 0;
  25.    
  26.  
  27.     cout<<"\t\t\tWELCOME TO THE GUESSING GAME!\n\n"<<endl;
  28.    
  29.  
  30.     do
  31.     {
  32.         cout<<"\n\t 1. Human Vs. Computer - human Guesses."<<endl;
  33.         cout<<"\n\t 2. Computer Vs. Human - Computer Guesses."<<endl;
  34.  
  35.         cout<<"\nPlease choose one of the play types (0 to exit): ";
  36.         cin>>selection;
  37.  
  38.    
  39.         switch(selection)
  40.         {
  41.             case 1:
  42.  
  43.                
  44.  
  45.                 randm = randomNum();
  46.                 cout<<"\nThe computer has guessed a number between 0 and 100."<<endl;
  47.                 cout<<"Please enter your guess: ";
  48.  
  49.                 do
  50.                 {
  51.                     cin>>guess;
  52.                     cin.ignore();
  53.  
  54.                     checkGuess(guess, randm, tries);
  55.  
  56.                 }while(guess != randm);
  57.  
  58.                 cout<<"Thank you! You got it in "<<tries<<" tries."<<endl;
  59.                 tries = 0;
  60.  
  61.                 cin.ignore();
  62.                 break;
  63.  
  64.             case 2:
  65.  
  66.                 string highOrLow = "ERROR"; //user will enter wether computer guessed too high or too low
  67.  
  68.                 int max = 101;
  69.                 int min = 0;
  70.  
  71.                 cout<<"\nPlease Think of a number between 0 - 100"<<endl;
  72.                 cout<<"The computer will attempt to guess your number."<<endl;
  73.                 cout<<"Type in \"high\" if the number printed is too high."<<endl;
  74.                 cout<<"Type in \"low\" if the number printed is too low."<<endl;
  75.                 cout<<"Type in \"done\" if the number printed is what you guessed."<<endl;
  76.  
  77.                 do
  78.                 {
  79.                     guess = computerGuess(highOrLow, max, min, guess);
  80.                     cout<<"\nGuess number "<<tries+1<<" is "<< guess<<endl;
  81.                     //cout<<"Max: "<<max<<endl;
  82.                     //cout<<"Min :"<<min<<endl;
  83.                     cin>>highOrLow;
  84.                     tries++;
  85.  
  86.                 }while(highOrLow != "done");
  87.  
  88.                 cout<<"Glad thats over!"<<endl;
  89.                 tries = 0;
  90.  
  91.                 break;
  92.  
  93.         }
  94.  
  95.     }while(selection != 0);
  96. }
  97.  
  98. int computerGuess(string highOrLow, int& max, int& min, int guess)
  99. {
  100.     if(highOrLow == "high")
  101.     {
  102.         max = guess;
  103.         guess = (min+max)/2;
  104.  
  105.         return guess;
  106.     }
  107.  
  108.     else if(highOrLow == "low")
  109.     {
  110.         min = guess;
  111.         guess = (min+max)/2;
  112.  
  113.         return guess;
  114.     }
  115.  
  116.     else
  117.     {
  118.         guess = (min+max)/2;
  119.  
  120.         return guess;
  121.     }
  122. }
  123.  
  124. int checkGuess(int guess, int randm, int& tries)
  125. {
  126.     if(guess < randm)
  127.     {
  128.         cout<<"Too Low!"<<endl;
  129.     }
  130.  
  131.     else if(guess > randm)
  132.     {
  133.         cout<<"Too High!"<<endl;
  134.     }
  135.  
  136.     else
  137.     {
  138.         cout<<"Perfect!"<<endl;
  139.     }
  140.  
  141.     return tries++;
  142. }
  143.  
  144. int randomNum()
  145. {
  146.     srand(time(NULL));
  147.     int randm = (rand() % 100) + 1;
  148.  
  149.     return randm;
  150. }
Advertisement
Add Comment
Please, Sign In to add comment