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

Content added Content deleted
Line 1,831:
<lang rust>use std::io::stdio::stdin;
use std::str;
 
fn main() {
let mut low = 1;
Line 1,838:
let mut number_of_guesses = 1;
let mut stdin = stdin();
let take_a_guess = |new_low: int, new_high: int| {
low = new_low;
high = new_high;
number_of_guesses += 1;
current_guess = calculate_guess(low, high);
};
println!("Hello, please choose a number between {} and {} and remember it.", low, high);
println("Got it? Good. Now I shall guess it using only the power of my mind.\n");
loop {
println!("My guess is {:d}. Is that too [h]igh, too [l]ow or is it [e]qual to it? ", current_guess);
Line 1,853 ⟶ 1,860:
break;
}
"l" | "L" => {take_a_guess(current_guess, high),
"h" | low"H" => current_guess;take_a_guess(low, current_guess),
current_guess = calculate_guess(low, high);
number_of_guesses += 1;
}
"h" | "H" => {
high = current_guess;
current_guess = calculate_guess(low, high);
number_of_guesses += 1;
}
_ => println("sorry, I didn't quite get that")
 
}
if low == high {
Line 1,876 ⟶ 1,875:
}
}
 
fn calculate_guess(low: int, high: int) -> int {
(low + high) / 2
}</lang>
}
</lang>
 
=={{header|Scheme}}==