//
// Program name:  guessing_game
// Author:  B. Massingill
//
// Purpose:  user thinks of a number and program tries to guess 
//	it
//
// Input/Output:
//	Program instructs user to think of a number between 1 and 100.
//		(Call the number X.)
//	Program repeatedly guesses a number, prints it out, and prompts
//		user to enter:
//		"<" if the guess < X
//		"=" if the guess = X
//		">" if the guess > X
//	At end, program prints how many guesses were required.
//	
//	If user enters inconsistent replies (e.g., first saying the
//	number is >10 and later that it's <5), program should cope --
//	print an error message.
//
#include <iostream.h>
int main()
{
	int low = 1 ;		// low end of range of possible values
	int high = 100 ;	// high end of range of possible values
	int guess ;		// next guess
	int num_guesses = 0 ;	// number of guesses so far 
	char response ;		// response to prompt
	bool found = false ;	// true when program has guessed number

	cout << "Think of a number between " << low << " and "
		<< high << endl ;
	cout << "I will try to guess the number\n" ;

	// we know here that low <= X <= high

	while (!found && (low <= high))
	{
		// we know here that low <= X <= high
		// and that low <= high

		// make a new guess -- midpoint of range
		guess = (low + high) / 2 ;
		num_guesses++ ;

		// ask user if it's too big, too small, or just right
		cout << "My guess is " << guess << endl ;
		cout << "(I am guessing in the range " << low 
			<< " to " << high << ")\n" ;
		cout << "Enter:\n"
			<< "\t= if your number = " << guess << endl 
			<< "\t< if your number < " << guess << endl 
			<< "\t> if your number > " << guess << endl ;
		cin >> response ;

		// if user enters something other than <, >, or =,
		// 	prompt again
		while (!((response == '=') || (response == '<') ||
			(response == '>')))
		{
			cout << "Please enter =, <, or >\n" ;
			cin >> response ;
		}

		// now we know response is one of the characters we want
		if (response == '=') 
		{
			found = true ;
		}
		else if (response == '<')
		{
			high = guess - 1 ;
		}
		else	// response == '>'
		{
			low = guess + 1 ;
		}

		// at this point we have either
		//	found = true
		// or
		//	found = false and low <= X <= high
		//	if low > high, we can't guess the number --
		//	the user must have entered bad replies
	}

	if (found)
	{
		cout << "Your number was " << guess << endl ;
		cout << "Number of guesses was " << num_guesses << endl ;
	}
	else
	{
		cout << "I could not guess your number.\n" ;
		cout << "I think you gave inconsistent replies!\n" ;
	}

	return 0 ;
}