From 3366f4895bef653c706d2d2b4d6177569cbfe1ad Mon Sep 17 00:00:00 2001 From: Jeremy Thurgood Date: Wed, 14 May 2014 21:49:43 +0200 Subject: [PATCH] Get legal moves from player. --- naja/player.py | 8 ++++++++ naja/tests/test_player.py | 10 ++++++++++ 2 files changed, 18 insertions(+) diff --git a/naja/player.py b/naja/player.py index ff253da..2156a36 100644 --- a/naja/player.py +++ b/naja/player.py @@ -91,3 +91,11 @@ class Player(object): self.position = new_position return True return False + + def legal_moves(self): + positions = [] + for direction in [BITS.NORTH, BITS.SOUTH, BITS.EAST, BITS.WEST]: + position = self.get_adjacent_position(direction) + if position is not None and self.bits.check_bit(direction): + positions.append(position) + return positions diff --git a/naja/tests/test_player.py b/naja/tests/test_player.py index 9939f58..cbae8cc 100644 --- a/naja/tests/test_player.py +++ b/naja/tests/test_player.py @@ -157,3 +157,13 @@ class TestPlayer(TestCase): player = Player(0x0f, (4, 2)) self.assertEqual(player.move(BITS.EAST), False) self.assertEqual(player.position, (4, 2)) + + def test_legal_moves_all_available(self): + player = Player(0x0f, (2, 2)) + self.assertEqual( + player.legal_moves(), [(2, 1), (2, 3), (3, 2), (1, 2)]) + + def test_legal_moves_some_unavailable(self): + player = Player(0x0f, (0, 2)) + player.bits.clear_bit(BITS.NORTH) + self.assertEqual(player.legal_moves(), [(0, 3), (1, 2)]) -- 2.34.1