diff --git a/main.py b/main.py index c0c978b..7d11f24 100644 --- a/main.py +++ b/main.py @@ -13,6 +13,7 @@ class GestureSuit(Enum): class GameMode(Enum): + KILL = "kill" TRANSFORM = "transform" @@ -43,6 +44,10 @@ def __gt__(self, other: Self): def equals(self, other: Self): return self.suit == other.suit + def kill(self): + self.alive = False + self.cell = None + def transform(self, suit: GestureSuit): self.suit = suit @@ -78,6 +83,18 @@ def remove_gesture(self): self.gesture.cell = None self.gesture = None + def _challenge_kill(self, incoming: Gesture): + incoming.cell.remove_gesture() + + if self.gesture > incoming: + incoming.kill() + self.stats[f"remaining_{incoming.suit.value}"] -= 1 + + else: + self.gesture.kill() + self.stats[f"remaining_{self.gesture.suit.value}"] -= 1 + self._assign_gesture(incoming) + def _challenge_transform(self, incoming: Gesture): if self.gesture > incoming: self.stats[f"remaining_{incoming.suit.value}"] -= 1 @@ -197,12 +214,20 @@ def _move_gesture(self, gesture: Gesture): new_cell = random.choice(surrounding_cells) new_cell.run_challenge(gesture) + def _remove_not_alive_gestures(self): + self.gestures = [g for g in self.gestures if g.alive] + def _move_gestures(self): random.shuffle(self.gestures) for gesture in self.gestures: + # gesture objs can become not alive while in this loop + if not gesture.alive: + continue self._move_gesture(gesture) + self._remove_not_alive_gestures() + def _play_round(self): self.stats["round_number"] += 1 diff --git a/tests.py b/tests.py index d8e9568..7548d1f 100644 --- a/tests.py +++ b/tests.py @@ -40,6 +40,15 @@ def test_equals(self): g2 = Gesture(gs) self.assertTrue(g1.equals(g2)) + def test_kill(self): + g = Gesture(GestureSuit.ROCK) + g.cell = Cell(MagicMock()) + + g.kill() + + self.assertFalse(g.alive) + self.assertIsNone(g.cell) + def test_transform(self): g = Gesture(GestureSuit.ROCK) g.transform(GestureSuit.PAPER) @@ -95,6 +104,7 @@ def test_remove_gesture(self): @parameterized.expand([ ("_challenge_transform", GameMode.TRANSFORM), + ("_challenge_kill", GameMode.KILL), ]) def test_get_challenge_function(self, function_name, mode): game = MagicMock(GAME_MODE=mode) @@ -369,11 +379,27 @@ def test_move_gesture_no_available_cells(self, _get_available_cells_to_move_to, _get_available_cells_to_move_to.assert_called_once_with(gesture) self.assertFalse(run_challenge.called) + def test_remove_not_alive_gestures(self): + game = RockPaperScissor() + + gesture = game.gestures[5] + gesture.kill() + + game._remove_not_alive_gestures() + self.assertNotIn(gesture, game.gestures) + + @patch.object(RockPaperScissor, "_remove_not_alive_gestures") @patch.object(RockPaperScissor, "_move_gesture") - def test_move_gestures(self, _move_gesture): + def test_move_gestures(self, _move_gesture, _remove_not_alive_gestures): game = RockPaperScissor() + + gesture = game.gestures[5] + gesture.kill() + game._move_gestures() - self.assertEqual(len(game.gestures), len(_move_gesture.call_args_list)) + self.assertFalse(call(gesture) in _move_gesture.call_args_list) + + _remove_not_alive_gestures.assert_called() @patch.object(RockPaperScissor, "_move_gestures") @patch.object(RockPaperScissor, "_print_board")