Get legal moves from player.
authorJeremy Thurgood <firxen@gmail.com>
Wed, 14 May 2014 19:49:43 +0000 (21:49 +0200)
committerJeremy Thurgood <firxen@gmail.com>
Wed, 14 May 2014 19:49:43 +0000 (21:49 +0200)
naja/player.py
naja/tests/test_player.py

index ff253da35ea3113216240649fa4dfe8a0dbf5dab..2156a3665c8f64e21452f5e8bffd6b1750bc8146 100644 (file)
@@ -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
index 9939f58b31c4118953f0c0b6e5f5b6cc07dd4b66..cbae8cc23c03580a14f26f9f9043a90ba124b680 100644 (file)
@@ -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)])