All puzzles to have cards without default actions.
[naja.git] / naja / player.py
index fc95a8fa55f66990d2116782d2e836c3af1429cf..892fa05b32f88c117a59f98d6025a7b0821a5469 100644 (file)
@@ -63,10 +63,11 @@ class Player(object):
     A representation of the player.
     """
 
-    def __init__(self, bits, position, movement_mode=None):
+    def __init__(self, bits, position, movement_mode=None, gameboard=None):
         self.bits = PlayerBits(bits)
         self.position = position
         self.movement_mode = movement_mode if movement_mode else MOVES.ADJACENT
+        self.gameboard = gameboard
 
     @classmethod
     def import_player(cls, definition):
@@ -149,6 +150,19 @@ class Player(object):
             return True
         return False
 
+    def set_gameboard(self, gameboard):
+        self.gameboard = gameboard
+
+    def pos_has_action(self, pos):
+        card = self.gameboard.board_locations[pos]
+        for action in card.actions:
+            if self.bits.check_bits(action.required_bits):
+                return True
+        return False
+
+    def filter_moves_with_no_actions(self, positions):
+        return [pos for pos in positions if self.pos_has_action(pos)]
+
     def legal_moves(self):
         POSITION_FUNCTION = {
             MOVES.ADJACENT: self.get_adjacent_positions,
@@ -156,7 +170,8 @@ class Player(object):
             MOVES.BISHOP: self.get_bishop_positions,
             MOVES.CASTLE: self.get_castle_positions,
         }
-        return POSITION_FUNCTION[self.movement_mode]()
+        positions = POSITION_FUNCTION[self.movement_mode]()
+        return self.filter_moves_with_no_actions(positions)
 
     def allow_chess_move(self, chesspiece):
         self.movement_mode = chesspiece