def capture(self, piece: Piece, captured_position: (int, int)): if piece.get_color() != self.__player_turn: raise BadPlayerException('Bad player') captured = self.__get_piece_at(captured_position) if captured not in piece.get_possible_captures( self.__board, self.__turn) or captured.get_color() == piece.get_color(): raise InvalidMove('Cannot capture that') self.__board.capture(piece, captured, self.__turn) self.__next_turn()
def move(self, piece: Piece, to_pos: (int, int)): if piece.get_color() != self.__player_turn: raise BadPlayerException('Bad player') if to_pos not in piece.get_possible_moves(self.__board, self.__turn): raise InvalidMove('Cannot move there') self.__board.move(piece, to_pos, self.__turn) self.__next_turn()
def line_of_sight_captures(board: Board, piece: Piece, directions: [(int, int)], max_step: int = float('inf')) \ -> set: position = piece.get_position() captures = functools.reduce(lambda x, y: x + [__get_first_seen_in_direction(board, position, y, max_step)], directions, []) captures = filter(lambda x: x.is_just(), captures) captures = list(map(lambda x: x.get(), captures)) return set(filter(lambda x: x.get_color() != piece.get_color(), captures))
def draw_selected_piece(self, selected_piece: Piece): x, y = selected_piece.get_position() gl.glPushMatrix() gl.glTranslatef(*self.__transform) gl.glTranslatef(x * self.__tile_size, y * self.__tile_size, 0) self.__draw_label(selected_piece.get_color(), '▢', self.__half_tile_size, self.__half_tile_size * 1.4, self.__piece_size * 1.25) gl.glPopMatrix()
def draw_captures(self, piece: Piece, captures: (int, int)): color = piece.get_color() for c in captures: x, y = c gl.glPushMatrix() gl.glTranslatef(*self.__transform) gl.glTranslatef(x * self.__tile_size, y * self.__tile_size, 0) self.__draw_label(color, 'C', self.__half_tile_size, self.__half_tile_size, self.__piece_size) gl.glPopMatrix()
def draw_moves(self, piece: Piece, moves: (int, int)): color = piece.get_color() for m in moves: x, y = m gl.glPushMatrix() gl.glTranslatef(*self.__transform) gl.glTranslatef(x * self.__tile_size, y * self.__tile_size, 0) self.__draw_label(color, 'M', self.__half_tile_size, self.__half_tile_size, self.__piece_size) gl.glPopMatrix()
def get_possible_moves_for(self, piece: Piece) -> set: color = piece.get_color() king = self.__white_king if color == Piece.Color.WHITE else self.__black_king moves = piece.get_possible_moves(self.__board, self.__turn) if king.is_check(self.__board, self.__turn) and piece != king: moves = set( filter(lambda m: self.__can_move_save_king(piece, king, m), moves)) if piece != king: moves = set( filter( lambda m: not king.can_move_cause_check( self.__board, piece, m, self.__turn), moves)) return moves
def get_possible_captures_for(self, piece: Piece) -> set: color = piece.get_color() king = self.__white_king if color == Piece.Color.WHITE else self.__black_king captures = piece.get_possible_captures(self.__board, self.__turn) if piece != king and king.is_check(self.__board, self.__turn): captures = list( filter(lambda c: self.__can_capture_save_king(piece, king, c), captures)) captures = set( filter( lambda c: not king.can_capture_cause_check( self.__board, piece, c, self.__turn), captures)) return set(map(lambda x: x.get_position(), captures))