Updated README.md

Signed-off-by: TuDatTr <tuan-dat.tran@tudattr.dev>
main
TuDatTr 2022-12-02 15:09:14 +01:00
parent 57a95b7c34
commit a5420b8c51
4 changed files with 86 additions and 65 deletions

View File

@ -0,0 +1,63 @@
use strum::IntoEnumIterator;
use strum_macros::{Display, EnumIter};
use tracing::{info, debug};
use crate::gameresult::GameResult;
/// Enum containing the possible choices for Rock, Paper, Scissors.
#[derive(Display, Clone, Eq, PartialEq, EnumIter)]
pub enum Choice {
Rock,
Paper,
Scissors,
}
impl Choice {
/// Returns the outcome of the game from `i`s perspective, playing against `other`.
fn win(i: &Choice, other: &Choice) -> GameResult {
debug!("Get GameResult based on {}, {}", i, other);
let result = match (i, other) {
(Choice::Rock, Choice::Scissors) => GameResult::Win,
(Choice::Paper, Choice::Rock) => GameResult::Win,
(Choice::Scissors, Choice::Paper) => GameResult::Win,
_ if i == other => GameResult::Draw,
_ => GameResult::Loose
};
info!("GameResult: {}", &result);
result
}
/// Calculate score based on `self`s and `other`s =Rock, Paper, Scissors=-choice and the resulting outcome.
pub fn play(&self, other: &Choice) -> u64 {
debug!("Get score based on {}, {}", &self, other);
let hand_value = match self {
Choice::Rock => 1,
Choice::Paper => 2,
Choice::Scissors => 3,
};
let game_value = match Choice::win(self, other) {
GameResult::Win => 6,
GameResult::Draw => 3,
GameResult::Loose => 0,
};
let result = hand_value + game_value;
info!("Score: {}", &result);
result
}
/// Calculates the score based on the wished upon `outcome` when `self` is the opponents choice.
pub fn cheat(&self, outcome: &GameResult) -> u64 {
debug!("Get score by cheating based on {}, {}", &self, outcome);
let user_choice = Choice::iter().filter(|c| &Choice::win(&c, &self) == outcome).next();
let result =user_choice.unwrap().play(&self);
info!("Score: {}", result);
result
}
}

View File

@ -0,0 +1,9 @@
use strum_macros::Display;
/// Enum containing the possible outcomes for Rock, Paper, Scissors.
#[derive(Display, Clone, Eq, PartialEq)]
pub enum GameResult {
Win,
Loose,
Draw
}

View File

@ -1,69 +1,14 @@
use common::read_file; use common::read_file;
use strum::IntoEnumIterator;
use strum_macros::{Display, EnumIter};
use std::path::PathBuf; use std::path::PathBuf;
use tracing::{info, debug}; use tracing::{info, debug};
#[derive(Display, Clone, Eq, PartialEq, EnumIter)] mod choice;
enum Choice { use crate::choice::Choice;
Rock,
Paper,
Scissors,
}
#[derive(Display, Clone, Eq, PartialEq)] mod gameresult;
enum GameResult { use crate::gameresult::GameResult;
Win,
Loose,
Draw
}
impl Choice {
fn win(i: &Choice, other: &Choice) -> GameResult {
debug!("Get GameResult based on {}, {}", i, other);
let result = match (i, other) {
(Choice::Rock, Choice::Scissors) => GameResult::Win,
(Choice::Paper, Choice::Rock) => GameResult::Win,
(Choice::Scissors, Choice::Paper) => GameResult::Win,
_ if i == other => GameResult::Draw,
_ => GameResult::Loose
};
info!("GameResult: {}", &result);
result
}
fn play(&self, other: &Choice) -> u64 {
debug!("Get score based on {}, {}", &self, other);
let hand_value = match self {
Choice::Rock => 1,
Choice::Paper => 2,
Choice::Scissors => 3,
};
let game_value = match Choice::win(self, other) {
GameResult::Win => 6,
GameResult::Draw => 3,
GameResult::Loose => 0,
};
let result = hand_value + game_value;
info!("Score: {}", &result);
result
}
fn cheat(&self, outcome: &GameResult) -> u64 {
debug!("Get score by cheating based on {}, {}", &self, outcome);
let user_choice = Choice::iter().filter(|c| &Choice::win(&c, &self) == outcome).next();
let result =user_choice.unwrap().play(&self);
info!("Score: {}", result);
result
}
}
/// Calculates score for single game, given specification from part one of the task.
fn calculate_result_1a(line: &str) -> u64 { fn calculate_result_1a(line: &str) -> u64 {
debug!("Get score for 1a of game {}", line); debug!("Get score for 1a of game {}", line);
@ -72,7 +17,7 @@ fn calculate_result_1a(line: &str) -> u64 {
Some('A') => Choice::Rock, Some('A') => Choice::Rock,
Some('B') => Choice::Paper, Some('B') => Choice::Paper,
Some('C') => Choice::Scissors, Some('C') => Choice::Scissors,
_ => Choice::Rock, _ => Choice::Rock, // Todo: Error handling, this shouldn't happen
}; };
input.next(); input.next();
@ -81,13 +26,14 @@ fn calculate_result_1a(line: &str) -> u64 {
Some('X') => Choice::Rock, Some('X') => Choice::Rock,
Some('Y') => Choice::Paper, Some('Y') => Choice::Paper,
Some('Z') => Choice::Scissors, Some('Z') => Choice::Scissors,
_ => Choice::Rock, _ => Choice::Rock, // Todo: Error handling, this shouldn't happen
}; };
let result = user_choice.play(&opponent_choice); let result = user_choice.play(&opponent_choice);
info!("Total score (1a): {}", result); info!("Total score (1a): {}", result);
result result
} }
/// Calculates score for single game, given specification from part two of the task.
fn calculate_result_1b(line: &str) -> u64 { fn calculate_result_1b(line: &str) -> u64 {
debug!("Get score for 1b of game {}", line); debug!("Get score for 1b of game {}", line);
@ -96,7 +42,7 @@ fn calculate_result_1b(line: &str) -> u64 {
Some('A') => Choice::Rock, Some('A') => Choice::Rock,
Some('B') => Choice::Paper, Some('B') => Choice::Paper,
Some('C') => Choice::Scissors, Some('C') => Choice::Scissors,
_ => Choice::Rock, // Error handling, this shouldn't happen _ => Choice::Rock, // Todo: Error handling, this shouldn't happen
}; };
input.next(); input.next();
@ -105,7 +51,7 @@ fn calculate_result_1b(line: &str) -> u64 {
Some('X') => GameResult::Loose, Some('X') => GameResult::Loose,
Some('Y') => GameResult::Draw, Some('Y') => GameResult::Draw,
Some('Z') => GameResult::Win, Some('Z') => GameResult::Win,
_ => GameResult::Loose, // Error handling, this shouldn't happen _ => GameResult::Loose, // Todo: Error handling, this shouldn't happen
}; };
let result = opponent_choice.cheat(&outcome); let result = opponent_choice.cheat(&outcome);
info!("Total score (1b): {}", result); info!("Total score (1b): {}", result);

View File

@ -4,8 +4,11 @@
``` ```
. .
├── 2022 ├── 2022
│   ├── common
│   ├── day-1_calorie_counting │   ├── day-1_calorie_counting
│   └── day-1_calorie_counting-alt │   ├── day-1_calorie_counting-alt
│   ├── day-2_rock_paper_scissors
│   └── day-2_rock_paper_scissors-alt
├── create_readme.sh ├── create_readme.sh
└── README.md └── README.md
``` ```