def new_game(self): """Sets up a new game state by clearing off board and making all new pieces.""" # Clear board and player states self.board = [[None] * 8 for i in range(8)] self.capturedWhite = [] self.capturedBlack = [] self.playerWhite.clear() self.playerBlack.clear() # Create all the White pieces and put those objects on the board and add them to the White player temp_pieces = [ p.Rook(self.playerWhite, (0, 0)), p.Knight(self.playerWhite, (0, 1)), p.Bishop(self.playerWhite, (0, 2)), p.Queen(self.playerWhite, (0, 3)), p.King(self.playerWhite, (0, 4)), p.Bishop(self.playerWhite, (0, 5)), p.Knight(self.playerWhite, (0, 6)), p.Rook(self.playerWhite, (0, 7)) ] temp_pieces += [p.Pawn(self.playerWhite, (1, i)) for i in range(8)] for cp in temp_pieces: cp.set_board(self.board) self.playerWhite.add_piece(cp) self.board[cp.get_row()][cp.get_col()] = cp self.playerWhite.set_king(temp_pieces[4]) # Create all the Black pieces and put those objects on the board and add them to the Black player temp_pieces = [ p.Rook(self.playerBlack, (7, 0)), p.Knight(self.playerBlack, (7, 1)), p.Bishop(self.playerBlack, (7, 2)), p.Queen(self.playerBlack, (7, 3)), p.King(self.playerBlack, (7, 4)), p.Bishop(self.playerBlack, (7, 5)), p.Knight(self.playerBlack, (7, 6)), p.Rook(self.playerBlack, (7, 7)) ] temp_pieces += [p.Pawn(self.playerBlack, (6, i)) for i in range(8)] for cp in temp_pieces: cp.set_board(self.board) self.playerBlack.add_piece(cp) self.board[cp.get_row()][cp.get_col()] = cp self.playerBlack.set_king(temp_pieces[4])
def initialize_pieces(random=False, keep_prob=1.0): """Construct list of pieces as objects""" # Args: (1) random: Whether board is initialized to random initial state # (2) keep_prob: Probability of retaining piece # Returns: Python list of pieces # 1,1 = a1 ... 8,8 = h8 piece_list = [p.Rook('white',1,1), p.Knight('white',2,1), p.Bishop('white',3,1), p.Queen('white'), p.King('white'), p.Bishop('white',6,1), p.Knight('white',7,1), p.Rook('white',8,1), p.Pawn('white',1,2), p.Pawn('white',2,2), p.Pawn('white',3,2), p.Pawn('white',4,2), p.Pawn('white',5,2), p.Pawn('white',6,2), p.Pawn('white',7,2), p.Pawn('white',8,2), p.Pawn('black',1,7), p.Pawn('black',2,7), p.Pawn('black',3,7), p.Pawn('black',4,7), p.Pawn('black',5,7), p.Pawn('black',6,7), p.Pawn('black',7,7), p.Pawn('black',8,7), p.Rook('black',1,8), p.Knight('black',2,8), p.Bishop('black',3,8), p.Queen('black'), p.King('black'), p.Bishop('black',6,8), p.Knight('black',7,8), p.Rook('black',8,8)] # If random is True, randomize piece positions and activity if random: # For piece in piece list... for piece in piece_list: # Toggle activity based on uniform distribution (AND PIECE IS NOT KING) if r.random() >= keep_prob and piece.name != 'King': piece.remove() # If the piece was not removed, randomize file and rank else: newfile = r.randint(1,8) newrank = r.randint(1,8) # If there is another piece in the target tile, swap places for other_piece in piece_list: if other_piece.is_active and other_piece.file == newfile and other_piece.rank == newrank: # Swap places other_piece.file = piece.file other_piece.rank = piece.rank # Else, and in the previous case, update the piece's file and rank piece.file = newfile piece.rank = newrank piece.move_count += 1 return piece_list
def __init__(self): self.board = [["BR", "BH", "BB", "BQ", "BK", "BB", "BH", "BR"], ["BP", "BP", "BP", "BP", "BP", "BP", "BP", "BP"], ["EE", "EE", "EE", "EE", "EE", "EE", "EE", "EE"], ["EE", "EE", "EE", "EE", "EE", "EE", "EE", "EE"], ["EE", "EE", "EE", "EE", "EE", "EE", "EE", "EE"], ["EE", "EE", "EE", "EE", "EE", "EE", "EE", "EE"], ["WP", "WP", "WP", "WP", "WP", "WP", "WP", "WP"], ["WR", "WH", "WB", "WQ", "WK", "WB", "WH", "WR"]] self.black_vision = set() self.white_vision = set() self.current_player = "W" self.turn_count = 0 # Initialize board with Piece objects for x in range(8): for y in range(8): piece = self.board[x][y] if piece == "EE": continue color = piece[0] p_type = piece[1] if p_type == "Q": self.board[x][y] = pieces.Queen(color, x, y) elif p_type == "R": self.board[x][y] = pieces.Rook(color, x, y) elif p_type == "B": self.board[x][y] = pieces.Bishop(color, x, y) elif p_type == "P": self.board[x][y] = pieces.Pawn(color, x, y) elif p_type == "K": self.board[x][y] = pieces.King(color, x, y) elif p_type == "H": self.board[x][y] = pieces.Knight(color, x, y) self.white_king = self.board[7][4] self.black_king = self.board[0][4] current_vision = set() other_vision = set() for y in range(8): for x in range(8): piece = self.get_piece(x, y) if str(piece)[0] == self.current_player: current_vision.add((y, x)) current_vision = current_vision.union( self.board[y][x].vision(self)) elif str(piece) != "EE": other_vision.add((y, x)) other_vision = other_vision.union( self.board[y][x].vision(self)) self.white_vison = current_vision self.black_vision = other_vision
def readPiecesArrays(save, c): wp_array = save.readline().split(',') k = 0 if c == "W": globVar.w_pieces = [] n = globVar.w_NumPieces else: globVar.b_pieces = [] n = globVar.b_NumPieces for i in range(n): color = wp_array[k] k += 1 selected = wp_array[k] if selected == "True": sl = True else: sl = False k += 1 type = wp_array[k] k += 1 label = int(wp_array[k]) k += 1 row = int(wp_array[k]) k += 1 col = int(wp_array[k]) k += 1 if type == "none": pc = pieces.Pawn("none", "none") elif type == "pawn": pc = pieces.Pawn(color, type) elif type == "rook": pc = pieces.Rook(color, type) elif type == "bishop": pc = pieces.Bishop(color, type) elif type == "queen": pc = pieces.Queen(color, type) elif type == "king": pc = pieces.King(color, type) elif type == "knight": pc = pieces.Knight(color, type) pc.selected = sl pc.label = label pc.row = row pc.col = col if c == "W": globVar.w_pieces.append(pc) else: globVar.b_pieces.append(pc)
def __init_piece_by_name(self, n, white): white = int(white) if n == 'P': return p.Pawn(white) elif n == 'R': return p.Rook(white) elif n == 'N': return p.Knight(white) elif n == 'B': return p.Bishop(white) elif n == 'Q': return p.Queen(white) elif n == 'K': return p.King(white)
def move(self, move): row, col = move.move_end[0], move.move_end[1] piece = self.board[move.white].pop( (move.move_start[0], move.move_start[1])) op = 0 if move.white else 1 if type(piece) is p.King and piece.can_castle: if abs(col - move.move_start[1]) > 1: if col == 6: self.board[move.white][(row, 5)] = self.board[move.white].pop( (row, 7)) self.board[move.white][(row, 5)].can_castle = False elif col == 1: self.board[move.white][(row, 2)] = self.board[move.white].pop( (row, 0)) self.board[move.white][(row, 2)].can_castle = False piece.can_castle = False pawn_promotion = False if type(piece) is p.Pawn: pawn_promotion = row in (0, 7) piece.en_passant = False if piece.move_range == 3: piece.move_range = 2 if abs(row - move.move_start[0]) == 2: piece.en_passant = True try: if move.white: if self.board[op][(row + 1, col)].en_passant: self.__kill_piece(op, row + 1, col) else: if self.board[op][(row - 1, col)].en_passant: self.__kill_piece(op, row - 1, col) except (KeyError, AttributeError): pass if col > 7: if col == 9: piece = p.Queen(move.white) elif col == 10: piece = p.Rook(move.white) elif col == 11: piece = p.Bishop(move.white) elif col == 12: piece = p.Knight(move.white) row, col = move.move_start self.__kill_piece(op, row, col) if type(piece) is p.Rook: piece.can_castle = False self.board[move.white][(row, col)] = piece return pawn_promotion
def makeGrid(): # global grid colorSwitcher = {"white": "black", "black": "white"} # If playerColor = White, tileColor = black, and vice versa tileColor = "black" for row in range(0, 8): rowTiles = [] for col in range(0, 8): rowTiles.append(Square(None, tileColor)) tileColor = colorSwitcher[tileColor] grid.append(rowTiles) tileColor = colorSwitcher[tileColor] # init pawns for col in range(0, 8): grid[1][col].piece = pieces.Pawn("W", 1, col) grid[6][col].piece = pieces.Pawn("B", 6, col) # init rest of pieces grid[0][0].piece = pieces.Rook("W", 0, 0) grid[0][1].piece = pieces.Knight("W", 0, 1) grid[0][2].piece = pieces.Bishop("W", 0, 2) grid[0][3].piece = pieces.Queen("W", 0, 3) grid[0][4].piece = pieces.King("W", 0, 4) grid[0][5].piece = pieces.Bishop("W", 0, 5) grid[0][6].piece = pieces.Knight("W", 0, 6) grid[0][7].piece = pieces.Rook("W", 0, 7) grid[7][0].piece = pieces.Rook("B", 6, 0) grid[7][1].piece = pieces.Knight("B", 6, 1) grid[7][2].piece = pieces.Bishop("B", 6, 2) grid[7][3].piece = pieces.Queen("B", 6, 3) grid[7][4].piece = pieces.King("B", 6, 4) grid[7][5].piece = pieces.Bishop("B", 6, 5) grid[7][6].piece = pieces.Knight("B", 6, 6) grid[7][7].piece = pieces.Rook("B", 6, 7)
def initialise_board(self): "empty spaces" self._board.loc[[3, 4, 5, 6]] = None "pawns" for board_file in self._board.columns.values: self._board.loc[2] = pieces.Pawn(WHITE, board_file, 2) self._board.loc[7] = pieces.Pawn(BLACK, board_file, 7) "rooks" self._board.loc[1, 'A'] = pieces.Rook(WHITE, 'A', 1) self._board.loc[8, 'A'] = pieces.Rook(BLACK, 'A', 8) self._board.loc[1, 'H'] = pieces.Rook(WHITE, 'H', 1) self._board.loc[8, 'H'] = pieces.Rook(BLACK, 'H', 8) "knights" self._board.loc[1, 'B'] = pieces.Knight(WHITE, 'B', 1) self._board.loc[8, 'B'] = pieces.Knight(BLACK, 'B', 8) self._board.loc[1, 'G'] = pieces.Knight(WHITE, 'G', 1) self._board.loc[8, 'G'] = pieces.Knight(BLACK, 'G', 8) "bishops" self._board.loc[1, 'C'] = pieces.Bishop(WHITE, 'C', 1) self._board.loc[8, 'C'] = pieces.Bishop(BLACK, 'C', 8) self._board.loc[1, 'F'] = pieces.Bishop(WHITE, 'F', 1) self._board.loc[8, 'F'] = pieces.Bishop(BLACK, 'F', 8) "queens" self._board.loc[1, 'D'] = pieces.Queen(WHITE, 'D', 1) self._board.loc[8, 'D'] = pieces.Queen(BLACK, 'D', 8) "kings" self._board.loc[1, 'E'] = pieces.King(WHITE, 'E', 1) self._board.loc[8, 'E'] = pieces.King(BLACK, 'E', 8) print(self._board)
def set_up(self): for x in range(8): self.add(pieces.Pawn(x, 1, True)) self.add(pieces.Pawn(x, 1, False)) for x in [0, 7]: self.add(pieces.Rook(x, 0, True)) self.add(pieces.Rook(x, 0, False)) for x in [1, 6]: self.add(pieces.Knight(x, 0, True)) self.add(pieces.Knight(x, 0, False)) for x in [2, 5]: self.add(pieces.Bishop(x, 0 ,True)) self.add(pieces.Bishop(x, 0, False)) self.add(pieces.Queen(3, 0, True)) self.add(pieces.Queen(3, 0, False)) self.add(pieces.King(4, 0, True)) self.add(pieces.King(4, 0, False)) return self
def place_pieces(white_team: Team, black_team: Team): # Add knights. black_team.pieces.extend([ pieces.Knight(screen.squares[7][6], black_team), pieces.Knight(screen.squares[7][1], black_team) ]) white_team.pieces.extend([ pieces.Knight(screen.squares[0][1], white_team), pieces.Knight(screen.squares[0][6], white_team) ]) # Add bishops. black_team.pieces.extend([ pieces.Bishop(screen.squares[7][2], black_team), pieces.Bishop(screen.squares[7][5], black_team) ]) white_team.pieces.extend([ pieces.Bishop(screen.squares[0][2], white_team), pieces.Bishop(screen.squares[0][5], white_team) ]) # Add queens. black_team.pieces.append(pieces.Queen(screen.squares[7][4], black_team)) white_team.pieces.append(pieces.Queen(screen.squares[0][4], white_team)) # Add rooks. black_team.pieces.extend([ pieces.Rook(screen.squares[7][0], black_team), pieces.Rook(screen.squares[7][7], black_team) ]) white_team.pieces.extend([ pieces.Rook(screen.squares[0][0], white_team), pieces.Rook(screen.squares[0][7], white_team) ]) add_pawns(white_team, black_team) # Add kings. black_team.pieces.append(pieces.King(black_team)) white_team.pieces.append(pieces.King(white_team))
def pieceConstructor(clr, type): if type == "none": pc = pieces.Pawn("none", "none") elif type == "pawn": pc = pieces.Pawn(clr, type) elif type == "rook": pc = pieces.Rook(clr, type) elif type == "bishop": pc = pieces.Bishop(clr, type) elif type == "queen": pc = pieces.Queen(clr, type) elif type == "king": pc = pieces.King(clr, type) elif type == "knight": pc = pieces.Knight(clr, type) return pc
def __init_piece(self, row, col, white): piece = None if row in (1, 6): piece = p.Pawn(white) elif row in (0, 7): if col in (0, 7): piece = p.Rook(white) elif col in (1, 6): piece = p.Knight(white) elif col in (2, 5): piece = p.Bishop(white) elif col == 3: piece = p.Queen(white) elif col == 4: piece = p.King(white) return piece
def replace(pawn_team: Team, pawn): pawn_team.pieces.remove(pawn) print("Which piece do you want instead of the pawn?") piece_chose = input("q - queen, b - bishop, r - rook, k - knight\n") option_to_piece = { 'q': pieces.Queen(pawn.square, pawn.team), 'b': pieces.Bishop(pawn.square, pawn.team), 'r': pieces.Rook(pawn.team, pawn.square), 'k': pieces.Knight(pawn.square, pawn.team), } try: pawn_team.pieces.append(option_to_piece[piece_chose]) pawn.square.current_piece = option_to_piece[piece_chose] except KeyError: pawn_team.pieces.append(pawn) replace(pawn_team, pawn)
def pawn_promotion(self, pos): print("Promotion! - Bishop, Knight, Rook, or Queen?") promotion_choice = input() color = self.player_1_color if self.player_turn == 1 else self.player_2_color pawn = self.board.get_piece(pos) if promotion_choice == "Queen": self.board.spaces[pos.y][pos.x] = pieces.Queen(team=pawn.team) if promotion_choice == "Rook": self.board.spaces[pos.y][pos.x] = pieces.Rook(team=pawn.team) if promotion_choice == "Knight": self.board.spaces[pos.y][pos.x] = pieces.Knight(team=pawn.team) if promotion_choice == "Bishop": self.board.spaces[pos.y][pos.x] = pieces.Bishop(team=pawn.team)
def make_piece(self, piece, ra, fi): shorthand = piece.shorthand color = piece.color if shorthand == "": new_piece = pieces.Pawn(ra, fi, color) elif shorthand == "R": new_piece = pieces.Rook(ra, fi, color) elif shorthand == "N": new_piece = pieces.Knight(ra, fi, color) elif shorthand == "B": new_piece = pieces.Bishop(ra, fi, color) elif shorthand == "K": new_piece = pieces.King(ra, fi, color) elif shorthand == "Q": new_piece = pieces.Queen(ra, fi, color) new_piece.kill() new_piece.moves = list(piece.moves) new_piece.has_moved = piece.has_moved new_piece.en_passant_able = piece.en_passant_able self.board[ra][fi] = new_piece
def apply_move(self, game_state: state.GameState) -> state.GameState: """Return a new GameState object with the Move applied to it.""" promoting_piece = game_state.pieces_by_position[self.old_position] # Add new piece if self.new_piece_name == "Q": game_state.piece_list.append(pieces.Queen(self.new_position, self.piece.colour, True)) elif self.new_piece_name == "R": game_state.piece_list.append(pieces.Rook(self.new_position, self.piece.colour, True)) elif self.new_piece_name == "B": game_state.piece_list.append(pieces.Bishop(self.new_position, self.piece.colour, True)) elif self.new_piece_name == "N": game_state.piece_list.append(pieces.Knight(self.new_position, self.piece.colour, True)) else: raise ValueError("Invalid new piece! Promotion routine went wrong- aborting...") # Delete old pawn game_state.piece_list.remove(promoting_piece) # Add to move list game_state.move_history.append(self) return game_state
def populate(): place = 0 global grid # fill grid with empty Squares for i in range(16): grid.append([]) for j in range(16): grid[i].append(Square(False, "b", pieces.Piece("none", "pawn"), i, j)) # alternate colors for i in range(8): if (i % 2 == 0): place = 0 for j in range(8): grid[i][place].pieceStatus = False grid[i][place].color = "white" grid[i][place].row = i grid[i][place].col = place grid[i][place + 1].pieceStatus = False grid[i][place + 1].color = "black" grid[i][place + 1].row = i grid[i][place + 1].col = (place + 1) place += 2 else: place = 0 for k in range(8): grid[i][place].pieceStatus = False grid[i][place].color = "black" grid[i][place].row = i grid[i][place].col = place grid[i][place + 1].pieceStatus = False grid[i][place + 1].color = "white" grid[i][place + 1].row = i grid[i][place + 1].col = (place + 1) place += 2 # Fill board with pieces # black plr = "b" Grid(0,0).piece = pieces.Rook(plr, "rook") Grid(0,1).piece = pieces.Knight(plr, "knight") Grid(0,2).piece = pieces.Bishop(plr, "bishop") Grid(0,3).piece = pieces.Queen(plr, "queen") Grid(0,4).piece = pieces.King(plr, "king") Grid(0,5).piece = pieces.Bishop(plr, "bishop") Grid(0,6).piece = pieces.Knight(plr, "knight") Grid(0,7).piece = pieces.Rook(plr, "rook") for i in range(8): Grid(1,i).piece = pieces.Pawn(plr, "pawn") Grid(1,i).piece.firstMove = True globVar.firstPawns.append(Grid(1,i).piece) # set pieceStatus and piece_ID for black pieces piece_ID = 0 for i in range(2): for j in range(8): Grid(i,j).pieceStatus = True Grid(i,j).piece.label = piece_ID piece_ID += 1 globVar.b_pieces.append(Grid(i,j).piece) # white plr = "W" Grid(7,0).piece = pieces.Rook(plr, "rook") Grid(7,1).piece = pieces.Knight(plr, "knight") Grid(7,2).piece = pieces.Bishop(plr, "bishop") Grid(7,3).piece = pieces.Queen(plr, "queen") Grid(7,4).piece = pieces.King(plr, "king") Grid(7,5).piece = pieces.Bishop(plr, "bishop") Grid(7,6).piece = pieces.Knight(plr, "knight") Grid(7,7).piece = pieces.Rook(plr, "rook") for i in range(8): Grid(6,i).piece = pieces.Pawn(plr, "pawn") Grid(6,i).piece.firstMove = True globVar.firstPawns.append(Grid(6,i).piece) # set pieceStatus and assign ID to white pieces place = 6 piece_ID = 0 for i in range(2): i = place for j in range(8): Grid(i,j).pieceStatus = True Grid(i,j).piece.label = piece_ID piece_ID += 1 globVar.w_pieces.append(Grid(i,j).piece) place += 1 # set pieceStatus to false for the rest place = 2 for i in range(4): i = place for j in range(8): Grid(i,j).pieceStatus = False # copy square coordinates to pieces for i in range(8): for j in range(8): if Grid(i,j).pieceStatus: Grid(i,j).piece.row = Grid(i,j).row Grid(i,j).piece.col = Grid(i,j).col # Clip board to 8 x 8 while len(grid) > 8: grid.pop() for i in range(8): while len(grid[i]) > 8: grid[i].pop() # initialize global variables resetGlobal()
def layout_pieces( self, game_class): #Probably make this take in a pgn file later!! game = game_class row = [1, 8] side = ['White', 'Black'] #0 is white, 1 is black p_row = [2, 7] KN = [0, 0] QN = [0, 0] KB = [0, 0] QB = [0, 0] KR = [0, 0] QR = [0, 0] king = [0, 0] queen = [0, 0] self.piece_dict = {} #Ties canvas Id to pointer to class instance for i in range(2): r = row[i] c = side[i] p_r = p_row[i] pc_file = self.pieces_files[self.pieces_set] knight_file = '{}/{}{}.png'.format(pc_file, c, 'Knight') #Board class, canvas, color, image, size, coords QN[i] = pcs.Knight(self, self.board, game, i, knight_file, self.sqr_size, start_coords=self.get_coords('B{}'.format(r))) self.piece_dict[QN[i].Id] = QN[i] KN[i] = pcs.Knight(self, self.board, game, i, knight_file, self.sqr_size, start_coords=self.get_coords('G{}'.format(r))) self.piece_dict[KN[i].Id] = KN[i] bishop_file = '{}/{}{}.png'.format(pc_file, c, 'Bishop') QB[i] = pcs.Bishop(self, self.board, game, i, bishop_file, self.sqr_size, start_coords=self.get_coords('C{}'.format(r))) self.piece_dict[QB[i].Id] = QB[i] KB[i] = pcs.Bishop(self, self.board, game, i, bishop_file, self.sqr_size, start_coords=self.get_coords('F{}'.format(r))) self.piece_dict[KB[i].Id] = KB[i] rook_file = '{}/{}{}.png'.format(pc_file, c, 'Rook') QR[i] = pcs.Rook(self, self.board, game, i, rook_file, self.sqr_size, start_coords=self.get_coords('A{}'.format(r))) self.piece_dict[QR[i].Id] = QR[i] KR[i] = pcs.Rook(self, self.board, game, i, rook_file, self.sqr_size, start_coords=self.get_coords('H{}'.format(r))) self.piece_dict[KR[i].Id] = KR[i] king_file = '{}/{}{}.png'.format(pc_file, c, 'King') king[i] = pcs.King(self, self.board, game, i, king_file, self.sqr_size, start_coords=self.get_coords('E{}'.format(r))) self.piece_dict[king[i].Id] = king[i] queen_file = '{}/{}{}.png'.format(pc_file, c, 'Queen') queen[i] = pcs.Queen(self, self.board, game, i, queen_file, self.sqr_size, start_coords=self.get_coords('D{}'.format(r))) self.piece_dict[queen[i].Id] = queen[i] pawn_file = '{}/{}{}.png'.format(pc_file, c, 'Pawn') for p in range(8): #CHANGE TO PAWN!!! p_spot = '{}{}'.format(self.alpha[p + 1], p_r) pawn = pcs.Pawn(self, self.board, game, i, pawn_file, self.sqr_size, start_coords=self.get_coords(p_spot)) self.piece_dict[pawn.Id] = pawn
def generate_pieces(colour): return [pieces.Rook(colour), pieces.Knight(colour), pieces.Bishop(colour), pieces.Queen(colour), pieces.King(colour), pieces.Bishop(colour), pieces.Knight(colour), pieces.Rook(colour)]
def move_piece(board, target, kings, origin, destination, captures, promotion): global transcript, turn_number # start transcript if target.colour == 'white': turn_number += 1 transcript += f'{turn_number}. ' # piece move conditions for row in board: for piece in row: if piece and piece.name == 'pawn' and piece.en_passant: piece.en_passant = False promoting = False if target.name == 'pawn': if target.double_move: target.double_move = False if abs(origin[1] - destination[1]) == 2: target.en_passant = True if origin[0] != destination[0] and not board[destination[1]][destination[0]]: captures.append(board[destination[1] - target.direction][destination[0]]) board[destination[1] - target.direction][destination[0]] = None transcript += coords_to_notation(origin)[0] if destination[1] == (0 if target.colour == 'white' else 7): promoting = True piece_dict = {'queen': pieces.Queen(target.colour), 'knight': pieces.Knight(target.colour), 'rook': pieces.Rook(target.colour), 'bishop': pieces.Bishop(target.colour)} if target.name == 'king': kings[int(target.colour == "black")] = destination if target.castle_rights: target.castle_rights = False if destination[0] - origin[0] == 2: board[target.back_rank][5] = board[target.back_rank][7] board[target.back_rank][7] = None transcript += 'O-O ' if origin[0] - destination[0] == 2: board[target.back_rank][3] = board[target.back_rank][0] board[target.back_rank][0] = None transcript += 'O-O-O ' if target.name == 'rook' and target.castle_rights: target.castle_rights = False # finish transcript if transcript[-2] != 'O': if target.name != 'pawn': transcript += target.name[0].upper() if target.name != 'knight' else 'N' elif board[destination[1]][destination[0]]: transcript += coords_to_notation(origin)[0] transcript += f'x{coords_to_notation(destination)} ' if board[destination[1]][destination[0]] else f'{coords_to_notation(destination)} ' # add any existing piece to captures list if board[destination[1]][destination[0]]: captures.append(board[destination[1]][destination[0]]) # move piece if not promoting: board[destination[1]][destination[0]] = target else: board[destination[1]][destination[0]] = piece_dict[promotion] transcript = transcript[:-1] + f'={promotion[0].upper()} ' if promotion != 'knight' else '=N ' board[origin[1]][origin[0]] = None # any checks with new board status enemy_king = kings[int(target.colour == "white")] check = board[enemy_king[1]][enemy_king[0]].in_check(board, enemy_king) return board, captures, kings, check
def state_maker(piece_list): output_state = state.GameState([]) output_state.piece_list = piece_list output_state.pieces_by_position = dict(zip([piece.position for piece in output_state.piece_list], output_state.piece_list)) output_state.captured_pieces = [] output_state.whose_turn = "white" output_state.can_castle = state.can_castle(output_state) output_state.move_history = [] return output_state new_game_pieces = [pieces.Rook((0,7), "black"), pieces.Knight((1,7), "black"), pieces.Bishop((2,7), "black"), pieces.Queen((3,7), "black"), pieces.King((4,7), "black"), pieces.Bishop((5,7), "black"), pieces.Knight((6,7), "black"), pieces.Rook((7,7), "black"), pieces.Pawn((0,6), "black"), pieces.Pawn((1,6), "black"), pieces.Pawn((2,6), "black"), pieces.Pawn((3,6), "black"), pieces.Pawn((4,6), "black"), pieces.Pawn((5,6), "black"), pieces.Pawn((6,6), "black"), pieces.Pawn((7,6), "black"), pieces.Pawn((0,1), "white"), pieces.Pawn((1,1), "white"),
def test_bishop(self, name, side, coord, expected, allied_pieces={}): p = pieces.Bishop(side) self.assertEqual( set(p.get_moves(coord[0], coord[1], allied_pieces)), set(expected))
piece = pieces.Pawn() print str(piece.points) + ": " + piece.name foo.add_piece(piece) bar = branch.Branch(piece, (1, 1)) foo.apply_branch(bar) piece = pieces.Knight() print str(piece.points) + ": " + piece.name foo.add_piece(piece) bar = branch.Branch(piece, (2, 2)) foo.apply_branch(bar) piece = pieces.Bishop() print str(piece.points) + ": " + piece.name foo.add_piece(piece) bar = branch.Branch(piece, (3, 3)) foo.apply_branch(bar) piece = pieces.Rook() print str(piece.points) + ": " + piece.name foo.add_piece(piece) bar = branch.Branch(piece, (4, 4)) foo.apply_branch(bar) piece = pieces.Queen() print str(piece.points) + ": " + piece.name
def initialize_pieces(self): """ Initializes pieces on the right locations on the right teams """ self.white_pieces = { "P": [ pieces.Pawn("white", self.coordinates_map[6][x]) for x in range(9) ], "B": [pieces.Bishop("white", self.coordinates_map[7][1])], "R": [pieces.Rook("white", self.coordinates_map[7][7])], "L": [ pieces.Lance("white", self.coordinates_map[8][0]), pieces.Lance("white", self.coordinates_map[8][8]) ], "N": [ pieces.Knight("white", self.coordinates_map[8][1]), pieces.Knight("white", self.coordinates_map[8][7]) ], "S": [ pieces.Silver("white", self.coordinates_map[8][2]), pieces.Silver("white", self.coordinates_map[8][6]) ], "G": [ pieces.Gold("white", self.coordinates_map[8][3]), pieces.Gold("white", self.coordinates_map[8][5]) ], "K": [pieces.King("white", self.coordinates_map[8][4])] } self.white_captives = { "P": [], "L": [], "N": [], "S": [], "G": [], "B": [], "R": [] } self.black_pieces = { "P": [ pieces.Pawn("black", self.coordinates_map[2][x]) for x in range(9) ], "B": [pieces.Bishop("black", self.coordinates_map[1][7])], "R": [pieces.Rook("black", self.coordinates_map[1][1])], "L": [ pieces.Lance("black", self.coordinates_map[0][0]), pieces.Lance("black", self.coordinates_map[0][8]) ], "N": [ pieces.Knight("black", self.coordinates_map[0][1]), pieces.Knight("black", self.coordinates_map[0][7]) ], "S": [ pieces.Silver("black", self.coordinates_map[0][2]), pieces.Silver("black", self.coordinates_map[0][6]) ], "G": [ pieces.Gold("black", self.coordinates_map[0][3]), pieces.Gold("black", self.coordinates_map[0][5]) ], "K": [pieces.King("black", self.coordinates_map[0][4])] } self.black_captives = { "P": [], "L": [], "N": [], "S": [], "G": [], "B": [], "R": [] } # places the pieces on the board for key in self.white_pieces: for value in self.white_pieces[key]: self.board_pieces_location[int(8 - ( (value.pos[1] - 62.5) // 75))][int( (value.pos[0] - 132.5) // 75)] = value self.all_pieces.append(value.sprite) for value in self.black_pieces[key]: value.sprite.angle = 180 self.board_pieces_location[int(8 - ( (value.pos[1] - 62.5) // 75))][int( (value.pos[0] - 132.5) // 75)] = value self.all_pieces.append(value.sprite)
def start_game(): # White pieces wr1 = p.Rook(sett.files[0], sett.rows[0], sett.piece_types[0], sett.teams[0]) wkn1 = p.Knight(sett.files[1], sett.rows[0], sett.piece_types[1], sett.teams[0]) wb1 = p.Bishop(sett.files[2], sett.rows[0], sett.piece_types[2], sett.teams[0]) wq = p.Queen(sett.files[3], sett.rows[0], sett.piece_types[3], sett.teams[0]) wk = p.King(sett.files[4], sett.rows[0], sett.piece_types[4], sett.teams[0]) wb2 = p.Bishop(sett.files[5], sett.rows[0], sett.piece_types[2], sett.teams[0]) wkn2 = p.Knight(sett.files[6], sett.rows[0], sett.piece_types[1], sett.teams[0]) wr2 = p.Rook(sett.files[7], sett.rows[0], sett.piece_types[0], sett.teams[0]) master_board.place_piece(wr1) master_board.place_piece(wkn1) master_board.place_piece(wb1) master_board.place_piece(wq) master_board.place_piece(wk) master_board.place_piece(wb2) master_board.place_piece(wkn2) master_board.place_piece(wr2) for i in range(8): pawn = p.Pawn(sett.files[i], sett.rows[1], sett.piece_types[5], sett.teams[0]) master_board.place_piece(pawn) # Black pieces br1 = p.Rook(sett.files[0], sett.rows[7], sett.piece_types[0], sett.teams[1]) bkn1 = p.Knight(sett.files[1], sett.rows[7], sett.piece_types[1], sett.teams[1]) bb1 = p.Bishop(sett.files[2], sett.rows[7], sett.piece_types[2], sett.teams[1]) bq = p.Queen(sett.files[3], sett.rows[7], sett.piece_types[3], sett.teams[1]) bk = p.King(sett.files[4], sett.rows[7], sett.piece_types[4], sett.teams[1]) bb2 = p.Bishop(sett.files[5], sett.rows[7], sett.piece_types[2], sett.teams[1]) bkn2 = p.Knight(sett.files[6], sett.rows[7], sett.piece_types[1], sett.teams[1]) br2 = p.Rook(sett.files[7], sett.rows[7], sett.piece_types[0], sett.teams[1]) master_board.place_piece(br1) master_board.place_piece(bkn1) master_board.place_piece(bb1) master_board.place_piece(bq) master_board.place_piece(bk) master_board.place_piece(bb2) master_board.place_piece(bkn2) master_board.place_piece(br2) for i in range(8): pawn = p.Pawn(sett.files[i], sett.rows[6], sett.piece_types[5], sett.teams[1]) master_board.place_piece(pawn) master_board.init_coloured_pieces() for peece in master_board.coloured_pieces['white']: peece.create_vision(master_board) for peece in master_board.coloured_pieces['black']: peece.create_vision(master_board)