def find_potential_positions(self): """ Get all the positions one-square ahead, two-squares ahead, and diagonally ahead of the current position. """ current_col = self.current_position[0] take_col_indices = [ COLNAMES.index(current_col) - 1, COLNAMES.index(current_col) + 1 ] if self.colour == "WHITE": direction = 1 else: direction = -1 pos_one_ahead = (self.current_position[0], self.current_position[1] + direction) pos_two_ahead = (self.current_position[0], self.current_position[1] + 2 * direction) pos_takes = [] for i in take_col_indices: if i in range(8): pos_takes.append( (COLNAMES[i], self.current_position[1] + direction)) pass pass return pos_one_ahead, pos_two_ahead, pos_takes
def can_castle(self, board, pos): """ Can we castle to given position? """ if self.has_moved: return False row = 1 if self.colour == "WHITE" else 8 current_colnum = COLNAMES.index(self.current_position[0]) target_colnum = COLNAMES.index(pos[0]) if target_colnum < current_colnum: direction = -1 rook_column = "A" else: direction = 1 rook_column = "H" ## check if the rook is there and hasn't moved if board.is_empty((rook_column, row)): return False else: p = board.piece_at((rook_column, row)) if p.piece_type != "Rook" or p.has_moved: return False ## now check if the coast is clear to the target square ## (no other pieces, not going through check. for colnum in range(current_colnum, target_colnum + direction, direction): col = COLNAMES[colnum] if board.is_threatened((col, row), self.colour): return False if colnum != current_colnum and not board.is_empty((col, row)): return False return True
def is_castling_move(self, start_pos, end_pos): """ Return True if the piece at start_pos is a king, and end_pos is more than one row away. """ if self.board.is_empty(start_pos): return False if not self.board.piece_at(start_pos).piece_type == "King": return False start_colnum = COLNAMES.index(start_pos[0]) end_colnum = COLNAMES.index(end_pos[0]) is_castling = abs(start_colnum - end_colnum) > 1 return is_castling
def find_potential_positions(self): """ Find all the squares one around the current position, plus the endpoints of castling. """ adjacent_positions = [] current_colnum = COLNAMES.index(self.current_position[0]) for row_move in range(-1, 2): new_row = self.current_position[1] + row_move if new_row not in range(1, 9): continue for col_move in range(-1, 2): if row_move == 0 and col_move == 0: continue new_colnum = current_colnum + col_move if new_colnum not in range(8): continue adjacent_positions.append((COLNAMES[new_colnum], new_row)) ## we will deal with castling separately if self.colour == "WHITE": castling_positions = [("C", 1), ("G", 1)] else: castling_positions = [("C", 8), ("G", 8)] return adjacent_positions, castling_positions
def find_available_moves(self, board): self.available_moves = [] self.threatens = [] current_col = self.current_position[0] current_row = self.current_position[1] current_colnum = COLNAMES.index(current_col) loop_configs = { "left": range(current_colnum - 1, -1, -1), "right": range(current_colnum + 1, 8), "up": range(current_row + 1, 9), "down": range(current_row - 1, 0, -1) } ## go as far as we can in each direction for direction, loop in loop_configs.items(): for i in loop: if direction == "left" or direction == "right": pos = (COLNAMES[i], current_row) else: pos = (COLNAMES[current_colnum], i) if board.is_empty(pos): self.available_moves.append(pos) self.threatens.append(pos) elif board.piece_at(pos).colour != self.colour: ## opposite colour piece - can take, ## but can't move beyond self.threatens.append(pos) self.available_moves.append(pos) break else: ## same colour piece - can't go there or beyond self.threatens.append(pos) break
def find_available_moves(self, board): self.available_moves = [] self.threatens = [] starting_row = self.current_position[1] starting_colnum = COLNAMES.index(self.current_position[0]) steps = [(1, 2), (2, 1), (-1, 2), (2, -1), (1, -2), (-2, -1), (-2, 1), (-1, -2)] for step in steps: colnum = starting_colnum + step[0] row = starting_row + step[1] if not (row in range(1, 9) and colnum in range(8)): continue pos = (COLNAMES[colnum], row) if board.is_empty(pos): self.available_moves.append(pos) self.threatens.append(pos) elif board.piece_at(pos).colour != self.colour: self.available_moves.append(pos) self.threatens.append(pos) else: self.threatens.append(pos)
def find_available_moves(self, board): self.available_moves = [] self.threatens = [] starting_row = self.current_position[1] starting_colnum = COLNAMES.index(self.current_position[0]) step_directions = [(1, 1), (1, -1), (-1, 1), (-1, -1)] for direction in step_directions: colnum = starting_colnum + direction[0] row = starting_row + direction[1] while colnum in range(8) and row in range(1, 9): pos = (COLNAMES[colnum], row) if board.is_empty(pos): self.threatens.append(pos) self.available_moves.append(pos) elif board.piece_at(pos).colour != self.colour: self.available_moves.append(pos) self.threatens.append(pos) break else: self.threatens.append(pos) break colnum += direction[0] row += direction[1]