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

Added FreeBASIC
(added ceylon)
(Added FreeBASIC)
Line 855:
My guess is: 57 Score(h, l or c)?: c
I solved it!</pre>
 
=={{header|FreeBASIC}}==
<lang freebasic>' FB 1.05.0 Win64
 
Dim hle As String
Dim lowest As Integer = 1
Dim highest As Integer = 20
Dim guess As Integer = 10
Print "Please choose a number between 1 and 20 but don't tell me what it is yet"
Print
Do
Print "My guess is"; guess
Do
Input "Is this higher/lower or equal to your chosen number h/l/e : "; hle
hle = LCase(hle)
If hle = "l" AndAlso guess = highest Then
Print "It can't be more than"; highest; ", try again"
hle = "i" '' invalid
ElseIf hle = "h" AndAlso guess = lowest Then
Print "It can't be less than"; lowest; ", try again"
hle = "i"
End If
Loop Until hle = "h" OrElse hle = "l" OrElse hle = "e"
If hle = "e" Then
Print "Good, thanks for playing the gaame with me!"
Exit Do
ElseIf hle = "h" Then
If highest > guess - 1 Then highest = guess - 1
Else
If lowest < guess + 1 Then lowest = guess + 1
End If
guess = (lowest + highest)\2
Loop
End</lang>
 
Sample input/output
{{out}}
<pre>
Please choose a number between 1 and 20 but don't tell me what it is yet
 
My guess is 10
Is this higher/lower or equal to your chosen number h/l/e : ? l
My guess is 15
Is this higher/lower or equal to your chosen number h/l/e : ? h
My guess is 12
Is this higher/lower or equal to your chosen number h/l/e : ? h
My guess is 11
Is this higher/lower or equal to your chosen number h/l/e : ? e
Good, thanks for playing the gaame with me!
</pre>
 
=={{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]
9,476

edits