Guess the number/With feedback (player)

From Rosetta Code
Revision as of 10:22, 8 November 2010 by rosettacode>Dkf (Mark as game)
Task
Guess the number/With feedback (player)
You are encouraged to solve this task according to the task description, using any language you may know.

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

Fortran

Works with: Fortran version 95 and later

<lang fortran>program Guess_a_number_Player

 implicit none
 
 integer, parameter :: limit = 100
 integer :: guess, mx = limit, mn = 1
 real :: rnum
 character(1) :: score
 
 write(*, "(a, i0, a)") "Think of a number between 1 and ", limit, &
                        " and I will try to guess it." 
 write(*, "(a)")  "You score my guess by entering: h if my guess is higher than that number"
 write(*, "(a)")  "                                l if my guess is lower than that number"
 write(*, "(a/)") "                                c if my guess is the same as that number"
 call random_seed
 call random_number(rnum)
 guess = rnum * limit + 1
 do
   write(*, "(a, i0, a,)", advance='no') "My quess is: ", guess, "   Score(h, l or c)?: "
   read*, score
   select case(score)
     case("l", "L")
       mn = guess
       guess = (mx-guess+1) / 2 + mn 
       
     case("h", "H")
       mx = guess
       guess = mx - (guess-mn+1) / 2 
     case("c", "C")
       write(*, "(a)") "I solved it!"
       exit
     case default
       write(*, "(a)") "I did not understand that"
   end select
 end do

end program</lang> Output

Think of a number between 1 and 100 and I will try to guess it.
You score my guess by entering: h if my guess is higher than that number
                                l if my guess is lower than that number
                                c if my guess is the same as that number

My guess is: 58   Score(h, l or c)?: h
My guess is: 29   Score(h, l or c)?: l
My guess is: 44   Score(h, l or c)?: l 
My guess is: 51   Score(h, l or c)?: l 
My guess is: 55   Score(h, l or c)?: l 
My guess is: 57   Score(h, l or c)?: c
I solved it!

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.

Tcl

<lang tcl>set from 1 set to 10 fconfigure stdout -buffering none while 1 {

   set guess [expr {($from+$to) / 2}]
   puts -nonewline "Guess: $guess\tWas it lower (<) equal (=) or higher (>)? "
   switch -- [gets stdin] {

< { set from [expr {$guess + 1}] } > { set to [expr {$guess - 1}] } = { puts "Found it: $guess" break } default { puts "What sort of a response was that?!" }

   }
   if {$to < $from} {

puts "No answer possible?!" break

   }

}</lang> Sample run:

Guess: 5	Was it lower (<) equal (=) or higher (>)? <
Guess: 8	Was it lower (<) equal (=) or higher (>)? >
Guess: 6	Was it lower (<) equal (=) or higher (>)? =
Found it: 6