How to resolve the algorithm Guess the number/With feedback (player) step by step in the J programming language
Published on 12 May 2024 09:40 PM
How to resolve the algorithm Guess the number/With feedback (player) step by step in the J programming language
Table of Contents
Problem Statement
Write a player for the game that follows the following rules: The computer should guess intelligently based on the accumulated scores given. One way is to use a Binary search based algorithm.
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Guess the number/With feedback (player) step by step in the J programming language
Source code in the j programming language
require 'misc'
guess=:3 :0
'lo hi'=.y
while.lo < hi do.
smoutput 'guessing a number between ',(":lo),' and ',":hi
guess=.lo+?hi-lo
select.{.deb tolower prompt 'is it ',(":guess),'? '
case.'y'do. smoutput 'Win!' return.
case.'l'do. lo=.guess+1
case.'h'do. hi=.guess-1
case.'q'do. smoutput 'giving up' return.
case. do. smouput 'options: yes, low, high, quit'
end.
end.
)
guess 1 100
guessing a number between 1 and 100
is it 86? hi
guessing a number between 1 and 85
is it 56? hi
guessing a number between 1 and 55
is it 24? lo
guessing a number between 25 and 55
is it 29? lo
guessing a number between 30 and 55
is it 43? lo
guessing a number between 44 and 55
is it 53? hi
guessing a number between 44 and 52
is it 51? hi
guessing a number between 44 and 50
is it 48? lo
guessing a number between 49 and 50
is it 49? lo
50
You may also check:How to resolve the algorithm Top rank per group step by step in the Haskell programming language
You may also check:How to resolve the algorithm Reflection/List properties step by step in the Go programming language
You may also check:How to resolve the algorithm XML/DOM serialization step by step in the J programming language
You may also check:How to resolve the algorithm Empty program step by step in the Wren programming language
You may also check:How to resolve the algorithm Literals/String step by step in the Objective-C programming language