Created
August 1, 2024 19:21
-
-
Save yvan-sraka/9629052d694ac72e2c2e16796a8ed551 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[package] | |
name = "mymastermind" | |
version = "0.1.0" | |
authors = ["Yvan Sraka <[email protected]>"] | |
edition = "2018" | |
[dependencies] | |
ansi_term = "0.11.0" | |
rand = "0.5.0" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
extern crate rand; | |
use rand::{ | |
distributions::{Distribution, Standard}, | |
Rng, | |
}; | |
use ansi_term::Colour::{Red, Blue, Green, Yellow}; | |
#[derive(PartialEq)] | |
enum Color { | |
Red, | |
Blue, | |
Green, | |
Yellow | |
} | |
impl Distribution<Color> for Standard { | |
fn sample<R: Rng + ?Sized>(&self, rng: &mut R) -> Color { | |
match rng.gen_range(0, 3) { | |
0 => Color::Red, | |
1 => Color::Blue, | |
2 => Color::Green, | |
_ => Color::Yellow, | |
} | |
} | |
} | |
fn fancy_print_guess(guess: &Vec<Color>) { | |
for pawn in guess { | |
print!("{}", match pawn { | |
Color::Red => Red.paint("R"), | |
Color::Blue => Blue.paint("B"), | |
Color::Green => Green.paint("G"), | |
Color::Yellow => Yellow.paint("Y") | |
}); | |
} | |
println!(""); | |
} | |
fn number_of_well_placed_pawns(secret: &Vec<Color>, guess: &Vec<Color>) -> i32 { | |
let mut number = 0; | |
for i in 0..4 { | |
if secret[i] == guess[i] { | |
number += 1; | |
} | |
} | |
number | |
} | |
fn number_of_not_well_placed_pawns(secret: &Vec<Color>, guess: &Vec<Color>) -> i32 { | |
let mut number = 0; | |
for i in 0..4 { | |
for j in 0..4 { | |
if secret[i] == guess[j] { | |
number += 1; | |
break; | |
} | |
} | |
} | |
number - number_of_well_placed_pawns(secret, guess) | |
} | |
fn main() { | |
let mut turn = 0; | |
let mut secret: Vec<Color> = vec![]; | |
for _ in 0..4 { | |
let color: Color = rand::random(); | |
secret.push(color); | |
} | |
println!("███╗ ███╗ █████╗ ███████╗████████╗███████╗██████╗ ███╗ ███╗██╗███╗ ██╗██████╗ "); | |
println!("████╗ ████║██╔══██╗██╔════╝╚══██╔══╝██╔════╝██╔══██╗████╗ ████║██║████╗ ██║██╔══██╗"); | |
println!("██╔████╔██║███████║███████╗ ██║ █████╗ ██████╔╝██╔████╔██║██║██╔██╗ ██║██║ ██║"); | |
println!("██║╚██╔╝██║██╔══██║╚════██║ ██║ ██╔══╝ ██╔══██╗██║╚██╔╝██║██║██║╚██╗██║██║ ██║"); | |
println!("██║ ╚═╝ ██║██║ ██║███████║ ██║ ███████╗██║ ██║██║ ╚═╝ ██║██║██║ ╚████║██████╔╝"); | |
println!("╚═╝ ╚═╝╚═╝ ╚═╝╚══════╝ ╚═╝ ╚══════╝╚═╝ ╚═╝╚═╝ ╚═╝╚═╝╚═╝ ╚═══╝╚═════╝ "); | |
println!(""); | |
println!("Try to guess my hidden combination of colors:"); | |
loop { | |
let mut input = String::new(); | |
std::io::stdin().read_line(&mut input) | |
.expect("Failed to read line"); | |
// Mind about the newline character. | |
if input.trim().len() != 4 { | |
println!("Sorry I am hard programmed to understand input of size four, can you help me?"); | |
continue; | |
} | |
let mut guess = vec![]; | |
for character in input.trim().chars() { | |
let pawn = match character { | |
'B' => Color::Blue, | |
'R' => Color::Red, | |
'Y' => Color::Yellow, | |
'G' => Color::Green, | |
// Exaustive pattern | |
_ => { | |
println!("Sorry dear human, I can see only four color please type:"); | |
println!("- R for red"); | |
println!("- B for blue"); | |
println!("- Y for Yellow"); | |
println!("- G for Green"); | |
println!("Otherwise I will be confused as you are."); | |
break; | |
} | |
}; | |
guess.push(pawn); | |
} | |
// Encountered an error in parsing? | |
if guess.len() != 4 { | |
continue; | |
} | |
turn += 1; | |
print!("Turn {}: ", turn); | |
fancy_print_guess(&guess); | |
println!("Number of weel placed pawns: {}", number_of_well_placed_pawns(&secret, &guess)); | |
println!("Number of not weel placed pawns: {}", number_of_not_well_placed_pawns(&secret, &guess)); | |
if guess == secret { | |
break | |
} | |
} | |
println!("🎉🎊 Congratulation this is a huge success! 🎊🎉"); | |
println!("🤯 YOU are the MasterMind! 🤯"); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment