Guess the number/With feedback (player): Difference between revisions

From Rosetta Code
Content added Content deleted
(Added PureBasic)
(Add links)
Line 5: Line 5:
The computer should guess intelligently based on the accumulated scores given. One way is to use a [[wp:Binary search algorithm|binary search]] based algorithm.
The computer should guess intelligently based on the accumulated scores given. One way is to use a [[wp:Binary search algorithm|binary search]] based algorithm.


C.f: [[Guess the number/With Feedback]]
C.f: [[Guess the number/With Feedback]], [[Bulls and cows/Player]]


=={{header|PureBasic}}==
=={{header|PureBasic}}==

Revision as of 21:34, 29 October 2010

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, Bulls and cows/Player

PureBasic

<lang PureBasic>min=0 max=100

If OpenConsole()

 PrintN("Think of a number between "+Str(min)+" and "+Str(max)+".")
 PrintN("On every guess of mine you should state whether my guess was")
 PrintN("too high, too low, or equal to your number by typing 'h', 'l', Or '='")
 Repeat
   If max<=min
     PrintN("I think somthing is strange here...")
     Break
   EndIf
   Guess=(max-min)/2+min
   Print("My guess is "+Str(Guess)+",is this correct? "): Respons.s=UCase(Input())
   If Respons="H":     min=Guess
   ElseIf Respons="L": max=Guess
   ElseIf Respons="="
     PrintN("I did it!")
     Break
   Else
     PrintN("I do not understand that...")
   EndIf
 ForEver

EndIf</lang>

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.