Skip to the content of the web site.

Project M.5: High-low

Write a game of Hi-lo where your program will play with a human player. The player and your program will alternate between guessing the number between 1 and 100. You begin by asking whether or not the player wishes to start guessing first.

Do you want to start guessing first (y/n)? 

When the human guesses, your program will say "correct", "high" or "low", meaning that the guess is either too high or too low, respectively. Your program will record the number of guesses.

When the human wins, the maximum, average, minimum and total number of guesses will be displayed, together with the number of games.

Maximum number of guesses: 15
Average number of guesses: 15
Minimum number of guesses: 15
  Total number of guesses: 15
          Number of games: 1

When it is your program's turn to guess, you should follow an optimal strategy. Additionally, you should be able to determine if the player made a mistake.

My guess is 5. Is this correct, high or low (c/h/l)?

Your program should ignore case with the player's response. Your program should also indicate whether or not the player may have made a mistake.

Here is one strategy you can employ. In guess a number between 1 and 100, there are two numbers in the middle: 50 and 51. If your program always chooses 50, and then subsequently always chooses 25 or 75, the player can make an intelligent guess as to which number to choose so as to maximize the number of turns the computer must play. Instead, if there are two choices, randomly choose one of the two.

There is a 50-50 chance that it will take your program the maximum number of guesses to determine the number the player picked. If your program repeatedly takes the maximum number of guesses, you may ask if the player is cheating. After $n$ games, approximately $\frac{n}{2}$ games should have taken the maximum number of guesses; however, if $\frac{n}{2} + \frac{1}{\sqrt{n}}$ or more games took the maximum number of guesses, then this is two standard deviations from the expected outcome, and you can say with almost 98% certainty that the player is cheating.