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

Content added Content deleted
(→‎{{header|Ruby}}: Got rid of one variable and a typo)
Line 1,843: Line 1,843:


=={{header|Rust}}==
=={{header|Rust}}==
{{works with|rustc|0.9}}
{{works with|rustc|0.11-pre-nightly}}
<lang rust>use std::io::stdio::stdin;
<lang rust>use std::io::stdio::stdin;

use std::str;
static INITIAL_LOW: int = 1;
static INITIAL_HIGH: int = 100;

struct GameState {
low: int,
high: int,
current_guess: int,
number_of_guesses: int,
}

impl GameState {
fn new() -> GameState {
return GameState {
low: INITIAL_LOW,
high: INITIAL_HIGH,
current_guess: (INITIAL_LOW + INITIAL_HIGH) / 2,
number_of_guesses: 0
};
}
fn guess_higher (&self) -> GameState {
let new_low = self.current_guess;
let new_guess = (self.current_guess + self.high) / 2;
return GameState {
low: new_low,
high: self.high,
current_guess: new_guess,
number_of_guesses: self.number_of_guesses + 1
};
}
fn guess_lower(&self) -> GameState {
let new_high = self.current_guess;
let new_guess = (self.current_guess + self.low) / 2;
return GameState {
low: self.low,
high: new_high,
current_guess: new_guess,
number_of_guesses: self.number_of_guesses + 1
};
}
}
fn main() {
fn main() {
let mut low = 1;
let mut current_state = GameState::new();
let mut high = 100;
let mut current_guess = calculate_guess(low, high);
let mut number_of_guesses = 1;
let mut stdin = stdin();
let mut stdin = stdin();
println!("Hello, please choose a number between {} and {} and remember it.", current_state.high, current_state.low);
let take_a_guess = |new_low: int, new_high: int| {
println!("Got it? Good. Now I shall guess it using only the power of my mind.\n");
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 {
loop {
println!("My guess is {:d}. Is that too [h]igh, too [l]ow or is it [e]qual to it? ", current_guess);
println("(please type h, l or e and then hit enter)");
println!("My guess is {:d}. Is that too [h]igh, too [l]ow or is it [e]qual to it? ", current_state.current_guess);
println!("(please type h, l or e and then hit enter) (or type q to quit)");
let buf: &mut [u8] = [0u8, ..8];
let num_bytes = stdin.read(buf);
let answer = str::from_utf8(buf).slice_to(num_bytes.unwrap()).trim();
let answer_str = match stdin.read_line().ok() {
Some(s) => s.trim().to_owned(),
match answer {
None => ~""
};
match answer_str.as_slice() {
"e" | "E" => {
"e" | "E" => {
println!("Jolly good! I got it in only {} tries!", number_of_guesses);
println!("Woot, I got it in only {} tries!", current_state.number_of_guesses + 1);
break;
break;
}
}
"l" | "L" => take_a_guess(current_guess, high),
"l" | "L" => current_state = current_state.guess_higher(),
"h" | "H" => take_a_guess(low, current_guess),
"h" | "H" => current_state = current_state.guess_lower(),
_ => println("sorry, I didn't quite get that")
"q" | "Q" => break,
_ => println!("sorry, I didn't quite get that")
}
}
if low == high {
if current_state.low == current_state.high {
println!("Your number must be {}. Or maybe you forgot it? It happens!", low);
println!("Your number must be {}. Or maybe you forgot it? It happens!", current_state.low);
break;
break;
}
}
if low > high {
if current_state.low > current_state.high {
println("Uh, I think something went wrong. Entirely my fault, I'm sure!");
println!("Uh, I think something went wrong. Entirely my fault, I'm sure!");
break;
break;
}
}
}
}
}
fn calculate_guess(low: int, high: int) -> int {
(low + high) / 2
}</lang>
}</lang>