From: Jeremy Thurgood Date: Wed, 14 May 2014 19:49:43 +0000 (+0200) Subject: Get legal moves from player. X-Git-Tag: 0.1~302 X-Git-Url: https://git.ctpug.org.za/?p=naja.git;a=commitdiff_plain;h=3366f4895bef653c706d2d2b4d6177569cbfe1ad Get legal moves from player. --- 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)])