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

m
Line 1,974:
 
=={{header|Rust}}==
{{works with|rustc|0.1112-pre-nightly}}
<lang rust>use std::io::stdio::stdin;
use std::ascii::AsciiExt;
 
static INITIAL_LOWMIN: int = 1;
static INITIAL_HIGHMAX: 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() {
let mut current_statestdin = GameState::newstdin();
loop {
let mut stdinmin = stdin()MIN;
let mut max = MAX;
let mut num_guesses = 1i;
println!("Hello, please choose a number between {} and {} and remember it.", current_state.high, current_state.low);
println!("Please think of a number between {} and {}", min, max);
println!("Got it? Good. Now I shall guess it using only the power of my mind.\n");
} loop {
let guess = (min + max) / 2;
loop {
println!("Is it {}?", guess);
println!("My(type guessh isif {:d}.my Isguess thatis too [h]ighhigh, too [l]ow orif istoo itlow, [e]qual toif it?equal ",and current_state.current_guessq to quit)");
break;
println!("(please type h, l or e and then hit enter) (or type q to quit)");
match stdin.read_line().ok().and_then(|line| line.as_slice().trim().to_ascii_upper().shift_char()) {
let answer_str = match stdin.read_line().ok Some('H') => {
max = guess - 1;
Some(s) => s.trim().to_owned(),
None num_guesses +=> ~""1;
};,
Some('L') => {
min = guess + 1;
match answer_str.as_slice() {
"e" | "E" num_guesses +=> {1;
"q" | "Q" => break },
println!("Woot, I got it in only {} tries!", current_state.number_of_guesses + 1);
break; Some('E') => {
if num_guesses == 1 {
}
println!("\n*** That was easy! Got it in one guess! ***\n");
"l" | "L" => current_state = current_state.guess_higher(),
} else {
"h" | "H" => current_state = current_state.guess_lower(),
println!("\n*** I knew it! Got it in only {} guesses! ***\n", num_guesses);
"q" | "Q" => break,
_ => println!("sorry, I didn't quite get that")}
break;
},
Some('Q') => return,
_ => println!("Sorry, I didn't quite get that. Please try again.")
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>
 
Anonymous user