Anonymous user
Guess the number/With feedback (player): Difference between revisions
Guess the number/With feedback (player) (view source)
Revision as of 01:24, 26 April 2014
, 10 years ago→{{header|Rust}}
(→{{header|Ruby}}: Got rid of one variable and a typo) |
|||
Line 1,843:
=={{header|Rust}}==
{{works with|rustc|0.
<lang rust>use std::io::stdio::stdin;
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() {
let mut
▲ let mut number_of_guesses = 1;
let mut stdin = stdin();
println!("Hello, please choose a number between {} and {} and remember it.",
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;
▲ };
▲ 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!("
println!("(please type h, l or e and then hit enter) (or type q to quit)");
let
Some(s) => s.trim().to_owned(),
None => ~""
};
match answer_str.as_slice() {
"e" | "E" => {
println!("
break;
}
"l" | "L" =>
"h" | "H" =>
_ => 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!", current_state.low);
break;
}
if current_state.low > current_state.high {
println!("Uh, I think something went wrong. Entirely my fault, I'm sure!");
break;
}
}
▲}
}</lang>
|