Guess the number/With feedback (player)
Guess the number/With feedback (player) is a draft programming task. It is not yet considered ready to be promoted as a complete task, for reasons that should be found in its talk page.
The task is to write a player for the game that follows the following rules:
- The scorer will choose a number between set limits. The computer player will print a guess of the target number. The computer asks for a score of whether its guess is higher than, lower than, or equal to the target. The computer guesses, and the scorer scores, in turn, until the computer correctly guesses the target number.
The computer should guess intelligently based on the accumulated scores given. One way is to use a binary search based algorithm.
C.f: Guess the number/With Feedback
Python
<lang python>inclusive_range = mn, mx = (1, 10)
print(\ Think of a number between %i and %i and wait for me to guess it. On every guess of mine you should state whether the guess was too high, too low, or equal to your number by typing h, l, or = % inclusive_range)
i = 0 while True:
i += 1 guess = (mn+mx)//2 txt = input("Guess %2i is: %2i. The score for which is (h,l,=): " % (i, guess)).strip().lower()[0] if txt not in 'hl=': print(" I don't understand your input of '%s' ?" % txt) continue if txt == 'h': mx = guess-1 if txt == 'l': mn = guess+1 if txt == '=': print(" Ye-Haw!!") break if (mn > mx) or (mn < inclusive_range[0]) or (mx > inclusive_range[1]): print("Please check your scoring as I cannot find the value") break
print("\nThanks for keeping score.")</lang>
Sample Game-play
Think of a number between 1 and 10 and wait for me to guess it. On every guess of mine you should state whether the guess was too high, too low, or equal to your number by typing h, l, or = Guess 1 is: 5. The score for which is (h,l,=): l Guess 2 is: 8. The score for which is (h,l,=): l Guess 3 is: 9. The score for which is (h,l,=): l Guess 4 is: 10. The score for which is (h,l,=): = Ye-Haw!! Thanks for keeping score.