From 625614b848ba67cfa2072a733f3bedf2d3277e07 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ond=C5=99ej=20=C5=BD=C3=A1ra?= Date: Tue, 7 Apr 2020 21:36:42 +0200 Subject: [PATCH] prerelease --- README.md | 28 ++++++++++++++++++++++++++++ src/ai/defender.rs | 5 ----- src/config.rs | 39 +++++++++++++++++++++++++++++++++++++++ src/main.rs | 41 ++++++++++++++++++++++++++++------------- 4 files changed, 95 insertions(+), 18 deletions(-) create mode 100644 src/config.rs diff --git a/README.md b/README.md index 45913f6..5881f44 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,31 @@ # rustymind ![CI](https://github.com/ondras/rustymind/workflows/CI/badge.svg) + +This is a hobby project to learn Rust. It is an implementation of the *Mastermind* game, including a game-solving AI. + +![screenshot](https://i.imgur.com/SXachN1.png) + +## Running + +```sh +$ git clone https://github.com/ondras/rustymind.git && cd rustymind +$ cargo run +``` + +With an explicit code length: + +```sh +$ cargo run -- 5 +``` + +Tests: + +```sh +$ cargo test +``` + +## TODO + + - [X] GH Actions to lint, test, and release + - [ ] the `--i-guess` option diff --git a/src/ai/defender.rs b/src/ai/defender.rs index 2e46b8d..1f8e402 100644 --- a/src/ai/defender.rs +++ b/src/ai/defender.rs @@ -24,11 +24,6 @@ impl crate::Defender for Defender { Style::new().bold().paint("secret code"), code ); - print!("======================"); - for _ in 0..(3 * code.data.len()) { - print!("="); - } - println!(); Self { code } } diff --git a/src/config.rs b/src/config.rs new file mode 100644 index 0000000..9f856c3 --- /dev/null +++ b/src/config.rs @@ -0,0 +1,39 @@ +use std::env; + +pub struct Config { + pub code_length: u8, + pub ai: bool, +} + +impl Config { + pub fn parse() -> Result { + let mut code_length = 4; + let mut ai = true; + let mut args: Vec = env::args().skip(1).collect(); + + let iguess_idx = args.iter().position(|x| x == "--i-guess"); + if let Some(idx) = iguess_idx { + args.remove(idx); + ai = false; + } + + if let Some(last) = args.pop() { + if let Ok(parsed) = last.parse::() { + let range = 2..6 + 1; + if range.contains(&parsed) { + code_length = parsed; + } else { + return Err(format!( + "Pick a value from {} to {} for code length", + range.start, + range.end - 1 + )); + } + } else { + return Err(format!("Cannot parse '{}' as a code length", last)); + } + } + + Ok(Self { code_length, ai }) + } +} diff --git a/src/main.rs b/src/main.rs index e4502c6..104c9be 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,8 +1,10 @@ mod ai; +mod config; mod guess; mod score; mod util; +use config::Config; use guess::Guess; use score::Score; @@ -21,7 +23,15 @@ trait Defender { fn score(&self, attempt: &Guess) -> Score; } -fn game(mut attacker: impl Attacker, defender: impl Defender) { +fn print_line(code_length: u8) { + let count = 22 + code_length * 3; + for _ in 0..count { + print!("="); + } + println!(); +} + +fn game(mut attacker: impl Attacker, defender: impl Defender) -> String { let mut rounds = 0; let mut previous: Option = None; @@ -36,28 +46,33 @@ fn game(mut attacker: impl Attacker, defender: impl Defender) { ); let score = defender.score(&attempt); - println!("Round #{}\n {} | {}", rounds, score, attempt); + println!( + "Round #{} |\n {} | {}", + rounds, score, attempt + ); if score.is_won() { - println!("The code was found in {} rounds.", rounds); - break; - } - - if rounds == 10 { - println!("Not found in {} rounds", rounds); - break; + return format!("The code was found in {} rounds.", rounds); } previous = Some((attempt, score)); } else { - println!("Code was not found"); - break; + return "Code was not found".to_string(); } } } fn main() { - let defender = ai::defender::Defender::new(4); + let config = Config::parse().unwrap_or_else(|e| { + println!("{}", e); + std::process::exit(1); + }); + + let defender = ai::defender::Defender::new(config.code_length); let attacker = ai::attacker::Attacker::new(defender.code_length()); - game(attacker, defender); + + print_line(config.code_length); + let result = game(attacker, defender); + print_line(config.code_length); + println!("{}", result); }