Guess the number/With feedback (player): Difference between revisions
Content added Content deleted
(→{{header|XPL0}}: added zkl) |
(Added Kotlin) |
||
Line 1,271: | Line 1,271: | ||
println("\nI win after ", attempts, attempts == 1 ? " attempt." : " attempts.")</lang> |
println("\nI win after ", attempts, attempts == 1 ? " attempt." : " attempts.")</lang> |
||
=={{header|Kotlin}}== |
|||
{{trans|FreeBASIC}} |
|||
<lang scala>// version 1.0.5-2 |
|||
fun main(args: Array<String>) { |
|||
var hle: Char |
|||
var lowest = 1 |
|||
var highest = 20 |
|||
var guess = 10 |
|||
println("Please choose a number between 1 and 20 but don't tell me what it is yet\n") |
|||
while (true) { |
|||
println("My guess is $guess") |
|||
do { |
|||
print("Is this higher/lower than or equal to your chosen number h/l/e : ") |
|||
hle = readLine()!!.first().toLowerCase() |
|||
if (hle == 'l' && guess == highest) { |
|||
println("It can't be more than $highest, try again") |
|||
hle = 'i' // signifies invalid |
|||
} |
|||
else if (hle == 'h' && guess == lowest) { |
|||
println("It can't be less than $lowest, try again") |
|||
hle = 'i' |
|||
} |
|||
} |
|||
while (hle !in "hle") |
|||
when (hle) { |
|||
'e' -> { println("Good, thanks for playing the game with me!") ; return } |
|||
'h' -> if (highest > guess - 1) highest = guess - 1 |
|||
'l' -> if (lowest < guess + 1) lowest = guess + 1 |
|||
} |
|||
guess = (lowest + highest) / 2 |
|||
} |
|||
}</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 : h |
|||
My guess is 5 |
|||
Is this higher/lower or equal to your chosen number h/l/e : l |
|||
My guess is 7 |
|||
Is this higher/lower or equal to your chosen number h/l/e : l |
|||
My guess is 8 |
|||
Is this higher/lower or equal to your chosen number h/l/e : e |
|||
Good, thanks for playing the game with me! |
|||
/pre> |
|||
=={{header|Lasso}}== |
=={{header|Lasso}}== |