// Arup Guha
// 7/31/02
// This program implements a guessing game, where the user has to guess a
// number in between 1 and 100. After each guess the user is told if
// his/her guess is too high or too low. Also, there is an option to see
// the computer play.

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

void main() {

  srand(time(0)); // Initialize the random number generator.
  int number = (rand()%100) + 1; // Create secret number
  int guess;
  char choice;
  int high = 100;
  int low = 1;

  // Ask user who is playing.
  cout << "Do you want to (p)lay or do you want to see the (c)omputer play?\n";
  cin >> choice;

  do {
    
    // Get next guess based on computer or human player.
    if (choice=='p') {
      cout << "Enter your guess : " << flush;
      cin >> guess;
    }
    else {
      guess = (high+low)/2;
      cout << "The computer\'s guess is " << guess << ".\n";
    }
    
    // Deal with correct guess.
    if (guess == number) 
      cout << "You are correct, the number is " << number << ". You Win!!!\n";
	
    // Guess is too high.
    else if (guess>number) {
      cout << "Sorry, your guess was too high.\n";
      high = guess - 1;
    }

    // Guess is too low.
    else {
      cout << "Sorry, your guess was too low.\n";
      low = guess + 1;
    }
  } while (guess!=number);
}

