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

Content added Content deleted
Line 1,831: Line 1,831:
<lang rust>use std::io::stdio::stdin;
<lang rust>use std::io::stdio::stdin;
use std::str;
use std::str;

fn main() {
fn main() {
let mut low = 1;
let mut low = 1;
Line 1,838: Line 1,838:
let mut number_of_guesses = 1;
let mut number_of_guesses = 1;
let mut stdin = stdin();
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!("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");
println("Got it? Good. Now I shall guess it using only the power of my mind.\n");
loop {
loop {
println!("My guess is {:d}. Is that too [h]igh, too [l]ow or is it [e]qual to it? ", current_guess);
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: Line 1,860:
break;
break;
}
}
"l" | "L" => {
"l" | "L" => take_a_guess(current_guess, high),
low = current_guess;
"h" | "H" => 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")
_ => println("sorry, I didn't quite get that")

}
}
if low == high {
if low == high {
Line 1,876: Line 1,875:
}
}
}
}

fn calculate_guess(low: int, high: int) -> int {
fn calculate_guess(low: int, high: int) -> int {
(low + high) / 2
(low + high) / 2
}</lang>
}
</lang>


=={{header|Scheme}}==
=={{header|Scheme}}==