From 1e276e61ff59e29afbbbc22ea9c56d2004335e87 Mon Sep 17 00:00:00 2001 From: Florent DENEF Date: Fri, 2 Dec 2022 11:50:55 +0100 Subject: [PATCH] WIP on problem 2 --- src/lib.rs | 3 ++- src/prob2/mod.rs | 43 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 45 insertions(+), 1 deletion(-) create mode 100644 src/prob2/mod.rs diff --git a/src/lib.rs b/src/lib.rs index 9dcaa11..7f30432 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1 +1,2 @@ -pub mod prob1; \ No newline at end of file +pub mod prob1; +pub mod prob2; \ No newline at end of file diff --git a/src/prob2/mod.rs b/src/prob2/mod.rs new file mode 100644 index 0000000..d11278c --- /dev/null +++ b/src/prob2/mod.rs @@ -0,0 +1,43 @@ +#[repr(u8)] +pub enum Play { + Rock, + Paper, + Scissors, +} + +impl Into for Play { + fn into(self) -> char { + match self { + Play::Rock => 'X', + Play::Paper => 'Y', + Play::Scissors => 'Z', + } + } +} + +impl Into for Play { + fn into(self) -> i32 { + match self { + Play::Rock => 1, + Play::Paper => 2, + Play::Scissors => 3, + } + } +} + +pub fn translate(letter: char) -> Option { + match letter { + 'A' => Some(Play::Rock), + 'B' => Some(Play::Paper), + 'C' => Some(Play::Scissors), + _ => None, + } +} + +pub fn winning(play: Play) -> char { + match play { + Play::Rock => Play::Paper.into(), + Play::Paper => Play::Scissors.into(), + Play::Scissors => Play::Rock.into(), + } +}