X-Git-Url: https://git.ctpug.org.za/?a=blobdiff_plain;f=naja%2Fplayer.py;h=892fa05b32f88c117a59f98d6025a7b0821a5469;hb=64cf509d389faaecca0bdb494aec1e1d712558b2;hp=2aad55b029a4c323cd4582f42727496a0215d4d2;hpb=266021313a1501c86c11c803851c2b4bb50a8c00;p=naja.git diff --git a/naja/player.py b/naja/player.py index 2aad55b..892fa05 100644 --- a/naja/player.py +++ b/naja/player.py @@ -49,16 +49,25 @@ class PlayerBits(object): for bit in bits: self.toggle_bit(bit) + def shift_bits_left(self, shift): + wrap = self.bits >> (8 - shift) + self.bits = (self.bits << shift & 0xff | wrap) + + def shift_bits_right(self, shift): + 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): @@ -141,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, @@ -148,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