Created
March 27, 2023 17:54
-
-
Save AlJohri/f6ee3261bc400ccf05702c56608c59bf 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
use std::error::Error; | |
#[derive(PartialEq)] | |
enum MyGameResult { | |
Win, | |
Lose, | |
Tie, | |
} | |
#[derive(Debug, PartialEq, Clone, Copy)] | |
enum Shape { | |
Rock = 0, | |
Paper = 1, | |
Scissors = 2, | |
Lizard = 3, | |
Spock = 4, | |
} | |
impl std::convert::TryFrom<u8> for Shape { | |
type Error = Box<dyn Error>; | |
fn try_from(value: u8) -> Result<Self, Self::Error>{ | |
match value { | |
0 => Ok(Shape::Rock), | |
1 => Ok(Shape::Paper), | |
2 => Ok(Shape::Scissors), | |
3 => Ok(Shape::Lizard), | |
4 => Ok(Shape::Spock), | |
_ => Err(format!("invalid shape: {}", value).into()), | |
} | |
} | |
} | |
fn vs(me: Shape, opponent: Shape) -> MyGameResult { | |
todo!("implement me"); | |
} | |
fn main() { | |
// Rules from: https://bigbangtheory.fandom.com/wiki/Rock,_Paper,_Scissors,_Lizard,_Spock | |
// Wins | |
assert!(vs(Shape::Scissors, Shape::Paper) == MyGameResult::Win); // "Scissors cuts Paper" | |
assert!(vs(Shape::Paper, Shape::Rock) == MyGameResult::Win); // "Paper covers Rock" | |
assert!(vs(Shape::Rock, Shape::Lizard) == MyGameResult::Win); // "Rock crushes Lizard" | |
assert!(vs(Shape::Lizard, Shape::Spock) == MyGameResult::Win); // "Lizard poisons Spock" | |
assert!(vs(Shape::Spock, Shape::Scissors) == MyGameResult::Win); // "Spock smashes Scissors" | |
assert!(vs(Shape::Scissors, Shape::Lizard) == MyGameResult::Win); // "Scissors decapitates Lizard" | |
assert!(vs(Shape::Lizard, Shape::Paper) == MyGameResult::Win); // "Lizard eats Paper" | |
assert!(vs(Shape::Paper, Shape::Spock) == MyGameResult::Win); // "Paper disproves Spock" | |
assert!(vs(Shape::Spock, Shape::Rock) == MyGameResult::Win); // "Spock vaporizes Rock" | |
assert!(vs(Shape::Rock, Shape::Scissors) == MyGameResult::Win); // "Rock crushes Scissors" | |
// Ties | |
assert!(vs(Shape::Rock, Shape::Rock) == MyGameResult::Tie); | |
assert!(vs(Shape::Paper, Shape::Paper) == MyGameResult::Tie); | |
assert!(vs(Shape::Scissors, Shape::Scissors) == MyGameResult::Tie); | |
assert!(vs(Shape::Lizard, Shape::Lizard) == MyGameResult::Tie); | |
assert!(vs(Shape::Spock, Shape::Spock) == MyGameResult::Tie); | |
// One Example Loss | |
assert!(vs(Shape::Scissors, Shape::Rock) == MyGameResult::Lose); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment