-
Notifications
You must be signed in to change notification settings - Fork 19
/
main.rs
73 lines (59 loc) · 1.55 KB
/
main.rs
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
// Create a trait called StateTransition with a method transition that takes an charachter and based on his score change his level in GameLevel (with levels like Beginner, Intermediate, and Expert).
use std::{io, process};
use rand::Rng;
trait StateTransition {
fn transition(&mut self);
}
#[allow(dead_code)]
#[derive(Debug)]
struct Player {
name: String,
score: i32,
levels: Levels
}
#[derive(Debug)]
enum Levels {
Beginner,
Intermediate,
Expert
}
impl Player {
fn new(name: &str) -> Self {
Self {
name: name.to_string(),
score: 0,
levels: Levels::Beginner
}
}
fn play(&mut self) {
let rand_score: i32 = rand::thread_rng().gen_range(-5..10);
println!("Random num: {:?}", rand_score);
self.score += rand_score;
self.transition();
}
}
impl StateTransition for Player {
fn transition(&mut self) {
if self.score > 50 && self.score <= 100 {
self.levels = Levels::Intermediate;
}
if self.score > 500 {
self.levels = Levels::Expert;
}
}
}
fn main() {
let mut player: Player = Player::new("Sam");
println!("Click \"enter\" to continue playing or \"q\" to quit the game.");
loop {
let mut action: String = String::new();
io::stdin()
.read_line(&mut action)
.expect("Failed to read the user input!");
if action.trim() == "q" {
process::exit(1);
}
player.play();
println!("{:?}", player);
}
}