Repository navigation
Expand file tree
/
Copy pathmain.rs
More file actions
108 lines (92 loc) · 2.99 KB
/
Copy pathmain.rs
File metadata and controls
108 lines (92 loc) · 2.99 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
use std::env;
#[derive(Debug)]
pub enum Game {
Rock,
Paper,
Scissors,
}
impl TryFrom<&String> for Game {
type Error = String;
fn try_from(s: &String) -> Result<Self, Self::Error> {
match s.to_lowercase().as_str() {
"rock" => Ok(Game::Rock),
"paper" => Ok(Game::Paper),
"scissors" => Ok(Game::Scissors),
_ => Err("Invalid game".to_string()),
}
}
}
#[derive(Debug, PartialEq)]
pub enum GameResult {
Win,
Draw,
Lost,
}
pub fn play(a: Game, b: Game) -> GameResult {
match (a, b) {
// win
(Game::Paper, Game::Rock) => GameResult::Win,
(Game::Rock, Game::Scissors) => GameResult::Win,
(Game::Scissors, Game::Paper) => GameResult::Win,
// draw
(Game::Rock, Game::Rock) => GameResult::Draw,
(Game::Scissors, Game::Scissors) => GameResult::Draw,
(Game::Paper, Game::Paper) => GameResult::Draw,
// lose
(Game::Rock, Game::Paper) => GameResult::Lost,
(Game::Paper, Game::Scissors) => GameResult::Lost,
(Game::Scissors, Game::Rock) => GameResult::Lost,
}
}
pub fn greets(name: &str) -> String {
format!("Hello, {} 🦀 !", name)
}
fn main() {
let args: Vec<String> = env::args().collect();
let cmd = args.get(1).expect("Command is required");
match cmd.as_str() {
"greets" => {
let name = args.get(2).expect("Name is required for greetings");
println!("{}", greets(name));
}
"chifoumi" => {
let p_one = args.get(2).expect("Player one is required for chifoumi");
let p_two = args.get(3).expect("Player two is required for chifoumi");
// cast to Game enum
let p_one: Game = Game::try_from(p_one).unwrap();
let p_two: Game = Game::try_from(p_two).unwrap();
let result = play(p_one, p_two);
println!("p1 vs p2 : {:?}", result);
}
_ => eprintln!("Not supported command"),
}
}
#[cfg(test)]
mod tests {
use super::greets;
use super::play;
use super::Game;
use super::GameResult;
#[test]
fn test_player_one_wins() {
assert_eq!(play(Game::Rock, Game::Scissors), GameResult::Win);
assert_eq!(play(Game::Scissors, Game::Paper), GameResult::Win);
assert_eq!(play(Game::Paper, Game::Rock), GameResult::Win);
}
#[test]
fn test_player_one_draw() {
assert_eq!(play(Game::Rock, Game::Rock), GameResult::Draw);
assert_eq!(play(Game::Paper, Game::Paper), GameResult::Draw);
assert_eq!(play(Game::Scissors, Game::Scissors), GameResult::Draw);
}
#[test]
fn test_player_one_lose() {
assert_eq!(play(Game::Rock, Game::Paper), GameResult::Lost);
assert_eq!(play(Game::Paper, Game::Scissors), GameResult::Lost);
assert_eq!(play(Game::Scissors, Game::Rock), GameResult::Lost);
}
#[test]
fn test_greets() {
assert_eq!(greets("You"), String::from("Hello, You 🦀 !"));
}
}