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

Added Easylang
(Added solution for Action!)
(Added Easylang)
 
(11 intermediate revisions by 9 users not shown)
Line 16:
{{trans|Python}}
 
<langsyntaxhighlight lang="11l">V (target_min, target_max) = (1, 10)
V (mn, mx) = (target_min, target_max)
 
Line 44:
L.break
 
print("\nThanks for keeping score.")</langsyntaxhighlight>
 
{{out}}
Line 62:
 
=={{header|Action!}}==
<langsyntaxhighlight Actionlang="action!">PROC Main()
BYTE n,min=[1],max=[100]
CHAR c
Line 86:
FI
OD
RETURN</langsyntaxhighlight>
{{out}}
[https://gitlab.com/amarok8bit/action-rosetta-code/-/raw/master/images/Guess_the_number_with_feedback_(player).png Screenshot from Atari 8-bit computer]
Line 108:
=={{header|Ada}}==
 
<langsyntaxhighlight Adalang="ada">with Ada.Text_IO;
procedure Guess_Number_Player is
procedure Guess_Number (Lower_Limit : Integer; Upper_Limit : Integer) is
Line 143:
end loop;
Guess_Number (Lower_Limit, Upper_Limit);
end Guess_Number_Player;</langsyntaxhighlight>
 
=={{header|ALGOL 68}}==
{{works with|ALGOL 68G|Any - tested with release 2.8.3.win32}}
<langsyntaxhighlight lang="algol68">BEGIN
INT lower := 1;
INT upper := 100;
Line 172:
FI
DO SKIP OD
END</langsyntaxhighlight>
 
=={{header|AppleScript}}==
 
<langsyntaxhighlight AppleScriptlang="applescript">-- defining the range of the number to be guessed
property minLimit : 1
property maxLimit : 100
Line 206:
end if
end repeat
end run</langsyntaxhighlight>
 
=={{header|Arturo}}==
 
<syntaxhighlight lang="rebol">print {:
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 =
:}
 
rmin: 1
rmax: 10
 
while ø [
guess: random rmin rmax
print ["My guess is:" guess]
inputOk: false
while [not? inputOk][
hle: strip input "How did I do? (h)igh, (l)ow, (=): "
case [hle]
when? [="h"][
if? rmin =< dec guess [rmax: dec guess, inputOk: true]
else -> print "\tThat doesn't make any sense. I cannot find the number..."
]
when? [="l"][
if? (inc guess) =< rmax [rmin: inc guess, inputOk: true]
else -> print "\tThat doesn't make any sense. I cannot find the number..."
]
when? [="="][
print ""
print "Great! I found it! :)"
print "Thanks for keeping the score."
exit
]
else [
print "\tPlease, check your input; it doesn't appear to be correct!"
]
]
print ""
]</syntaxhighlight>
 
{{out}}
 
<pre>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 =
 
My guess is: 7
How did I do? (h)igh, (l)ow, (=): h
 
My guess is: 1
How did I do? (h)igh, (l)ow, (=): fff
Please, check your input; it doesn't appear to be correct!
How did I do? (h)igh, (l)ow, (=): l
 
My guess is: 3
How did I do? (h)igh, (l)ow, (=): h
 
My guess is: 2
How did I do? (h)igh, (l)ow, (=): l
That doesn't make any sense. I cannot find the number...
How did I do? (h)igh, (l)ow, (=): =
 
Great! I found it! :)
Thanks for keeping the score.</pre>
 
=={{header|AsciiDots}}==
 
This is certainly not the fastest or smallest AsciiDots implementation of this task, but it works consistently.
 
The instructions print in order only because the ends of the lines are aligned. If they were not, they might print in a different order. The "Impossible" error message indicates that their are no remaining logical guesses. The range of 1 to 127 was chosen because it's easy to binary-search. All numbers are floating-point integers in AsciiDots, so rounding would be needed to use a range like 1 to 100.
 
<syntaxhighlight lang="asciidots">
/.
*$"Think of a number from 1 to 127"
*$"Enter H if my guess is too high" /#67\
*-$"Enter L if my guess is too low" *--{=}:$""$"I WIN"&
\-$"Enter C if my guess is correct"\| /------~*{-}\
/23@-------------------------------/|/#72\ | !| @ |
\#64>*$_"Is it "$_#$_"? (H/L/C) "#a?**--{=}+------/\-/ |
/---/| | | |
| \------------------------------+------*~*{+}------v
| /#1{>}:$"Impossible"&|/#76\ !| @ |
|#a_$01#\ /2#\ | @ \*--{=}-/\-/ |
\-------*{/}@*-*---*-----------------------------------/
</syntaxhighlight>
 
=={{header|AutoHotkey}}==
Line 212 ⟶ 297:
Works with the AutoHotkey entry at: [[Guess the number/With feedback]]
 
<langsyntaxhighlight AutoHotkeylang="autohotkey">MaxGuesses = 50
 
GetParams(LowerBound,UpperBound)
Line 265 ⟶ 350:
Else
Return, "Too High"
}</langsyntaxhighlight>
 
=={{header|Batch File}}==
<langsyntaxhighlight lang="dos">
@echo off
 
Line 299 ⟶ 384:
echo Guesses: %attempts%
pause>nul
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 314 ⟶ 399:
 
=={{header|BASIC}}==
<langsyntaxhighlight lang="basic">10 DEFINT A-Z
20 INPUT "Lower limit? ",L
30 INPUT "Upper limit? ",H
Line 325 ⟶ 410:
90 L=(H-L)\2+L: GOTO 40
100 H=(H-L)\2+L: GOTO 40
110 PRINT "It took";T;"tries."</langsyntaxhighlight>
{{out}}
<pre>Lower limit? 1
Line 344 ⟶ 429:
 
=={{header|BBC BASIC}}==
<langsyntaxhighlight lang="bbcbasic"> min% = 1
max% = 100
PRINT "Think of a number between "; min% " and " ;max%
Line 360 ⟶ 445:
UNTIL FALSE
PRINT "Goodbye."
END</langsyntaxhighlight>
 
=={{header|BCPL}}==
<langsyntaxhighlight lang="bcpl">get "libhdr"
 
let reads(s) = valof
Line 412 ⟶ 497:
writes("Upper bound? ") ; max := readn()
writef("It took %N attempts.*N", guess(min, max, 1))
$)</langsyntaxhighlight>
{{out}}
<pre>Lower bound? 1
Line 425 ⟶ 510:
 
=={{header|C}}==
<langsyntaxhighlight Clang="c">#include <stdio.h>
 
int main(){
Line 454 ⟶ 539:
 
return 0;
}</langsyntaxhighlight>
 
Demonstration (number is 57):
Line 471 ⟶ 556:
The following is a hacky solution using <tt>bsearch()</tt> and pointers to represent integers. Although the pointers do not point to valid things, <tt>bsearch()</tt> doesn't actually dereference the pointers or care what they point to; it just passes them to the comparator and searches the space of pointers. We can use that to search any space of integers.
{{trans|Java}}
<langsyntaxhighlight lang="c">#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
Line 522 ⟶ 607:
printf("Your number is %d.\n", (int)(result - ZERO));
return 0;
}</langsyntaxhighlight>
 
=={{header|C sharp}}==
<langsyntaxhighlight lang="csharp">using System;
using System.Collections.Generic;
using System.Linq;
Line 657 ⟶ 742:
}
}
</syntaxhighlight>
</lang>
 
=={{header|C++}}==
A clever solution that takes advantage of C++'s built-in binary search function <code>lower_bound()</code>. Instead of searching a slice of a container, we search a range of numbers by implementing a specially-designed custom iterator.
{{trans|Go}}
<langsyntaxhighlight lang="cpp">#include <iostream>
#include <algorithm>
#include <string>
Line 712 ⟶ 797:
std::cout << "Your number is " << answer << ".\n";
return 0;
}</langsyntaxhighlight>
 
=={{header|Ceylon}}==
<langsyntaxhighlight lang="ceylon">shared void run() {
while(true) {
variable value low = 1;
Line 753 ⟶ 838:
}
}
}</langsyntaxhighlight>
 
=={{header|Clojure}}==
 
<langsyntaxhighlight Clojurelang="clojure">(require '[clojure.string :as str])
 
(defn guess-game [low high]
Line 770 ⟶ 855:
\c (println "Huzzah!")
(do (println "Invalid input.")
(recur guess step)))))</langsyntaxhighlight>
Output
<pre>user=> (guess-game 1 100)
Line 790 ⟶ 875:
=={{header|Common Lisp}}==
An imperative solution using LOOP:
<syntaxhighlight lang="lisp">
<lang Lisp>
(defun guess-the-number (&optional (max 1000) (min 0))
(flet ((get-feedback (guess)
Line 807 ⟶ 892:
else do (setf max guess)
finally (write-line "I got it!"))))
</syntaxhighlight>
</lang>
 
A recursive solution (we use LABELS instead of FLET for the local function definitions in this version so that GUESS-LOOP will be able to call itself recursively):
<syntaxhighlight lang="lisp">
<lang Lisp>
(defun guess-the-number (&optional (max 1000) (min 0))
(labels ((guess-loop (min max)
Line 824 ⟶ 909:
(format t "Think of a number between ~a and ~a.~%" min max)
(guess-loop min max)))
</syntaxhighlight>
</lang>
 
=={{header|D}}==
{{trans|Python}}
<langsyntaxhighlight lang="d">import std.stdio, std.string;
 
void main() {
Line 869 ⟶ 954:
}
writeln("\nThanks for keeping score.");
}</langsyntaxhighlight>
{{out}}
<pre>Think of a number between 1 and 10 and wait for me to guess it.
Line 885 ⟶ 970:
{{libheader| system.console}}
Thanks JensBorrisholt for System.Console [https://github.com/JensBorrisholt/DelphiConsole/tree/master/Console].
<syntaxhighlight lang="delphi">
<lang Delphi>
program Guess_the_number;
 
Line 1,046 ⟶ 1,131:
 
readln;
end.</langsyntaxhighlight>
=={{header|EasyLang}}==
{{trans|Ring}}
<syntaxhighlight>
min = 1
max = 100
print "Think of a number between " & min & " and " & max
print "I will try to guess your number."
repeat
guess = (min + max) div 2
print "My guess is " & guess
write "Is it higher than, lower than or equal to your number? "
answer$ = input
print answer$
ans$ = substr answer$ 1 1
until ans$ = "e"
if ans$ = "l"
min = guess + 1
elif ans$ = "h"
max = guess - 1
else
print "Sorry, i didn't understand your answer."
.
.
print "Goodbye."
</syntaxhighlight>
 
=={{header|Elixir}}==
{{works with|Elixir|1.2}}
<langsyntaxhighlight Elixirlang="elixir">defmodule Game do
def guess(a..b) do
x = Enum.random(a..b)
Line 1,068 ⟶ 1,179:
end
end
Game.guess(1..100)</langsyntaxhighlight>
 
{{out|sample}}
Line 1,081 ⟶ 1,192:
 
=={{header|Erlang}}==
<langsyntaxhighlight lang="erlang">% Implemented by Arjun Sunel
-module(guess_game).
-export([main/0]).
Line 1,109 ⟶ 1,220:
guess(K,U,N)
end
end.</langsyntaxhighlight>
{{out}}
<pre>Player 1 : Guess my number between 1 and and 100 until you get it right.
Line 1,126 ⟶ 1,237:
=={{header|Euphoria}}==
{{trans|PureBasic}}
<langsyntaxhighlight lang="euphoria">include get.e
include wildcard.e
 
Line 1,156 ⟶ 1,267:
puts(1,"I do not understand that...\n")
end if
end while</langsyntaxhighlight>
 
=={{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.
<langsyntaxhighlight lang="factor">USING: binary-search formatting io kernel math.ranges words ;
 
: instruct ( -- )
Line 1,175 ⟶ 1,286:
"I did it. Your number was %d!\n" printf ;
 
instruct play-game gloat</langsyntaxhighlight>
{{out}}
<pre>
Line 1,193 ⟶ 1,304:
=={{header|Fantom}}==
 
<langsyntaxhighlight lang="fantom">
class Main
{
Line 1,230 ⟶ 1,341:
}
}
</syntaxhighlight>
</lang>
 
Example:
Line 1,265 ⟶ 1,376:
 
=={{header|FOCAL}}==
<langsyntaxhighlight lang="focal">01.10 A "LOWER LIMIT",L
01.15 A "UPPER LIMIT",H
01.20 S T=0
Line 1,280 ⟶ 1,391:
01.70 S H=G;G 1.3
01.75 T "ATTEMPTS",T,!
01.80 Q</langsyntaxhighlight>
{{out}}
<pre>LOWER LIMIT:1
Line 1,300 ⟶ 1,411:
=={{header|Fortran}}==
{{works with|Fortran|95 and later}}
<langsyntaxhighlight lang="fortran">program Guess_a_number_Player
implicit none
Line 1,337 ⟶ 1,448:
end select
end do
end program</langsyntaxhighlight>
Output
<pre>Think of a number between 1 and 100 and I will try to guess it.
Line 1,353 ⟶ 1,464:
 
=={{header|FreeBASIC}}==
<langsyntaxhighlight lang="freebasic">' FB 1.05.0 Win64
 
Dim hle As String
Line 1,384 ⟶ 1,495:
guess = (lowest + highest)\2
Loop
End</langsyntaxhighlight>
 
Sample input/output
Line 1,404 ⟶ 1,515:
=={{header|Go}}==
Go's binary search function (<code>sort.Search()</code>) is general enough to be able to do this type of task, as mentioned in the documentation for the function itself.[http://golang.org/pkg/sort/#Search]
<langsyntaxhighlight lang="go">package main
 
import (
Line 1,425 ⟶ 1,536:
})
fmt.Printf("Your number is %d.\n", lower+answer)
}</langsyntaxhighlight>
 
Manual solution:
<langsyntaxhighlight lang="go">package main
 
import (
Line 1,465 ⟶ 1,576:
}
}
}</langsyntaxhighlight>
 
=={{header|Haskell}}==
 
Explicit version with arbitrary range:
<syntaxhighlight lang="haskell">
<lang Haskell>
main :: IO ()
main = do
Line 1,494 ⟶ 1,605:
"h" -> loop guess to
(_) -> putStrLn "Invalid answer." >> loop from to
</syntaxhighlight>
</lang>
 
Short version with set limits:
<syntaxhighlight lang="haskell">
<lang Haskell>
main = f 0 100
where f x y = let g = div (x + y) 2 in
Line 1,505 ⟶ 1,616:
"h" -> f g y
"c" -> putStrLn "Yay!"
</syntaxhighlight>
</lang>
 
=={{header|Icon}} and {{header|Unicon}}==
{{trans|Fantom}}
 
<syntaxhighlight lang="icon">
<lang Icon>
procedure main ()
lower_limit := 1
Line 1,540 ⟶ 1,651:
}
end
</syntaxhighlight>
</lang>
 
=={{header|IS-BASIC}}==
<langsyntaxhighlight ISlang="is-BASICbasic">100 PROGRAM "GuessIt.bas"
110 LET N=100
120 TEXT 80
Line 1,568 ⟶ 1,679:
330 LOOP UNTIL K$>="0" AND K$<="3"
340 LET QUESTION=VAL(K$)
350 END DEF</langsyntaxhighlight>
 
=={{header|J}}==
 
<langsyntaxhighlight lang="j">require 'misc'
guess=:3 :0
'lo hi'=.y
Line 1,586 ⟶ 1,697:
end.
end.
)</langsyntaxhighlight>
 
Example session:
 
<syntaxhighlight lang="text"> guess 1 100
guessing a number between 1 and 100
is it 86? hi
Line 1,609 ⟶ 1,720:
guessing a number between 49 and 50
is it 49? lo
50</langsyntaxhighlight>
 
This could be made more efficient by replacing <code>guess=.lo+?hi-lo</code> with <code>guess=.<.-:lo+hi</code>. (The above example would have finished on the first guess, but the range 1..100 would never take more than 6 guesses -- the remaining answers would be determined after six guesses.)
Line 1,616 ⟶ 1,727:
A clever solution that uses the built-in binary search functions with a virtual list.
{{trans|Go}}
<langsyntaxhighlight lang="java5">import java.util.AbstractList;
import java.util.Collections;
import java.util.Scanner;
Line 1,651 ⟶ 1,762:
System.out.printf("Your number is %d.\n", result);
}
}</langsyntaxhighlight>
 
=={{header|JavaScript}}==
Line 1,660 ⟶ 1,771:
Well, it does use recursion for the guessing function, but that's OK because the depth is bounded by LOG<sub>2</sub> of the range. That's not true if the user changes his number, but then he gets what he deserves.
 
<langsyntaxhighlight lang="javascript">#!/usr/bin/env js
 
var DONE = RIGHT = 0, HIGH = 1, LOW = -1;
Line 1,713 ⟶ 1,824:
 
main();
</syntaxhighlight>
</lang>
 
An example session:
Line 1,740 ⟶ 1,851:
Is it 1? h
I can't guess it. Perhaps you changed your number.
 
=={{header|jq}}==
''Adapted from [[#Wren|Wren]]''
{{works with|jq}}
'''Also works with gojq, the Go implementation of jq'''
<syntaxhighlight lang=jq>
# Pick an integer from $lowest through $highest ...
def play($lowest; $highest):
# Helper function for guessing
def prompt:
.guess = (((.lowest + .highest)/2)|floor)
| .emit += "\nMy guess is \(.guess)." +
"\nIs this higher/lower than or equal to your chosen number? h/l/e : " ;
"Please choose a number from \($lowest) to \($highest) inclusive and then answer the questions.",
( {$highest, $lowest}
| prompt
| .emit,
( label $out
| foreach inputs as $in (.;
.emit = null
| .hle = ($in|ascii_downcase)
| if .hle == "l" and .guess == .highest
then .emit = "It can't be more than \(highest), try again."
elif .hle == "h" and .guess == .lowest
then .emit = "It can't be less than \(.lowest), try again."
elif .hle == "e"
then .emit = "Good, thanks for playing the game with me!"
| .quit = true
elif .hle == "h"
then if (.highest > .guess - 1) then .highest = .guess - 1
else .
end
elif .hle == "l"
then if (.lowest < .guess + 1) then .lowest = .guess + 1
else .
end
else .emit = "Please try again.\n"
end
| if .quit then ., break $out else prompt end ;
.emit
) ) ) ;
 
def play: play(1;20);
 
play
</syntaxhighlight>
'''Invocation:''' jq -nRr -f guess-the-number-with-feedback-player.jq
{{output}}
<pre>
Please choose a number from 1 to 20 inclusive and then answer the questions.
 
My guess is 10.
Is this higher/lower than or equal to your chosen number? h/l/e :
h
 
My guess is 5.
Is this higher/lower than or equal to your chosen number? h/l/e :
?
Please try again.
 
My guess is 5.
Is this higher/lower than or equal to your chosen number? h/l/e :
0
Please try again.
 
My guess is 5.
Is this higher/lower than or equal to your chosen number? h/l/e :
h
 
My guess is 2.
Is this higher/lower than or equal to your chosen number? h/l/e :
e
Good, thanks for playing the game with me!
$
</pre>
 
=={{header|Julia}}==
Line 1,745 ⟶ 1,932:
This version uses the expression <math>\left\lceil-\log\left(\frac{1}{\mathrm{upper} - \mathrm{lower}}\right) / \log(2)\right\rceil</math> to calculate the maximum number of guesses it will need.
 
<langsyntaxhighlight Julialang="julia">print("Enter an upper bound: ")
lower = 0
input = readline()
Line 1,792 ⟶ 1,979:
end
 
println("\nI win after ", attempts, attempts == 1 ? " attempt." : " attempts.")</langsyntaxhighlight>
 
=={{header|Kotlin}}==
{{trans|FreeBASIC}}
<langsyntaxhighlight lang="scala">// version 1.0.5-2
 
fun main(args: Array<String>) {
Line 1,830 ⟶ 2,017:
guess = (lowest + highest) / 2
}
}</langsyntaxhighlight>
Sample input/output:
{{out}}
Line 1,849 ⟶ 2,036:
 
=={{header|Lasso}}==
<langsyntaxhighlight Lassolang="lasso">#!/usr/bin/lasso9
 
local(
Line 1,898 ⟶ 2,085:
}
}
}</langsyntaxhighlight>
 
Examples:
Line 1,933 ⟶ 2,120:
 
=={{header|Liberty BASIC}}==
<langsyntaxhighlight lang="lb">mini=0
maxi=100
 
Line 1,962 ⟶ 2,149:
end select
wend
print "Thanks for playing." </langsyntaxhighlight>
 
=={{header|Lua}}==
<syntaxhighlight lang="text">function wait(waittime)--wait function is used so that app will not quit immediately
local timer = os.time()
repeat until os.time() == timer + waittime
Line 2,000 ⟶ 2,187:
end
end
</syntaxhighlight>
</lang>
 
=={{header|Mathematica}} / {{header|Wolfram Language}}==
<syntaxhighlight lang="text">guessnumber[min0_, max0_] :=
DynamicModule[{min = min0, max = max0, guess, correct = False},
guess[] := Round@Mean@{min, max};
Line 2,011 ⟶ 2,198:
Button["too low", min = guess[]],
Button["correct", correct = True]}}]];
guessnumber[1, 100]</langsyntaxhighlight>
 
=={{header|MATLAB}}==
<langsyntaxhighlight MATLABlang="matlab">function GuessNumberFeedbackPlayer
lowVal = input('Lower limit: ');
Line 2,043 ⟶ 2,230:
end
end
end</langsyntaxhighlight>
{{out}}
<pre>Lower limit: 0
Line 2,066 ⟶ 2,253:
 
=={{header|MAXScript}}==
<langsyntaxhighlight MAXScriptlang="maxscript">inclusiveRange = [1,100]
lowRange = inclusiveRange.x
maxRange = inclusiveRange.y
Line 2,091 ⟶ 2,278:
default: (format "\nI don't understand your input.")
)
)</langsyntaxhighlight>
{{out}}
<syntaxhighlight lang="maxscript">
<lang MAXScript>
OK
Think of a number between 1 and 100 and I will try to guess it.
Line 2,115 ⟶ 2,302:
Yay. I guessed your number after 6 tries.
OK
</syntaxhighlight>
</lang>
 
=={{header|Modula-2}}==
<langsyntaxhighlight lang="modula2">MODULE raden;
 
IMPORT InOut;
Line 2,153 ⟶ 2,340:
InOut.WriteString ("Thanks for letting me play with you.");
InOut.WriteLn
END raden.</langsyntaxhighlight>Example:<pre>raden
Choose a number between 0 and 1000.
 
Line 2,170 ⟶ 2,357:
 
=={{header|Nim}}==
<langsyntaxhighlight lang="nim">import strutils
 
let oRange = 1..10
Line 2,198 ⟶ 2,385:
break
 
echo "Thanks for keeping score."</langsyntaxhighlight>
Output:
<pre>Think of a number between 1 and 10 and wait for me to guess it.
Line 2,211 ⟶ 2,398:
 
=={{header|NS-HUBASIC}}==
<langsyntaxhighlight NSlang="ns-HUBASIChubasic">10 PRINT "THINK OF A NUMBER BETWEEN 1 AND";" 9, AND I'LL TRY TO GUESS IT."
20 ISVALID=0
30 GUESS=5
Line 2,220 ⟶ 2,407:
80 IF ANSWER$="EQUAL TO" THEN PRINT "OH YES! I GUESSED CORRECTLY!":END
90 IF ISVALID=0 THEN PRINT "SORRY";", BUT THAT ANSWER IS INVALID."
100 GOTO 40</langsyntaxhighlight>
 
=={{header|Objective-C}}==
A clever solution that uses the built-in binary search functions with a virtual list.
{{trans|Java}}
<langsyntaxhighlight lang="objc">#import <Foundation/Foundation.h>
 
@interface GuessNumberFakeArray : NSArray {
Line 2,281 ⟶ 2,468:
}
return 0;
}</langsyntaxhighlight>
 
=={{header|PARI/GP}}==
 
<langsyntaxhighlight lang="parigp">
guessnumber2(b)={
my(c=0,d=b,a=0);
Line 2,308 ⟶ 2,495:
);
}
</syntaxhighlight>
</lang>
 
=={{header|Pascal}}==
<langsyntaxhighlight lang="pascal">Program GuessIt(input, output);
 
var
Line 2,342 ⟶ 2,529:
writeln ('So the number is: ', guess:4);
writeln ('It was nice to play with you.');
end.</langsyntaxhighlight>
Output:
<pre>:> ./GuessTheNumberPlayerFeedback
Line 2,361 ⟶ 2,548:
 
=={{header|Perl}}==
<langsyntaxhighlight lang="perl">#!/usr/bin/perl
my $min = 1;
Line 2,396 ⟶ 2,583:
 
} while(1);
}</langsyntaxhighlight>
<pre>=>> Think of a number between 1 and 99 and I'll guess it!
 
Line 2,419 ⟶ 2,606:
 
=={{header|Phix}}==
<!--<syntaxhighlight lang="phix">(phixonline)-->
<lang Phix>integer Min=0, Max=100, Guess, Response
<span style="color: #000080;font-style:italic;">--
printf(1,"Think of a number between %d and %d.\n",{Min,Max})
-- demo\rosetta\Guess_the_number2.exw
while 1 do
--</span>
Guess = floor((Max+Min)/2)
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span> <span style="color: #000080;font-style:italic;">-- (spacing not (yet) great...)</span>
printf(1,"My guess is %d, is this too high, too low, or correct? (H/L/C)", Guess)
<span style="color: #008080;">include</span> <span style="color: #000000;">pGUI</span><span style="color: #0000FF;">.</span><span style="color: #000000;">e</span>
Response = lower(wait_key())
<span style="color: #004080;">Ihandle</span> <span style="color: #000000;">lbl</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">guess</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">toohigh</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">too_low</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">correct</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">newgame</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">dlg</span>
puts(1,"\n")
if Response='h' then
<span style="color: #004080;">integer</span> <span style="color: #000000;">Min</span><span style="color: #0000FF;">=</span><span style="color: #000000;">0</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">Max</span><span style="color: #0000FF;">=</span><span style="color: #000000;">100</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">Guess</span>
Max = Guess-1
<span style="color: #008080;">constant</span> <span style="color: #000000;">LTHINK</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">sprintf</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"Think of a number between %d and %d."</span><span style="color: #0000FF;">,{</span><span style="color: #000000;">Min</span><span style="color: #0000FF;">,</span><span style="color: #000000;">Max</span><span style="color: #0000FF;">})</span>
elsif Response='l' then
Min = Guess+1
<span style="color: #008080;">procedure</span> <span style="color: #000000;">set_active</span><span style="color: #0000FF;">(</span><span style="color: #004080;">bool</span> <span style="color: #000000;">bActive</span><span style="color: #0000FF;">)</span>
elsif Response='c' then
<span style="color: #7060A8;">IupSetInt</span><span style="color: #0000FF;">({</span><span style="color: #000000;">toohigh</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">too_low</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">correct</span><span style="color: #0000FF;">},</span><span style="color: #008000;">"ACTIVE"</span><span style="color: #0000FF;">,</span><span style="color: #000000;">bActive</span><span style="color: #0000FF;">)</span>
puts(1,"I did it!\n")
<span style="color: #7060A8;">IupSetInt</span><span style="color: #0000FF;">(</span><span style="color: #000000;">newgame</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"ACTIVE"</span><span style="color: #0000FF;">,</span><span style="color: #008080;">not</span> <span style="color: #000000;">bActive</span><span style="color: #0000FF;">)</span>
exit
<span style="color: #008080;">end</span> <span style="color: #008080;">procedure</span>
else
puts(1,"I do not understand that...\n")
<span style="color: #008080;">procedure</span> <span style="color: #000000;">set_guess</span><span style="color: #0000FF;">()</span>
end if
<span style="color: #000000;">Guess</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">floor</span><span style="color: #0000FF;">((</span><span style="color: #000000;">Max</span><span style="color: #0000FF;">+</span><span style="color: #000000;">Min</span><span style="color: #0000FF;">)/</span><span style="color: #000000;">2</span><span style="color: #0000FF;">)</span>
if Max<Min then
<span style="color: #004080;">string</span> <span style="color: #000000;">title</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">sprintf</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"My guess is %d, is this too high, too low, or correct?"</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">Guess</span><span style="color: #0000FF;">)</span>
puts(1,"I think something is strange here...\n")
<span style="color: #008080;">if</span> <span style="color: #000000;">Max</span><span style="color: #0000FF;"><</span><span style="color: #000000;">Min</span> <span style="color: #008080;">then</span>
exit
<span style="color: #000000;">set_active</span><span style="color: #0000FF;">(</span><span style="color: #004600;">false</span><span style="color: #0000FF;">)</span>
end if
<span style="color: #000000;">title</span> <span style="color: #0000FF;">=</span> <span style="color: #008000;">"I think something is strange here..."</span>
end while</lang>
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #7060A8;">IupSetStrAttribute</span><span style="color: #0000FF;">(</span><span style="color: #000000;">guess</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"TITLE"</span><span style="color: #0000FF;">,</span><span style="color: #000000;">title</span><span style="color: #0000FF;">)</span>
<span style="color: #7060A8;">IupRefresh</span><span style="color: #0000FF;">(</span><span style="color: #000000;">guess</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">procedure</span>
<span style="color: #008080;">function</span> <span style="color: #000000;">click_cb</span><span style="color: #0000FF;">(</span><span style="color: #004080;">Ihandle</span> <span style="color: #000000;">ih</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">switch</span> <span style="color: #7060A8;">substitute</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">IupGetAttribute</span><span style="color: #0000FF;">(</span><span style="color: #000000;">ih</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"TITLE"</span><span style="color: #0000FF;">),</span><span style="color: #008000;">"Too "</span><span style="color: #0000FF;">,</span><span style="color: #008000;">""</span><span style="color: #0000FF;">)[</span><span style="color: #000000;">1</span><span style="color: #0000FF;">]</span> <span style="color: #008080;">do</span>
<span style="color: #008080;">case</span> <span style="color: #008000;">'H'</span><span style="color: #0000FF;">:</span> <span style="color: #000000;">Max</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">Guess</span><span style="color: #0000FF;">-</span><span style="color: #000000;">1</span> <span style="color: #000000;">set_guess</span><span style="color: #0000FF;">()</span>
<span style="color: #008080;">case</span> <span style="color: #008000;">'L'</span><span style="color: #0000FF;">:</span> <span style="color: #000000;">Min</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">Guess</span><span style="color: #0000FF;">+</span><span style="color: #000000;">1</span> <span style="color: #000000;">set_guess</span><span style="color: #0000FF;">()</span>
<span style="color: #008080;">case</span> <span style="color: #008000;">'C'</span><span style="color: #0000FF;">:</span> <span style="color: #7060A8;">IupSetStrAttribute</span><span style="color: #0000FF;">(</span><span style="color: #000000;">guess</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"TITLE"</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"I did it!"</span><span style="color: #0000FF;">)</span>
<span style="color: #000000;">set_active</span><span style="color: #0000FF;">(</span><span style="color: #004600;">false</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">case</span> <span style="color: #008000;">'N'</span><span style="color: #0000FF;">:</span> <span style="color: #000000;">Min</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">0</span> <span style="color: #000000;">Max</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">100</span> <span style="color: #000000;">set_guess</span><span style="color: #0000FF;">()</span>
<span style="color: #000000;">set_active</span><span style="color: #0000FF;">(</span><span style="color: #004600;">true</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">switch</span>
<span style="color: #008080;">return</span> <span style="color: #004600;">IUP_DEFAULT</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
<span style="color: #008080;">procedure</span> <span style="color: #000000;">main</span><span style="color: #0000FF;">()</span>
<span style="color: #7060A8;">IupOpen</span><span style="color: #0000FF;">()</span>
<span style="color: #000000;">lbl</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">IupLabel</span><span style="color: #0000FF;">(</span><span style="color: #000000;">LTHINK</span><span style="color: #0000FF;">)</span>
<span style="color: #000000;">guess</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">IupLabel</span><span style="color: #0000FF;">(</span><span style="color: #008000;">""</span><span style="color: #0000FF;">)</span>
<span style="color: #000000;">toohigh</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">IupButton</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"Too High"</span><span style="color: #0000FF;">,</span> <span style="color: #7060A8;">Icallback</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"click_cb"</span><span style="color: #0000FF;">))</span>
<span style="color: #000000;">too_low</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">IupButton</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"Too Low"</span><span style="color: #0000FF;">,</span> <span style="color: #7060A8;">Icallback</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"click_cb"</span><span style="color: #0000FF;">))</span>
<span style="color: #000000;">correct</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">IupButton</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"Correct"</span><span style="color: #0000FF;">,</span> <span style="color: #7060A8;">Icallback</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"click_cb"</span><span style="color: #0000FF;">))</span>
<span style="color: #000000;">newgame</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">IupButton</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"New Game"</span><span style="color: #0000FF;">,</span> <span style="color: #7060A8;">Icallback</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"click_cb"</span><span style="color: #0000FF;">),</span> <span style="color: #008000;">"ACTIVE=NO"</span><span style="color: #0000FF;">)</span>
<span style="color: #000000;">dlg</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">IupDialog</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">IupVbox</span><span style="color: #0000FF;">({</span><span style="color: #000000;">lbl</span><span style="color: #0000FF;">,</span>
<span style="color: #000000;">guess</span><span style="color: #0000FF;">,</span>
<span style="color: #7060A8;">IupHbox</span><span style="color: #0000FF;">({</span><span style="color: #000000;">toohigh</span><span style="color: #0000FF;">,</span><span style="color: #000000;">too_low</span><span style="color: #0000FF;">,</span><span style="color: #000000;">correct</span><span style="color: #0000FF;">,</span><span style="color: #000000;">newgame</span><span style="color: #0000FF;">},</span>
<span style="color: #008000;">"GAP=10"</span><span style="color: #0000FF;">)},</span>
<span style="color: #008000;">`NMARGIN=15x15,GAP=10`</span><span style="color: #0000FF;">),</span>
<span style="color: #008000;">`MINSIZE=300x100,TITLE="Guess the number2"`</span><span style="color: #0000FF;">)</span>
<span style="color: #000000;">set_guess</span><span style="color: #0000FF;">()</span>
<span style="color: #7060A8;">IupShow</span><span style="color: #0000FF;">(</span><span style="color: #000000;">dlg</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">if</span> <span style="color: #7060A8;">platform</span><span style="color: #0000FF;">()!=</span><span style="color: #004600;">JS</span> <span style="color: #008080;">then</span>
<span style="color: #7060A8;">IupMainLoop</span><span style="color: #0000FF;">()</span>
<span style="color: #7060A8;">IupClose</span><span style="color: #0000FF;">()</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">procedure</span>
<span style="color: #000000;">main</span><span style="color: #0000FF;">()</span>
<!--</syntaxhighlight>-->
 
=={{header|PicoLisp}}==
{{trans|PureBasic}}
<langsyntaxhighlight PicoLisplang="picolisp">(de guessTheNumber (Min Max)
(prinl "Think of a number between " Min " and " Max ".")
(prinl "On every guess of mine you should state whether my guess was")
Line 2,462 ⟶ 2,690:
("L" (setq Min Guess))
("=" (nil (prinl "I did it!")))
(T (prinl "I do not understand that...")) ) ) ) ) )</langsyntaxhighlight>
Output:
<pre>: (guessTheNumber 1 99)
Line 2,476 ⟶ 2,704:
 
=={{header|Prolog}}==
<langsyntaxhighlight lang="prolog">min(1). max(10).
 
pick_number(Min, Max) :-
Line 2,497 ⟶ 2,725:
play :-
pick_number(Min, Max),
guess_number(Min, Max).</langsyntaxhighlight>
 
Example:
 
<langsyntaxhighlight lang="prolog">?- play.
Pick a number between 1 and 10, and I will guess it...
Ready? (Enter anything when ready):y.
Line 2,511 ⟶ 2,739:
Am I correct (c), too low (l), or too high (h)? c.
I'm correct!
true</langsyntaxhighlight>
 
=={{header|PureBasic}}==
<langsyntaxhighlight PureBasiclang="purebasic">min=0
max=100
 
Line 2,537 ⟶ 2,765:
EndIf
ForEver
EndIf</langsyntaxhighlight>
 
=={{header|Python}}==
<langsyntaxhighlight lang="python">inclusive_range = mn, mx = (1, 10)
 
print('''\
Line 2,568 ⟶ 2,796:
break
print("\nThanks for keeping score.")</langsyntaxhighlight>
 
'''Sample Game-play'''
Line 2,587 ⟶ 2,815:
Hacky solution using a fake list class and the <code>bisect</code> module from the standard library to do the binary search.
{{trans|Go}}
<langsyntaxhighlight lang="python">import bisect
try: input = raw_input
except: pass
Line 2,605 ⟶ 2,833:
""" % (LOWER, UPPER))
result = bisect.bisect_left(GuessNumberFakeList(), 0, LOWER, UPPER)
print("Your number is %d." % result)</langsyntaxhighlight>
 
;Sample output:
Line 2,626 ⟶ 2,854:
Is your number less than or equal to 71?no
Your number is 72.</pre>
 
=={{header|Quackery}}==
 
<syntaxhighlight lang="quackery">
[ [ $ "lower higher equal" nest$ ]
constant ] is responses ( --> $ )
 
[ trim reverse
trim reverse
$ "" swap witheach
[ lower join ] ] is cleanup ( $ --> $ )
 
[ $ "Think of a number from 1 to"
$ " 100 and press enter." join
input drop
0 temp put
[] 100 times [ i 1+ join ]
[ dup size 0 = iff
[ say "Impossible!" cr 0 ]
done
dup size 2 / split behead
dup temp replace
say "I guess " echo say "." cr
$ "lower, higher or equal? "
input cleanup responses find
[ table
[ nip false ]
[ drop false ]
[ say "I guessed it!"
cr true ]
[ say "I do not understand."
temp share swap join join
cr false ] ]
do until ]
2drop temp release ] is play ( --> )</syntaxhighlight>
 
{{out}}
 
Playing in the Quackery shell. In the first game the player tries to confuse the computer by giving an impossible response.
 
<pre>/O> play
...
Think of a number from 1 to 100 and press enter.
I guess 50.
lower, higher or equal? lower
I guess 25.
lower, higher or equal? HiGhEr
I guess 37.
lower, higher or equal? none of the above
I do not understand.
I guess 37.
lower, higher or equal? lower
I guess 31.
lower, higher or equal? higher
I guess 34.
lower, higher or equal? higher
I guess 35.
lower, higher or equal? higher
I guess 36.
lower, higher or equal? higher
Impossible!
 
Stack empty.
 
/O> play
...
Think of a number from 1 to 100 and press enter.
I guess 50.
lower, higher or equal? equal
I guessed it!
 
Stack empty.
 
/O> </pre>
 
=={{header|R}}==
Can be fooled if you lie to it. For example, always reporting "h" for guessANumberPlayer(1, 5) will have it guess 0.
<langsyntaxhighlight rlang="rsplus">guessANumberPlayer <- function(low, high)
{
boundryErrorCheck(low, high)
repeat
{
guess <- floor(mean(c(low, high)))
#Invalid inputs to this switch will simply cause the repeat loop to run again, breaking nothing.
switch(guessResult(guess),
l = low <- guess + 1,
h = high <- guess - 1,
c = return(paste0("Your number is ", guess, ".", " I win!")))
}
}
 
#Copied from my solution at https://rosettacode.org/wiki/Guess_the_number/With_feedback#R
boundryErrorCheck <- function(low, high)
{
if(!is.numeric(low) || as.integer(low) != low){ stop("Lower bound must be an integer. Try again.")}
if(!is.numeric(high) || as.integer(high) != high){ stop("Upper bound must be an integer. Try again.")}
if(high < low){ stop("Upper bound must be strictly greater than lower bound. Try again.")}
if(low == high){ stop("This game is impossible to lose. Try again.")}
invisible()
}
 
guessResult <- function(guess) readline(paste0("My guess is ", guess, ". If it is too low, submit l. If it is too high, h. Otherwise, c. "))</syntaxhighlight>
guessResult<-function(guess)
{
readline(paste0("My guess is ",guess,". If it is too low, submit l. If it is too high, h. Otherwise, c. "))
}</lang>
 
=={{header|Racket}}==
<langsyntaxhighlight Racketlang="racket">#lang racket
 
(define (guess low high)
Line 2,680 ⟶ 2,979:
(printf "Think of a number between ~a and ~a.
Use (h)igh, (l)ow and (c)orrect to guide me.\n" low high)
(guess-loop low high))</langsyntaxhighlight>
 
Another way with loops and mutation
<langsyntaxhighlight Racketlang="racket">#lang racket
(define (guess minimum maximum)
(printf "Think of a number from ~a to ~a, use (h)igh, (l)ow and (c)orrect." minimum maximum)
Line 2,698 ⟶ 2,997:
(begin (display "OK...")
(set! minimum (add1 guess)))))
(displayln "I was RIGHT!"))</langsyntaxhighlight>
 
=={{header|Raku}}==
(formerly Perl 6)
 
<syntaxhighlight lang="raku" perl6line>multi sub MAIN() { MAIN(0, 100) }
multi sub MAIN($min is copy where ($min >= 0), $max is copy where ($max > $min)) {
say "Think of a number between $min and $max and I'll guess it!";
Line 2,716 ⟶ 3,015:
}
say "How can your number be both higher and lower than $max?!?!?";
}</langsyntaxhighlight>
 
You may execute this program with '<tt>raku program</tt>' or with '<tt>raku program min max</tt>'. Raku creates a usage for us if we don't give the right parameters. It also parses the parameters for us and provides them via <tt>$min</tt> and <tt>$max</tt>. We use multi-subs to provide two MAIN subroutines so the user is able to choose between min and max parameters and no parameters at all, in which case min is set to 0 and max to 100.
 
=={{header|Red}}==
<syntaxhighlight lang="rebol">Red[]
 
lower: 1
upper: 100
print ["Please think of a number between" lower "and" upper]
 
forever [
guess: to-integer upper + lower / 2
print ["My guess is" guess]
until [
input: ask "Is your number (l)ower, (e)qual to, or (h)igher than my guess? "
find ["l" "e" "h"] input
]
if "e" = input [break]
either "l" = input [upper: guess] [lower: guess]
]
 
print ["I did it! Your number was" guess]</syntaxhighlight>
{{out}}
<pre>
Please think of a number between 1 and 100
My guess is 50
Is your number (l)ower, (e)qual to, or (h)igher than my guess? car
Is your number (l)ower, (e)qual to, or (h)igher than my guess? l
My guess is 25
Is your number (l)ower, (e)qual to, or (h)igher than my guess? h
My guess is 37
Is your number (l)ower, (e)qual to, or (h)igher than my guess? l
My guess is 31
Is your number (l)ower, (e)qual to, or (h)igher than my guess? h
My guess is 34
Is your number (l)ower, (e)qual to, or (h)igher than my guess? e
I did it! Your number was 34
</pre>
 
=={{header|REXX}}==
===with positive integers===
This version only handles positive integers up to the nine decimal digits, &nbsp; but the default HIGH is one thousand.
<langsyntaxhighlight lang="rexx">/*REXX program plays guess─the─number (with itself) with positive integers. */
parse arg low high seed . /*obtain optional arguments from the CL*/
if low=='' | low=="," then low= 1 /*Not specified? Then use the default.*/
Line 2,746 ⟶ 3,081:
end /*try*/
say /*stick a fork in it, we're all done. */
say 'Congratulations! You guessed the secret number in' # "tries."</langsyntaxhighlight>
'''output''' shown is from playing several games:
<pre style="height:70ex">
Line 2,883 ⟶ 3,218:
=== with positive numbers ===
This version handles decimal fractions, &nbsp; the method used can generate numbers from zero to five fractional digits.
<langsyntaxhighlight lang="rexx">/*REXX program plays guess─the─number (with itself) with positive rational numbers. */
parse arg low high frac seed . /*obtain optional arguments from the CL*/
if low=='' | low=="," then low= 1 /*Not specified? Then use the default.*/
Line 2,908 ⟶ 3,243:
end /*try*/
say /*stick a fork in it, we're all done. */
say 'Congratulations! You guessed the secret number in' # "tries.""</langsyntaxhighlight>
'''output''' will generally be about ten times longer (that is, has ten times the guesses) as the previous REXX version.
<pre style="height:35ex">
Line 3,059 ⟶ 3,394:
 
=={{header|Ring}}==
<langsyntaxhighlight lang="ring">
min = 1
max = 100
Line 3,077 ⟶ 3,412:
end
see "goodbye." + nl
</syntaxhighlight>
</lang>
Output:
<pre>
Line 3,099 ⟶ 3,434:
=={{header|Ruby}}==
Computer plays versus itself
<langsyntaxhighlight lang="ruby">def play(low, high, turns=1)
num = (low + high) / 2
print "guessing #{num}\t"
Line 3,122 ⟶ 3,457:
 
puts "guess a number between #{low} and #{high}"
play(low, high)</langsyntaxhighlight>
 
Example
Line 3,135 ⟶ 3,470:
 
Since Ruby 2.0 it can be done actually using a built-in binary search (output as above):
<langsyntaxhighlight lang="ruby">r = (1..100)
secret = rand(r)
turns = 0
Line 3,147 ⟶ 3,482:
low_high
end
</syntaxhighlight>
</lang>
 
=={{header|Rust}}==
{{works with|rustc|1.0.0-nightly}}
<langsyntaxhighlight lang="rust">use std::io::stdin;
 
const MIN: isize = 1;
Line 3,191 ⟶ 3,526:
}
}
}</langsyntaxhighlight>
 
=={{header|Scala}}==
<langsyntaxhighlight Scalalang="scala">object GuessNumber extends App {
val n = 1 + scala.util.Random.nextInt(20)
 
Line 3,206 ⟶ 3,541:
})
 
}</langsyntaxhighlight>
 
=={{header|Scheme}}==
{{works with|Guile}}
<langsyntaxhighlight lang="scheme">(define minimum 1) (define maximum 100)
 
(display "Enter a number from ")(display minimum)
Line 3,226 ⟶ 3,561:
(if (string= input "l") (begin (display "OK...")
(set! minimum (+ guess 1)))))
(display "I was RIGHT!\n")</langsyntaxhighlight>
 
=={{header|Seed7}}==
Line 3,236 ⟶ 3,571:
and the command is not c.
 
<langsyntaxhighlight lang="seed7">$ include "seed7_05.s7i";
$ include "keybd.s7i";
 
Line 3,274 ⟶ 3,609:
end if;
writeln("It was nice to play with you.");
end func;</langsyntaxhighlight>
 
Example
Line 3,294 ⟶ 3,629:
 
=={{header|Sidef}}==
<langsyntaxhighlight lang="ruby">var min = 1
var max = 99
var tries = 0
Line 3,335 ⟶ 3,670:
 
guess = ((min+max) // 2)
}</langsyntaxhighlight>
 
=={{header|Smalltalk}}==
Line 3,343 ⟶ 3,678:
Create a class like this:
 
<langsyntaxhighlight lang="smalltalk">Object subclass: #NumberGuesser
instanceVariableNames: ''
classVariableNames: ''
poolDictionaries: ''
category: 'Rosettacode'
</syntaxhighlight>
</lang>
Enter these methods into it:
 
<langsyntaxhighlight lang="smalltalk">playBetween: low and: high
Transcript clear.
number := (low to: high) atRandom.
Line 3,377 ⟶ 3,712:
showNumberFoundAtTurnNr: turn
Transcript show: (' ==> found the number in {1} turn(s).' format: {turn asString}); cr.
</syntaxhighlight>
</lang>
 
If you'd rather not use <code>caseOf:</code> you can write <code>playBetween:and:atTurn:</code> like this:
 
<langsyntaxhighlight lang="smalltalk">
playBetween: low and: high atTurn: turn
| guess |
Line 3,392 ⟶ 3,727:
ifTrue: [self showNumberBeing: #low. self playBetween: guess and: high atTurn: turn+1 ]
ifFalse: [self showNumberBeing: #high. self playBetween: low and: guess atTurn: turn+1]].
</syntaxhighlight>
</lang>
 
And evaluate this in a Workspace:
 
<langsyntaxhighlight lang="smalltalk">g := NumberGuesser new.
g playBetween: 1 and: 100.
</syntaxhighlight>
</lang>
 
Sample output:
Line 3,415 ⟶ 3,750:
{{works with|SML/NJ}}
{{trans|Go}}
<langsyntaxhighlight lang="sml">structure GuessNumberHelper : MONO_ARRAY = struct
type elem = order
type array = int * int
Line 3,472 ⟶ 3,807:
case GuessNumberBSearch.bsearch (fn (_, x) => x) ((), (lower, upper)) of
NONE => print "That is impossible.\n"
| SOME (result, _) => print ("Your number is " ^ Int.toString result ^ ".\n")</langsyntaxhighlight>
 
=={{header|Swift}}==
<syntaxhighlight lang="text">import Cocoa
 
var found = false
Line 3,522 ⟶ 3,857:
high = lastGuess
}
}</langsyntaxhighlight>
{{out}}
<pre>
Line 3,549 ⟶ 3,884:
 
=={{header|Tcl}}==
<langsyntaxhighlight lang="tcl">set from 1
set to 10
fconfigure stdout -buffering none
Line 3,570 ⟶ 3,905:
break
}
}</langsyntaxhighlight>
Sample run:
<pre>
Line 3,581 ⟶ 3,916:
=={{header|UNIX Shell}}==
{{works with|bash}}
<langsyntaxhighlight lang="bash">read -p "Lower bound: " lower
read -p "Upper bound: " upper
moves=0
Line 3,598 ⟶ 3,933:
((lower>upper)) && echo "you must be cheating!"
done
echo "I guessed it in $moves guesses"</langsyntaxhighlight>
 
=={{header|VBA}}==
 
<langsyntaxhighlight lang="vba">
Sub GuessNumberPlayer()
Dim iGuess As Integer, iLow As Integer, iHigh As Integer, iCount As Integer
Line 3,631 ⟶ 3,966:
MsgBox "Not possible. Were you cheating?!", vbCritical + vbOKOnly, "Rosetta Code | Guess the Number Player | ERROR!"
End Sub
</syntaxhighlight>
</lang>
 
=={{header|Wren}}==
{{trans|Kotlin}}
{{libheader|Wren-str}}
<langsyntaxhighlight ecmascriptlang="wren">import "io" for Stdin, Stdout
import "./str" for Char
 
var hle
Line 3,670 ⟶ 4,005:
}
guess = ((lowest + highest)/2).floor
}</langsyntaxhighlight>
 
{{out}}
Line 3,687 ⟶ 4,022:
 
=={{header|XPL0}}==
<langsyntaxhighlight XPL0lang="xpl0">include c:\cxpl\codes; \intrinsic 'code' declarations
int Hi, Lo, Guess;
[Text(0, "Think of a number between 1 and 100 then press a key.");
Line 3,702 ⟶ 4,037:
];
Text(0, "^M^JYippee!");
]</langsyntaxhighlight>
 
Example output:
Line 3,718 ⟶ 4,053:
=={{header|zkl}}==
Your basic binary search.
<langsyntaxhighlight lang="zkl">println("Pick a number between 0 and 100 and remember it.");
low,high,g := 0,100, 20;
while(True){
Line 3,727 ⟶ 4,062:
if(low>high){ println("I'm confused!"); break; }
g=(low + high)/2;
}</langsyntaxhighlight>
{{out}}
<pre>
Line 3,742 ⟶ 4,077:
=={{header|ZX Spectrum Basic}}==
{{trans|BBC_BASIC}}
<langsyntaxhighlight lang="zxbasic">10 LET min=1: LET max=100
20 PRINT "Think of a number between ";min;" and ";max
30 PRINT "I will try to guess your number."
Line 3,752 ⟶ 4,087:
80 IF a$="H" OR a$="h" THEN LET max=guess-1: GO TO 40
90 IF a$="E" OR a$="e" THEN PRINT "Goodbye.": STOP
100 PRINT "Sorry, I didn't understand your answer.": GO TO 60</langsyntaxhighlight>
 
{{omit from|GUISS}}
1,969

edits