Files
lux/examples/standard/guessing_game.lux
2026-02-17 06:52:44 -05:00

20 lines
902 B
Plaintext

fn checkGuess(guess: Int, secret: Int): String = if guess == secret then "Correct" else if guess < secret then "Too low" else "Too high"
fn binarySearch(low: Int, high: Int, secret: Int, attempts: Int): Int with {Console} = {
let mid = low + high / 2
let result = checkGuess(mid, secret)
Console.print("Guess " + toString(mid) + ": " + result + "!")
if result == "Correct" then attempts else if result == "Too low" then binarySearch(mid + 1, high, secret, attempts + 1) else binarySearch(low, mid - 1, secret, attempts + 1)
}
fn main(): Unit with {Console} = {
Console.print("Welcome to the Guessing Game!")
let secret = 42
Console.print("Target number: " + toString(secret))
Console.print("Simulating guesses...")
let attempts = binarySearch(1, 100, secret, 1)
Console.print("Found in " + toString(attempts) + " attempts!")
}
let output = run main() with {}