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

Add Factor
m (Fix Perl 6 -> Raku calling conventions and comments)
(Add Factor)
Line 807:
end if
end while</lang>
 
=={{header|Factor}}==
This solution uses the <code>search</code> combinator. It performs a binary search on a sequence by placing the "guess" on top of the data stack and expecting an ordering specifier in return.
<lang factor>USING: binary-search formatting io kernel math.ranges words ;
 
: instruct ( -- )
"Think of a number between 1 and 100." print
"Score my guess with +lt+ +gt+ or +eq+." print nl ;
 
: score ( n -- <=> )
"My guess is %d. " printf readln "math.order" lookup-word ;
 
: play-game ( -- n )
100 [1,b] [ score ] search nip nl ;
 
: gloat ( n -- )
"I did it. Your number was %d!\n" printf ;
 
instruct play-game gloat</lang>
{{out}}
<pre>
Think of a number between 1 and 100.
Score my guess with +lt+ +gt+ or +eq+.
 
My guess is 51. +lt+
My guess is 26. +gt+
My guess is 38. +lt+
My guess is 32. +gt+
My guess is 35. +lt+
My guess is 33. +eq+
 
I did it. Your number was 33!
</pre>
 
=={{header|Fantom}}==
1,808

edits