-
Notifications
You must be signed in to change notification settings - Fork 0
/
Collections_and_Enums.rs
115 lines (103 loc) · 3.09 KB
/
Collections_and_Enums.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
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
109
110
111
112
113
114
115
// Silence some warnings that could distract from the exercise
#![allow(unused_variables, unused_mut, dead_code)]
// Someone is shooting arrows at a target. We need to classify the shots.
//
// 1a. Create an enum called `Shot` with variants:
// - `Bullseye`
// - `Hit`, containing the distance from the center (an f64)
// - `Miss`
//
// You will need to complete 1b as well before you will be able to run this program successfully.
enum Shot{
Bullseye,
Hit { x: f64 },
Miss,
}
impl Shot {
// Here is a method for the `Shot` enum you just defined.
fn points(self) -> i32 {
// 1b. Implement this method to convert a Shot into points
// - return 5 points if `self` is a `Shot::Bullseye`
// - return 2 points if `self` is a `Shot::Hit(x)` where x < 3.0
// - return 1 point if `self` is a `Shot::Hit(x)` where x >= 3.0
// - return 0 points if `self` is a Miss
match self {
Shot::Bullseye => {
5
},
Shot::Miss => {
0
},
Shot::Hit{ x } => {
if x < 3.0 {
2
}
else {
1
}
}
}
}
}
fn main() {
// Simulate shooting a bunch of arrows and gathering their coordinates on the target.
let arrow_coords: Vec<Coord> = get_arrow_coords(5);
let mut shots: Vec<Shot> = Vec::new();
// 2. For each coord in arrow_coords:
//
// A. Call `coord.print_description()`
// B. Append the correct variant of `Shot` to the `shots` vector depending on the value of
// `coord.distance_from_center()`
// - Less than 1.0 -- `Shot::Bullseye`
// - Between 1.0 and 5.0 -- `Shot::Hit(value)`
// - Greater than 5.0 -- `Shot::Miss`
for a_coo in arrow_coords {
a_coo.print_description();
let dist = a_coo.distance_from_center();
if dist < 1.0 {
shots.push(Shot::Bullseye);
}
else if dist > 5.0 {
shots.push(Shot::Miss);
}
else {
shots.push(Shot::Hit{ x : dist });
}
}
let mut total = 0;
// 3. Finally, loop through each shot in shots and add its points to total
for shot in shots {
total += shot.points();
}
println!("Final point total is: {}", total);
}
// A coordinate of where an Arrow hit
#[derive(Debug)]
struct Coord {
x: f64,
y: f64,
}
impl Coord {
fn distance_from_center(&self) -> f64 {
(self.x.powf(2.0) + self.y.powf(2.0)).sqrt()
}
fn print_description(&self) {
println!(
"coord is {:.1} away, at ({:.1}, {:.1})",
self.distance_from_center(),
self.x,
self.y);
}
}
// Generate some random coordinates
fn get_arrow_coords(num: u32) -> Vec<Coord> {
let mut coords: Vec<Coord> = Vec::new();
for _ in 0..num {
let coord = Coord {
x: (rand::random::<f64>() - 0.5) * 12.0,
y: (rand::random::<f64>() - 0.5) * 12.0,
};
coords.push(coord);
}
coords
}