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

m
(→‎{{header|Ruby}}: Got rid of one variable and a typo)
Line 1,843:
 
=={{header|Rust}}==
{{works with|rustc|0.911-pre-nightly}}
<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: += 1;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 = low: new_low;,
if low ==high: self.high, {
current_guess: new_guess,
let mut 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 = high: new_high;,
current_guess: new_guess,
number_of_guesses: self.number_of_guesses + 1
};
}
}
fn main() {
let mut lowcurrent_state = 1GameState::new();
let mut high = 100;
let mut current_guess = calculate_guess(low, high);
let mut number_of_guesses = 1;
let mut stdin = stdin();
println!("Hello, please choose a number between {} and {} and remember it.", lowcurrent_state.high, highcurrent_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 {
println!("My guess is {:d}. Is that too [h]igh, too [l]ow or is it [e]qual to it? ", current_guess);
println!("(pleaseMy typeguess is {:d}. Is that too [h]igh, too [l]ow or eis andit then[e]qual hitto it? enter)", 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 answeranswer_str = str::from_utf8(buf)match stdin.slice_to(num_bytes.unwrapread_line()).trimok(); {
Some(s) => s.trim().to_owned(),
match answer {
None => ~""
};
match answer_str.as_slice() {
"e" | "E" => {
println!("Jolly good!Woot, I got it in only {} tries!", current_state.number_of_guesses + 1);
break;
}
"l" | "L" => take_a_guess(current_guess,current_state high= current_state.guess_higher(),
"h" | "H" => take_a_guess(low,current_state current_guess= current_state.guess_lower(),
_ "q" | "Q" => println("sorrybreak, I didn't quite get that")
_ => 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;
}
}
fn calculate_guess(low: int, high: int) -> int {
(low + high) / 2
}</lang>
 
Anonymous user