-
Notifications
You must be signed in to change notification settings - Fork 0
/
ocean.rs
78 lines (64 loc) · 1.78 KB
/
ocean.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
use crate::beach::Beach;
use crate::prey::{Algae, Clam, Minnow, Shrimp};
use crate::reef::Reef;
use std::cell::RefCell;
use std::rc::Rc;
use std::slice::Iter;
#[derive(Debug)]
pub struct Ocean {
// TODO: Fill in fields here.
beaches: Vec<Beach>,
reefs: Vec<Rc<RefCell<Reef>>>,
}
impl Ocean {
pub fn new() -> Ocean {
// unimplemented!();
Ocean {
beaches: Vec::new(),
reefs: Vec::new(),
}
}
pub fn add_beach(&mut self, beach: Beach) {
// unimplemented!();
self.beaches.push(beach);
}
pub fn beaches(&self) -> Iter<Beach> {
// unimplemented!();
self.beaches.iter()
}
pub fn reefs(&self) -> Iter<Rc<RefCell<Reef>>> {
// unimplemented!();
self.reefs.iter()
}
/**
* Generate a reef with the specified number of each concrete type of prey, and then add it to the ocean.
* - Minnows should have a speed of 25.
* - Shrimp should have an energy of 1.
*
* Returns a reference to the newly created reef.
*/
pub fn generate_reef(
&mut self,
n_minnows: u32,
n_shrimp: u32,
n_clams: u32,
n_algae: u32,
) -> Rc<RefCell<Reef>> {
// unimplemented!();
let reef = Rc::new(RefCell::new(Reef::new()));
for _ in 0..n_minnows {
reef.borrow_mut().add_prey(Box::new(Minnow::new(25)));
}
for _ in 0..n_shrimp {
reef.borrow_mut().add_prey(Box::new(Shrimp::new(1)));
}
for _ in 0..n_clams {
reef.borrow_mut().add_prey(Box::new(Clam::new()));
}
for _ in 0..n_algae {
reef.borrow_mut().add_prey(Box::new(Algae::new()));
}
self.reefs.push(reef.clone());
reef
}
}