Skip to content

Instantly share code, notes, and snippets.

@JosephTLyons
Last active September 30, 2021 12:23
Show Gist options
  • Save JosephTLyons/8ab0aeb31980d868d7a0ecd01bd49142 to your computer and use it in GitHub Desktop.
Save JosephTLyons/8ab0aeb31980d868d7a0ecd01bd49142 to your computer and use it in GitHub Desktop.
A quick Rust code jam to make Rock, Paper, Scissors!
use std::io::{self, Write};
#[derive(Debug, PartialEq)]
enum Weapon {
Rock,
Paper,
Scissors,
}
impl std::fmt::Display for Weapon {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
match *self {
Weapon::Rock => write!(f, "Rock"),
Weapon::Paper => write!(f, "Paper"),
Weapon::Scissors => write!(f, "Scissors"),
}
}
}
#[derive(Debug, PartialEq)]
enum Outcome {
PlayerOneWon,
PlayerTwoWon,
Draw,
}
impl std::fmt::Display for Outcome {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
match *self {
Outcome::PlayerOneWon => write!(f, "Player one won!"),
Outcome::PlayerTwoWon => write!(f, "Player two won!"),
Outcome::Draw => write!(f, "Draw!"),
}
}
}
fn main() {
let player_one_weapon = get_weapon_selection();
let player_two_weapon = get_weapon_selection();
println!(
"Player one ({}) vs Player two ({})",
player_one_weapon, player_two_weapon
);
let outcome = get_outcome(&player_one_weapon, &player_two_weapon);
println!("{}", outcome);
}
fn get_weapon_selection() -> Weapon {
loop {
println!("1: {}", Weapon::Rock);
println!("2: {}", Weapon::Paper);
println!("3: {}", Weapon::Scissors);
print!("Answer: ");
io::stdout().flush().expect("Failed to flush output stream");
let mut input_string = String::new();
io::stdin()
.read_line(&mut input_string)
.expect("Failed to read input");
println!();
match input_string.trim().to_lowercase().as_str() {
"1" => return Weapon::Rock,
"2" => return Weapon::Paper,
"3" => return Weapon::Scissors,
_ => println!("Invalid input"),
}
}
}
fn get_outcome(player_one_weapon: &Weapon, player_two_weapon: &Weapon) -> Outcome {
match (player_one_weapon, player_two_weapon) {
(Weapon::Paper, Weapon::Rock)
| (Weapon::Rock, Weapon::Scissors)
| (Weapon::Scissors, Weapon::Paper) => Outcome::PlayerOneWon,
(Weapon::Paper, Weapon::Scissors)
| (Weapon::Rock, Weapon::Paper)
| (Weapon::Scissors, Weapon::Rock) => Outcome::PlayerTwoWon,
_ => Outcome::Draw,
}
}
#[test]
#[rustfmt::skip]
fn test_get_outcome() {
assert_eq!(get_outcome(&Weapon::Paper, &Weapon::Rock), Outcome::PlayerOneWon);
assert_eq!(get_outcome(&Weapon::Rock, &Weapon::Scissors), Outcome::PlayerOneWon);
assert_eq!(get_outcome(&Weapon::Scissors, &Weapon::Paper), Outcome::PlayerOneWon);
assert_eq!(get_outcome(&Weapon::Paper, &Weapon::Scissors), Outcome::PlayerTwoWon);
assert_eq!(get_outcome(&Weapon::Rock, &Weapon::Paper), Outcome::PlayerTwoWon);
assert_eq!(get_outcome(&Weapon::Scissors, &Weapon::Rock), Outcome::PlayerTwoWon);
assert_eq!(get_outcome(&Weapon::Rock, &Weapon::Rock), Outcome::Draw);
assert_eq!(get_outcome(&Weapon::Paper, &Weapon::Paper), Outcome::Draw);
assert_eq!(get_outcome(&Weapon::Scissors, &Weapon::Scissors), Outcome::Draw);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment