summaryrefslogtreecommitdiff
path: root/src/game/player.rs
blob: 0874c769880a31c8d21e2ece7e3e1b0f333a0467 (plain)
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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
use crate::geometry::*;
use arrayvec::ArrayVec;

#[derive(Debug, PartialEq, Eq, Clone)]
pub struct Player {
    pub moves_score: i32,
    pub active_worm: usize,
    pub select_moves: u8,
    pub worms: ArrayVec<[Worm; 3]>,
}

#[derive(Debug, PartialEq, Eq, Clone)]
pub struct Worm {
    pub id: i32,
    pub health: i32,
    pub position: Point2d,
    pub bombs: u8,
    pub snowballs: u8,
    pub rounds_until_unfrozen: u8,
}

impl Player {
    pub fn find_worm_position(&self, id: i32) -> Option<usize> {
        self.worms.iter().position(|w| w.id == id)
    }

    pub fn find_worm(&self, id: i32) -> Option<&Worm> {
        self.worms.iter().find(|w| w.id == id)
    }

    pub fn find_worm_mut(&mut self, id: i32) -> Option<&mut Worm> {
        self.worms.iter_mut().find(|w| w.id == id)
    }

    pub fn active_worm(&self) -> Option<&Worm> {
        self.worms.get(self.active_worm)
    }

    pub fn active_worm_mut(&mut self) -> Option<&mut Worm> {
        self.worms.get_mut(self.active_worm)
    }

    pub fn health(&self) -> i32 {
        self.worms.iter().map(|w| w.health).sum()
    }

    pub fn max_worm_health(&self) -> i32 {
        self.worms.iter().map(|w| w.health).max().unwrap_or(0)
    }

    pub fn clear_dead_worms(&mut self) {
        for worm_index in (0..self.worms.len()).rev() {
            if self.worms[worm_index].health <= 0 {
                self.worms.remove(worm_index);
                if self.active_worm >= worm_index {
                    self.active_worm = if self.active_worm > 0 {
                        self.active_worm - 1
                    } else if self.worms.len() > 0 {
                        self.worms.len() - 1
                    } else {
                        0
                    };
                }
            }
        }
    }

    pub fn next_active_worm(&mut self) {
        self.active_worm = (self.active_worm + 1)
            .checked_rem(self.worms.len())
            .unwrap_or(0);
    }

    fn health_score(&self) -> i32 {
        self.health() / 3
    }

    pub fn score(&self) -> i32 {
        self.moves_score + self.health_score()
    }

    pub fn active_worm_is_frozen(&self) -> bool {
        self.active_worm()
            .map(|worm| worm.rounds_until_unfrozen > 0)
            .unwrap_or(false)
    }

    pub fn active_worm_is_frozen_after_tick(&self) -> bool {
        self.active_worm()
            .map(|worm| worm.rounds_until_unfrozen > 1)
            .unwrap_or(false)
    }

    pub fn bombs(&self) -> u8 {
        self.worms.iter().map(|w| w.bombs).sum()
    }

    pub fn snowballs(&self) -> u8 {
        self.worms.iter().map(|w| w.snowballs).sum()
    }
}

#[cfg(test)]
mod test {
    use super::*;

    #[test]
    fn clear_dead_worms_after_active_worm() {
        let mut worms = ArrayVec::new();
        worms.push(Worm {
            id: 1,
            health: 50,
            position: Point2d::new(0, 0),
            rounds_until_unfrozen: 0,
            bombs: 0,
            snowballs: 0,
        });
        worms.push(Worm {
            id: 2,
            health: 10,
            position: Point2d::new(0, 0),
            rounds_until_unfrozen: 0,
            bombs: 0,
            snowballs: 0,
        });
        worms.push(Worm {
            id: 3,
            health: -2,
            position: Point2d::new(0, 0),
            rounds_until_unfrozen: 0,
            bombs: 0,
            snowballs: 0,
        });
        let mut player = Player {
            active_worm: 1,
            moves_score: 0,
            select_moves: 0,
            worms,
        };

        player.clear_dead_worms();

        assert_eq!(2, player.worms.len());
        assert_eq!(1, player.active_worm);

        assert_eq!(1, player.worms[0].id);
        assert_eq!(2, player.worms[1].id);
    }

    #[test]
    fn clear_dead_worms_before_active_worm() {
        let mut worms = ArrayVec::new();
        worms.push(Worm {
            id: 1,
            health: 0,
            position: Point2d::new(0, 0),
            rounds_until_unfrozen: 0,
            bombs: 0,
            snowballs: 0,
        });
        worms.push(Worm {
            id: 2,
            health: 10,
            position: Point2d::new(0, 0),
            rounds_until_unfrozen: 0,
            bombs: 0,
            snowballs: 0,
        });
        worms.push(Worm {
            id: 3,
            health: 2,
            position: Point2d::new(0, 0),
            rounds_until_unfrozen: 0,
            bombs: 0,
            snowballs: 0,
        });
        let mut player = Player {
            active_worm: 1,
            moves_score: 0,
            worms,
            select_moves: 0,
        };

        player.clear_dead_worms();

        assert_eq!(2, player.worms.len());
        assert_eq!(0, player.active_worm);

        assert_eq!(2, player.worms[0].id);
        assert_eq!(3, player.worms[1].id);
    }

    #[test]
    fn clear_dead_worms_before_active_worm_wrapping() {
        let mut worms = ArrayVec::new();
        worms.push(Worm {
            id: 1,
            health: 0,
            position: Point2d::new(0, 0),
            rounds_until_unfrozen: 0,
            bombs: 0,
            snowballs: 0,
        });
        worms.push(Worm {
            id: 2,
            health: 10,
            position: Point2d::new(0, 0),
            rounds_until_unfrozen: 0,
            bombs: 0,
            snowballs: 0,
        });
        worms.push(Worm {
            id: 3,
            health: 2,
            position: Point2d::new(0, 0),
            rounds_until_unfrozen: 0,
            bombs: 0,
            snowballs: 0,
        });
        let mut player = Player {
            active_worm: 0,
            moves_score: 0,
            worms,
            select_moves: 0,
        };

        player.clear_dead_worms();

        assert_eq!(2, player.worms.len());
        assert_eq!(1, player.active_worm);

        assert_eq!(2, player.worms[0].id);
        assert_eq!(3, player.worms[1].id);
    }

    #[test]
    fn clear_last_dead_worm() {
        let mut worms = ArrayVec::new();
        worms.push(Worm {
            id: 1,
            health: -10,
            position: Point2d::new(0, 0),
            rounds_until_unfrozen: 0,
            bombs: 0,
            snowballs: 0,
        });
        let mut player = Player {
            active_worm: 0,
            moves_score: 0,
            worms,
            select_moves: 0,
        };

        player.clear_dead_worms();

        assert_eq!(0, player.worms.len());
        // active worm is undefined in this case, but clearing the worms must not panic.
    }
}