X-Git-Url: https://git.ctpug.org.za/?a=blobdiff_plain;f=naja%2Fplayer.py;h=892fa05b32f88c117a59f98d6025a7b0821a5469;hb=7e4bfa583e543435076471d46a53aaff1eccb83f;hp=d4ff73211a54db98354f18c55c02fa5557e9a918;hpb=8f1005d3b07d61d44d5a8a0302099126f7f2e6a9;p=naja.git diff --git a/naja/player.py b/naja/player.py index d4ff732..892fa05 100644 --- a/naja/player.py +++ b/naja/player.py @@ -57,15 +57,17 @@ class PlayerBits(object): wrap = self.bits << (8 - shift) & 0xff self.bits = (self.bits >> shift | wrap) + 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): @@ -148,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, @@ -155,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