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. |
{{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, |
|||
⚫ | |||
⚫ | |||
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 { |
|||
⚫ | |||
⚫ | |||
current_guess: new_guess, |
|||
⚫ | |||
}; |
|||
} |
|||
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, |
|||
⚫ | |||
current_guess: new_guess, |
|||
number_of_guesses: self.number_of_guesses + 1 |
|||
}; |
|||
} |
|||
} |
|||
fn main() { |
fn main() { |
||
let mut |
let mut current_state = GameState::new(); |
||
let mut high = 100; |
|||
let mut current_guess = calculate_guess(low, high); |
|||
⚫ | |||
let mut stdin = stdin(); |
let mut stdin = stdin(); |
||
⚫ | |||
let take_a_guess = |new_low: int, new_high: int| { |
|||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
current_guess = calculate_guess(low, high); |
|||
⚫ | |||
⚫ | |||
⚫ | |||
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(" |
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 |
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!(" |
println!("Woot, I got it in only {} tries!", current_state.number_of_guesses + 1); |
||
break; |
break; |
||
} |
} |
||
"l" | "L" => |
"l" | "L" => current_state = current_state.guess_higher(), |
||
"h" | "H" => |
"h" | "H" => current_state = current_state.guess_lower(), |
||
"q" | "Q" => break, |
|||
_ => println!("sorry, I didn't quite get that") |
|||
} |
} |
||
⚫ | |||
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> |
||