parent
bcfc006259
commit
7b364c6f6f
@ -0,0 +1,21 @@ |
||||
0,1,2,Does it quack? |
||||
1,0,0,duck |
||||
2,3,4,Is it wild? |
||||
3,5,6,Does it fly? |
||||
4,15,16,Does it woof? |
||||
5,7,8,Is it colourful? |
||||
6,13,14,Does it eat acorns? |
||||
7,0,0,parrot |
||||
8,9,10,Does it have a big beak? |
||||
9,11,12,Does it swim? |
||||
10,0,0,raven |
||||
11,0,0,swan |
||||
12,0,0,eagle |
||||
13,0,0,boar |
||||
14,0,0,wolf |
||||
15,0,0,dog |
||||
16,17,18,Does it give milk? |
||||
17,19,20,Is it grown for meat? |
||||
18,0,0,pig |
||||
19,0,0,cow |
||||
20,0,0,goat |
@ -0,0 +1,76 @@ |
||||
use crate::animals; |
||||
use crate::prompt; |
||||
|
||||
pub struct CliUser<'a> { |
||||
db: &'a animals::AnimalDB, |
||||
} |
||||
|
||||
impl<'a> CliUser<'a> { |
||||
pub fn new(db: &'a animals::AnimalDB) -> Self { |
||||
CliUser { db } |
||||
} |
||||
|
||||
fn abort(&self) -> ! { |
||||
println!("Exit."); |
||||
std::process::exit(1); |
||||
} |
||||
} |
||||
|
||||
impl<'a> animals::UserAPI for CliUser<'_> { |
||||
fn notify_new_game(&self) { |
||||
println!("----- NEW GAME -----"); |
||||
} |
||||
|
||||
fn notify_game_ended(&self) { |
||||
println!("Game ended.\n"); |
||||
} |
||||
|
||||
fn notify_new_animal(&self, animal: &str) { |
||||
println!("Learned a new animal: {}", animal); |
||||
self.db.save("animals.txt").unwrap(); |
||||
} |
||||
|
||||
fn notify_victory(&self) { |
||||
println!("Yay, found an answer! Thanks for playing."); |
||||
} |
||||
|
||||
fn answer_yes_no(&self, question: &str) -> bool { |
||||
println!("{}", question); |
||||
match prompt::ask_yn("> ") { |
||||
None => self.abort(), |
||||
Some(b) => b, |
||||
} |
||||
} |
||||
|
||||
fn is_it_a(&self, animal: &str) -> bool { |
||||
println!("Is it a {}?", animal); |
||||
match prompt::ask_yn("> ") { |
||||
None => self.abort(), |
||||
Some(b) => b, |
||||
} |
||||
} |
||||
|
||||
fn what_is_it(&self) -> String { |
||||
println!("What animal is it?"); |
||||
match prompt::ask("> ") { |
||||
None => self.abort(), |
||||
Some(s) => s, |
||||
} |
||||
} |
||||
|
||||
fn how_to_tell_apart(&self, secret: &str, other: &str) -> (String, bool) { |
||||
println!("How to tell apart {} and {}?", secret, other); |
||||
let q: String = match prompt::ask("> ") { |
||||
None => self.abort(), |
||||
Some(s) => s, |
||||
}; |
||||
|
||||
println!("What is the answer for {}?", secret); |
||||
let b: bool = match prompt::ask_yn("> ") { |
||||
None => self.abort(), |
||||
Some(b) => b, |
||||
}; |
||||
|
||||
(q, b) |
||||
} |
||||
} |
@ -1,221 +1,19 @@ |
||||
use std::collections::HashMap; |
||||
|
||||
mod animals; |
||||
mod prompt; |
||||
mod slot; |
||||
mod testing; |
||||
mod cli_user; |
||||
|
||||
struct AnswerMachine<'a> { |
||||
answer: String, |
||||
yes_no: HashMap<&'a str, bool>, |
||||
distinguish: HashMap<&'a str, (&'a str, bool)>, |
||||
} |
||||
|
||||
impl<'a> AnswerMachine<'a> { |
||||
fn abort(&self, msg: String) -> ! { |
||||
println!("{}", msg); |
||||
std::process::exit(1); |
||||
} |
||||
} |
||||
|
||||
impl<'a> animals::UserAPI for AnswerMachine<'a> { |
||||
fn notify_new_game(&self) { |
||||
println!("----- NEW GAME -----"); |
||||
println!("Secret animal is: {}", self.answer); |
||||
} |
||||
|
||||
fn notify_game_ended(&self) { |
||||
println!("Game ended.\n"); |
||||
} |
||||
|
||||
fn notify_new_animal(&self, animal: &str) { |
||||
println!("Learned a new animal: {}", animal); |
||||
} |
||||
|
||||
fn notify_victory(&self) { |
||||
println!("Yay, found an answer!"); |
||||
} |
||||
|
||||
fn answer_yes_no(&self, question: &str) -> bool { |
||||
println!("{}", question); |
||||
|
||||
let answer = match self.yes_no.get(question) { |
||||
Some(a) => *a, |
||||
None => { |
||||
self.abort(format!("* Missing answer for {}! *", &self.answer)); |
||||
} |
||||
}; |
||||
|
||||
println!("> {}", match answer { true => "yes", false => "no" }); |
||||
answer |
||||
} |
||||
|
||||
fn is_it_a(&self, animal: &str) -> bool { |
||||
println!("Is it a {}?", animal); |
||||
|
||||
let answer = animal == &self.answer; |
||||
println!("> {}", match answer { true => "yes", false => "no" }); |
||||
|
||||
answer |
||||
} |
||||
|
||||
fn what_is_it(&self) -> String { |
||||
println!("What animal is it?"); |
||||
println!("> {}", self.answer); |
||||
|
||||
self.answer.clone() |
||||
} |
||||
|
||||
fn how_to_tell_apart(&self, other: &str) -> (String, bool) { |
||||
println!("How to tell apart {} and {}?", self.answer, other); |
||||
|
||||
let (s, b) = self.distinguish.get(other).unwrap_or_else(|| { |
||||
self.abort(format!("* I don't know how to tell apart {} and {}! *", self.answer, other)); |
||||
}); |
||||
|
||||
println!("> {}", s); |
||||
println!("What is the answer for {}?", self.answer); |
||||
println!( |
||||
"> {}", |
||||
match *b { true => "yes", false => "no" } |
||||
); |
||||
|
||||
(s.to_string(), *b) |
||||
} |
||||
} |
||||
|
||||
// a little hack for map initializer literals
|
||||
macro_rules! map( |
||||
{ $($key:expr => $value:expr),* } => { |
||||
{ |
||||
let mut m = std::collections::HashMap::new(); |
||||
$( m.insert($key, $value); )* |
||||
m |
||||
} |
||||
}; |
||||
); |
||||
use crate::cli_user::CliUser; |
||||
|
||||
fn main() { |
||||
let db = animals::AnimalDB::new(); |
||||
//testing::main();
|
||||
|
||||
let duck = AnswerMachine { |
||||
answer: "duck".to_string(), |
||||
yes_no: map! { |
||||
"Does it quack?" => true |
||||
}, |
||||
distinguish: map! { |
||||
"cow" => ("Does it moo?", false) |
||||
}, |
||||
}; |
||||
db.start_game(&duck); |
||||
|
||||
let dog = AnswerMachine { |
||||
answer: "dog".to_string(), |
||||
yes_no: map! { |
||||
"Does it quack?" => false, |
||||
"Does it woof?" => true, |
||||
"Is it wild?" => false |
||||
}, |
||||
distinguish: map! { |
||||
"duck" => ("Does it quack?", false), |
||||
"wolf" => ("Is it wild?", false) |
||||
}, |
||||
}; |
||||
db.start_game(&dog); |
||||
|
||||
let wolf = AnswerMachine { |
||||
answer: "wolf".to_string(), |
||||
yes_no: map! { |
||||
"Does it quack?" => false, |
||||
"Is it wild?" => true, |
||||
"Does it fly?" => false, |
||||
"Does it eat acorns?" => false |
||||
}, |
||||
distinguish: map! { |
||||
"dog" => ("Is it wild?", true) |
||||
}, |
||||
}; |
||||
db.start_game(&wolf); |
||||
|
||||
let cow = AnswerMachine { |
||||
answer: "cow".to_string(), |
||||
yes_no: map! { |
||||
"Does it quack?" => false, |
||||
"Is it wild?" => false, |
||||
"Does it woof?" => false, |
||||
"Does it give milk?" => true, |
||||
"Is it grown for meat?" => true |
||||
}, |
||||
distinguish: map! { |
||||
"dog" => ("Does it woof?", false) |
||||
}, |
||||
}; |
||||
db.start_game(&cow); |
||||
|
||||
let pig = AnswerMachine { |
||||
answer: "pig".to_string(), |
||||
yes_no: map! { |
||||
"Does it quack?" => false, |
||||
"Is it wild?" => false, |
||||
"Does it woof?" => false, |
||||
"Does it give milk?" => false |
||||
}, |
||||
distinguish: map! { |
||||
"cow" => ("Does it give milk?", false) |
||||
}, |
||||
}; |
||||
db.start_game(&pig); |
||||
|
||||
let goat = AnswerMachine { |
||||
answer: "goat".to_string(), |
||||
yes_no: map! { |
||||
"Does it quack?" => false, |
||||
"Is it wild?" => false, |
||||
"Does it woof?" => false, |
||||
"Does it give milk?" => true, |
||||
"Is it grown for meat?" => false |
||||
}, |
||||
distinguish: map! { |
||||
"cow" => ("Is it grown for meat?", false) |
||||
}, |
||||
}; |
||||
db.start_game(&goat); |
||||
|
||||
let raven = AnswerMachine { |
||||
answer: "raven".to_string(), |
||||
yes_no: map! { |
||||
"Does it quack?" => false, |
||||
"Is it wild?" => true, |
||||
"Does it woof?" => false, |
||||
"Does it fly?" => true |
||||
}, |
||||
distinguish: map! { |
||||
"wolf" => ("Does it fly?", true) |
||||
}, |
||||
}; |
||||
db.start_game(&raven); |
||||
|
||||
let boar = AnswerMachine { |
||||
answer: "boar".to_string(), |
||||
yes_no: map! { |
||||
"Does it quack?" => false, |
||||
"Is it wild?" => true, |
||||
"Does it woof?" => false, |
||||
"Does it fly?" => false |
||||
}, |
||||
distinguish: map! { |
||||
"wolf" => ("Does it eat acorns?", true) |
||||
}, |
||||
}; |
||||
db.start_game(&boar); |
||||
|
||||
|
||||
// try how the DB works with the learned animals
|
||||
println!("===== Testing known animals ====="); |
||||
let db = animals::AnimalDB::new(); |
||||
db.load("animals.txt").unwrap_or(()); |
||||
let user = CliUser::new(&db); |
||||
|
||||
db.start_game(&cow); |
||||
db.start_game(&dog); |
||||
db.start_game(&wolf); |
||||
db.start_game(&duck); |
||||
db.start_game(&pig); |
||||
db.start_game(&goat); |
||||
db.start_game(&raven); |
||||
loop { |
||||
db.start_game(&user); |
||||
} |
||||
} |
||||
|
@ -0,0 +1,74 @@ |
||||
use std::io; |
||||
use std::io::Write; |
||||
use std::str::FromStr; |
||||
|
||||
fn flush() { |
||||
io::stdout().flush().expect("Failed to flush stdout"); |
||||
} |
||||
|
||||
struct YesNo(bool); |
||||
struct YesNoParseError(); |
||||
|
||||
impl From<YesNo> for bool { |
||||
fn from(yn: YesNo) -> Self { |
||||
yn.0 |
||||
} |
||||
} |
||||
|
||||
impl FromStr for YesNo { |
||||
type Err = YesNoParseError; |
||||
|
||||
fn from_str(s: &str) -> Result<Self, <Self as FromStr>::Err> { |
||||
match s { |
||||
"true" => Ok(YesNo(true)), |
||||
"y" => Ok(YesNo(true)), |
||||
"1" => Ok(YesNo(true)), |
||||
"yes" => Ok(YesNo(true)), |
||||
"a" => Ok(YesNo(true)), |
||||
"false" => Ok(YesNo(false)), |
||||
"f" => Ok(YesNo(false)), |
||||
"no" => Ok(YesNo(false)), |
||||
"n" => Ok(YesNo(false)), |
||||
"0" => Ok(YesNo(false)), |
||||
_ => Err(YesNoParseError()), |
||||
} |
||||
} |
||||
} |
||||
|
||||
pub fn ask_yn(prompt: &str) -> Option<bool> { |
||||
match ask::<YesNo>(prompt) { |
||||
Some(yn) => Some(yn.into()), |
||||
None => None, |
||||
} |
||||
} |
||||
|
||||
pub fn ask<T>(prompt: &str) -> Option<T> |
||||
where |
||||
T: FromStr, |
||||
{ |
||||
let mut buf = String::new(); |
||||
loop { |
||||
print!("{}", prompt); |
||||
|
||||
flush(); |
||||
buf.clear(); |
||||
io::stdin() |
||||
.read_line(&mut buf) |
||||
.expect("Failed to read line"); |
||||
|
||||
let s = buf.trim(); |
||||
if s.is_empty() { |
||||
println!(); // empty string returns None as a signal to terminate the program
|
||||
break None; |
||||
} |
||||
|
||||
match s.parse() { |
||||
Ok(val) => { |
||||
break Some(val); |
||||
} |
||||
Err(_) => { |
||||
println!("Could not parse \"{}\", please try again.", s); |
||||
} |
||||
}; |
||||
} |
||||
} |
@ -0,0 +1,239 @@ |
||||
use std::collections::HashMap; |
||||
use crate::animals; |
||||
|
||||
struct AnswerMachine<'a> { |
||||
answer: String, |
||||
yes_no: HashMap<&'a str, bool>, |
||||
distinguish: HashMap<&'a str, (&'a str, bool)>, |
||||
} |
||||
|
||||
impl<'a> AnswerMachine<'a> { |
||||
fn abort(&self, msg: String) -> ! { |
||||
println!("{}", msg); |
||||
std::process::exit(1); |
||||
} |
||||
} |
||||
|
||||
impl<'a> animals::UserAPI for AnswerMachine<'a> { |
||||
fn notify_new_game(&self) { |
||||
println!("----- NEW GAME -----"); |
||||
println!("Secret animal is: {}", self.answer); |
||||
} |
||||
|
||||
fn notify_game_ended(&self) { |
||||
println!("Game ended.\n"); |
||||
} |
||||
|
||||
fn notify_new_animal(&self, animal: &str) { |
||||
println!("Learned a new animal: {}", animal); |
||||
} |
||||
|
||||
fn notify_victory(&self) { |
||||
println!("Yay, found an answer!"); |
||||
} |
||||
|
||||
fn answer_yes_no(&self, question: &str) -> bool { |
||||
println!("{}", question); |
||||
|
||||
let answer = match self.yes_no.get(question) { |
||||
Some(a) => *a, |
||||
None => { |
||||
self.abort(format!("* Missing answer for {}! *", &self.answer)); |
||||
} |
||||
}; |
||||
|
||||
println!( |
||||
"> {}", |
||||
match answer { |
||||
true => "yes", |
||||
false => "no", |
||||
} |
||||
); |
||||
answer |
||||
} |
||||
|
||||
fn is_it_a(&self, animal: &str) -> bool { |
||||
println!("Is it a {}?", animal); |
||||
|
||||
let answer = animal == &self.answer; |
||||
println!( |
||||
"> {}", |
||||
match answer { |
||||
true => "yes", |
||||
false => "no", |
||||
} |
||||
); |
||||
|
||||
answer |
||||
} |
||||
|
||||
fn what_is_it(&self) -> String { |
||||
println!("What animal is it?"); |
||||
println!("> {}", self.answer); |
||||
|
||||
self.answer.clone() |
||||
} |
||||
|
||||
fn how_to_tell_apart(&self, secret: &str, other: &str) -> (String, bool) { |
||||
println!("How to tell apart {} and {}?", secret, other); |
||||
|
||||
let (s, b) = self.distinguish.get(other).unwrap_or_else(|| { |
||||
self.abort(format!( |
||||
"* I don't know how to tell apart {} and {}! *", |
||||
self.answer, other |
||||
)); |
||||
}); |
||||
|
||||
println!("> {}", s); |
||||
println!("What is the answer for {}?", self.answer); |
||||
println!( |
||||
"> {}", |
||||
match *b { |
||||
true => "yes", |
||||
false => "no", |
||||
} |
||||
); |
||||
|
||||
(s.to_string(), *b) |
||||
} |
||||
} |
||||
|
||||
// a little hack for map initializer literals
|
||||
macro_rules! map( |
||||
{ $($key:expr => $value:expr),* } => { |
||||
{ |
||||
let mut m = std::collections::HashMap::new(); |
||||
$( m.insert($key, $value); )* |
||||
m |
||||
} |
||||
}; |
||||
); |
||||
|
||||
#[allow(dead_code)] |
||||
pub fn main() { |
||||
let db = animals::AnimalDB::new(); |
||||
|
||||
let duck = AnswerMachine { |
||||
answer: "duck".to_string(), |
||||
yes_no: map! { |
||||
"Does it quack?" => true |
||||
}, |
||||
distinguish: map! { |
||||
"cow" => ("Does it moo?", false) |
||||
}, |
||||
}; |
||||
db.start_game(&duck); |
||||
|
||||
let dog = AnswerMachine { |
||||
answer: "dog".to_string(), |
||||
yes_no: map! { |
||||
"Does it quack?" => false, |
||||
"Does it woof?" => true, |
||||
"Is it wild?" => false |
||||
}, |
||||
distinguish: map! { |
||||
"duck" => ("Does it quack?", false), |
||||
"wolf" => ("Is it wild?", false) |
||||
}, |
||||
}; |
||||
db.start_game(&dog); |
||||
|
||||
let wolf = AnswerMachine { |
||||
answer: "wolf".to_string(), |
||||
yes_no: map! { |
||||
"Does it quack?" => false, |
||||
"Is it wild?" => true, |
||||
"Does it fly?" => false, |
||||
"Does it eat acorns?" => false |
||||
}, |
||||
distinguish: map! { |
||||
"dog" => ("Is it wild?", true) |
||||
}, |
||||
}; |
||||
db.start_game(&wolf); |
||||
|
||||
let cow = AnswerMachine { |
||||
answer: "cow".to_string(), |
||||
yes_no: map! { |
||||
"Does it quack?" => false, |
||||
"Is it wild?" => false, |
||||
"Does it woof?" => false, |
||||
"Does it give milk?" => true, |
||||
"Is it grown for meat?" => true |
||||
}, |
||||
distinguish: map! { |
||||
"dog" => ("Does it woof?", false) |
||||
}, |
||||
}; |
||||
db.start_game(&cow); |
||||
|
||||
let pig = AnswerMachine { |
||||
answer: "pig".to_string(), |
||||
yes_no: map! { |
||||
"Does it quack?" => false, |
||||
"Is it wild?" => false, |
||||
"Does it woof?" => false, |
||||
"Does it give milk?" => false |
||||
}, |
||||
distinguish: map! { |
||||
"cow" => ("Does it give milk?", false) |
||||
}, |
||||
}; |
||||
db.start_game(&pig); |
||||
|
||||
let goat = AnswerMachine { |
||||
answer: "goat".to_string(), |
||||
yes_no: map! { |
||||
"Does it quack?" => false, |
||||
"Is it wild?" => false, |
||||
"Does it woof?" => false, |
||||
"Does it give milk?" => true, |
||||
"Is it grown for meat?" => false |
||||
}, |
||||
distinguish: map! { |
||||
"cow" => ("Is it grown for meat?", false) |
||||
}, |
||||
}; |
||||
db.start_game(&goat); |
||||
|
||||
let raven = AnswerMachine { |
||||
answer: "raven".to_string(), |
||||
yes_no: map! { |
||||
"Does it quack?" => false, |
||||
"Is it wild?" => true, |
||||
"Does it woof?" => false, |
||||
"Does it fly?" => true |
||||
}, |
||||
distinguish: map! { |
||||
"wolf" => ("Does it fly?", true) |
||||
}, |
||||
}; |
||||
db.start_game(&raven); |
||||
|
||||
let boar = AnswerMachine { |
||||
answer: "boar".to_string(), |
||||
yes_no: map! { |
||||
"Does it quack?" => false, |
||||
"Is it wild?" => true, |
||||
"Does it woof?" => false, |
||||
"Does it fly?" => false |
||||
}, |
||||
distinguish: map! { |
||||
"wolf" => ("Does it eat acorns?", true) |
||||
}, |
||||
}; |
||||
db.start_game(&boar); |
||||
|
||||
// try how the DB works with the learned animals
|
||||
println!("===== Testing known animals ====="); |
||||
|
||||
db.start_game(&cow); |
||||
db.start_game(&dog); |
||||
db.start_game(&wolf); |
||||
db.start_game(&duck); |
||||
db.start_game(&pig); |
||||
db.start_game(&goat); |
||||
db.start_game(&raven); |
||||
|
||||
db.save("test_db.txt").unwrap(); |
||||
} |
Loading…
Reference in new issue