def new(cls): chess_pieces = [[0 for x in range(Board.WIDTH)] for y in range(Board.HEIGHT)] # Create pawns. for x in range(Board.WIDTH): chess_pieces[x][Board.HEIGHT-2] = pieces.Pawn(x, Board.HEIGHT-2, pieces.Piece.WHITE) chess_pieces[x][1] = pieces.Pawn(x, 1, pieces.Piece.BLACK) # Create rooks. chess_pieces[0][Board.HEIGHT-1] = pieces.Rook(0, Board.HEIGHT-1, pieces.Piece.WHITE) chess_pieces[Board.WIDTH-1][Board.HEIGHT-1] = pieces.Rook(Board.WIDTH-1, Board.HEIGHT-1, pieces.Piece.WHITE) chess_pieces[0][0] = pieces.Rook(0, 0, pieces.Piece.BLACK) chess_pieces[Board.WIDTH-1][0] = pieces.Rook(Board.WIDTH-1, 0, pieces.Piece.BLACK) # Create Knights. chess_pieces[1][Board.HEIGHT-1] = pieces.Knight(1, Board.HEIGHT-1, pieces.Piece.WHITE) chess_pieces[Board.WIDTH-2][Board.HEIGHT-1] = pieces.Knight(Board.WIDTH-2, Board.HEIGHT-1, pieces.Piece.WHITE) chess_pieces[1][0] = pieces.Knight(1, 0, pieces.Piece.BLACK) chess_pieces[Board.WIDTH-2][0] = pieces.Knight(Board.WIDTH-2, 0, pieces.Piece.BLACK) # Create Bishops. chess_pieces[2][Board.HEIGHT-1] = pieces.Bishop(2, Board.HEIGHT-1, pieces.Piece.WHITE) chess_pieces[Board.WIDTH-3][Board.HEIGHT-1] = pieces.Bishop(Board.WIDTH-3, Board.HEIGHT-1, pieces.Piece.WHITE) chess_pieces[2][0] = pieces.Bishop(2, 0, pieces.Piece.BLACK) chess_pieces[Board.WIDTH-3][0] = pieces.Bishop(Board.WIDTH-3, 0, pieces.Piece.BLACK) # Create King & Queen. chess_pieces[4][Board.HEIGHT-1] = pieces.King(4, Board.HEIGHT-1, pieces.Piece.WHITE) chess_pieces[3][Board.HEIGHT-1] = pieces.Queen(3, Board.HEIGHT-1, pieces.Piece.WHITE) chess_pieces[4][0] = pieces.King(4, 0, pieces.Piece.BLACK) chess_pieces[3][0] = pieces.Queen(3, 0, pieces.Piece.BLACK) return cls(chess_pieces, False, False)
def set_pieces(self): black = [] black.append(pieces.Rook(0, 0, BLACK)) black.append(pieces.Rook(0, 7, BLACK)) black.append(pieces.Knight(0, 1, BLACK)) black.append(pieces.Knight(0, 6, BLACK)) black.append(pieces.Bishop(0, 2, BLACK)) black.append(pieces.Bishop(0, 5, BLACK)) black.append(pieces.Queen(0, 3, BLACK)) black.append(pieces.King(0, 4, BLACK)) for x in range(8): black.append(pieces.Pawn(1, x, BLACK)) white = [] white.append(pieces.Rook(7, 0, WHITE)) white.append(pieces.Rook(7, 7, WHITE)) white.append(pieces.Knight(7, 1, WHITE)) white.append(pieces.Knight(7, 6, WHITE)) white.append(pieces.Bishop(7, 2, WHITE)) white.append(pieces.Bishop(7, 5, WHITE)) white.append(pieces.Queen(7, 3, WHITE)) white.append(pieces.King(7, 4, WHITE)) for x in range(8): white.append(pieces.Pawn(6, x, WHITE)) self.pieces = black + white
def createBoard(self): board = [['.']*8 for _ in range(8)] board[0][0] = p.Rook(0, 0, 'B') board[0][1] = p.Knight(0, 1, 'B') board[0][2] = p.Bishop(0, 2, 'B') board[0][3] = p.Queen(0, 3, 'B') board[0][4] = p.King(0, 4, 'B') board[0][5] = p.Bishop(0, 5, 'B') board[0][6] = p.Knight(0, 6, 'B') board[0][7] = p.Rook(0, 7, 'B') for i in range(0,8): board[1][i] = p.Pawn(1, i, 'B') board[7][0] = p.Rook(7, 0, 'W') board[7][1] = p.Knight(7, 1, 'W') board[7][2] = p.Bishop(7, 2, 'W') board[7][3] = p.Queen(7, 3, 'W') board[7][4] = p.King(7, 4, 'W') board[7][5] = p.Bishop(7, 5, 'W') board[7][6] = p.Knight(7, 6, 'W') board[7][7] = p.Rook(7, 7, 'W') for i in range(0,8): board[6][i] = p.Pawn(6, i, 'W') return board
def init_pieces(self): piece_list = [] back_row = 0 # add pawns for row in range(8): if self.player == "white": self.piece_list.append(pieces.Pawn(row, 1, self.player)) elif self.player == "black": self.piece_list.append(pieces.Pawn(row, 6, self.player)) else: raise Exception("Player must be white or black") # set row depending on color if self.player == "black": back_row = 7 self.piece_list.append(pieces.Rook(0, back_row, self.player)) self.piece_list.append(pieces.Rook(7, back_row, self.player)) self.piece_list.append(pieces.Knight(1, back_row, self.player)) self.piece_list.append(pieces.Knight(6, back_row, self.player)) self.piece_list.append(pieces.Bishop(2, back_row, self.player)) self.piece_list.append(pieces.Bishop(5, back_row, self.player)) self.piece_list.append(pieces.Queen(3, back_row, self.player)) self.piece_list.append(pieces.King(4, back_row, self.player)) return piece_list
def init_starting_position(self): self.board.add_all( (pieces.Rook((0, 0, 1), "white"), pieces.Queen((1, 0, 1), "white"), pieces.Pawn((0, 1, 1), "white"), pieces.Pawn( (1, 1, 1), "white"), pieces.Pawn( (1, 2, 0), "white"), pieces.Pawn( (2, 2, 0), "white"), pieces.Pawn((3, 2, 0), "white"), pieces.Pawn((4, 2, 0), "white"), pieces.Knight( (1, 1, 0), "white"), pieces.Bishop( (2, 1, 0), "white"), pieces.Bishop((3, 1, 0), "white"), pieces.Knight((4, 1, 0), "white"), pieces.King( (4, 0, 1), "white"), pieces.Rook( (5, 0, 1), "white"), pieces.Pawn( (4, 1, 1), "white"), pieces.Pawn((5, 1, 1), "white"), pieces.Rook((0, 9, 5), "black"), pieces.Queen( (1, 9, 5), "black"), pieces.Pawn( (0, 8, 5), "black"), pieces.Pawn( (1, 8, 5), "black"), pieces.Pawn((1, 7, 4), "black"), pieces.Pawn((2, 7, 4), "black"), pieces.Pawn((3, 7, 4), "black"), pieces.Pawn((4, 7, 4), "black"), pieces.Knight( (1, 8, 4), "black"), pieces.Bishop( (2, 8, 4), "black"), pieces.Bishop((3, 8, 4), "black"), pieces.Knight((4, 8, 4), "black"), pieces.King( (4, 9, 5), "black"), pieces.Rook( (5, 9, 5), "black"), pieces.Pawn( (4, 8, 5), "black"), pieces.Pawn((5, 8, 5), "black")))
def _set_default(self): # unfortunately python doesn't have real 2D lists board = [[ pieces.Rook(0, 0), pieces.Knight(0, 1), pieces.Bishop(0, 2), pieces.Queen(0, 3), pieces.King(0, 4), pieces.Bishop(0, 5), pieces.Knight(0, 6), pieces.Rook(0, 7) ]] board.append([pieces.Pawn(1, i) for i in range(8)]) board.extend([['o' for _ in range(8)] for __ in range(4)]) board.append([pieces.Pawn(6, i, player.Color.BLACK) for i in range(8)]) board.append([ pieces.Rook(7, 0, player.Color.BLACK), pieces.Knight(7, 1, player.Color.BLACK), pieces.Bishop(7, 2, player.Color.BLACK), pieces.Queen(7, 3, player.Color.BLACK), pieces.King(7, 4, player.Color.BLACK), pieces.Bishop(7, 5, player.Color.BLACK), pieces.Knight(7, 6, player.Color.BLACK), pieces.Rook(7, 7, player.Color.BLACK) ]) return board
def initialize_board(): board = np.ndarray((8, 8), dtype=np.object) for i in range(0, 8): pieces.append(pcs.Pawn('b', (1, i))) for i in range(0, 8): pieces.append(pcs.Pawn('w', (6, i))) pieces.append(pcs.Beeshop('b', (0, 2))) pieces.append(pcs.Beeshop('b', (0, 5))) pieces.append(pcs.Beeshop('w', (7, 2))) pieces.append(pcs.Beeshop('w', (7, 5))) pieces.append(pcs.Knight('b', (0, 1))) pieces.append(pcs.Knight('b', (0, 6))) pieces.append(pcs.Knight('w', (7, 1))) pieces.append(pcs.Knight('w', (7, 6))) pieces.append(pcs.Rook('b', (0, 0))) pieces.append(pcs.Rook('b', (0, 7))) pieces.append(pcs.Rook('w', (7, 0))) pieces.append(pcs.Rook('w', (7, 7))) pieces.append(pcs.Queen('b', (0, 3))) pieces.append(pcs.Queen('w', (7, 3))) pieces.append(pcs.King('b', (0, 4))) pieces.append(pcs.King('w', (7, 4))) for piece in pieces: board[piece.pos] = piece return board, pieces
def testAddPiece(self): board = ChessBoard.ChessBoard() pawn1 = pieces.Pawn(PieceColor.WHITE) pawn2 = pieces.Pawn(PieceColor.BLACK) rook1 = pieces.Rook(PieceColor.WHITE) rook2 = pieces.Rook(PieceColor.BLACK) bishop1 = pieces.Bishop(PieceColor.BLACK) bishop2 = pieces.Bishop(PieceColor.WHITE) knight1 = pieces.Knight(PieceColor.BLACK) knight2 = pieces.Knight(PieceColor.WHITE) queen1 = pieces.Queen(PieceColor.BLACK) queen2 = pieces.Queen(PieceColor.WHITE) king1 = pieces.King(PieceColor.BLACK) king2 = pieces.King(PieceColor.WHITE) self.assertTrue(board.addPiece('2a', pawn1)) self.assertTrue(board.addPiece('7a', pawn2)) self.assertTrue(board.addPiece('1a', rook1)) self.assertTrue(board.addPiece('8a', rook2)) self.assertTrue(board.addPiece('8b', knight1)) self.assertTrue(board.addPiece('1b', knight2)) self.assertTrue(board.addPiece('8c', bishop1)) self.assertTrue(board.addPiece('1c', bishop2)) self.assertTrue(board.addPiece('8d', queen1)) self.assertTrue(board.addPiece('1d', queen2)) self.assertTrue(board.addPiece('8e', king1)) self.assertTrue(board.addPiece('1e', king2)) #try to add piece at invalid coordinates self.assertFalse(board.addPiece('9e', king2)) self.assertFalse(board.addPiece('9a', king2)) #try to add piece at non empty cell self.assertFalse(board.addPiece('1d', queen2)) self.assertFalse(board.addPiece('1e', king2))
def setup_pieces(self): # Top team for num in range(0, 8): self.board.set_piece(Vec2(num, 1), pieces.Pawn(team=2)) self.board.set_piece(Vec2(0, 0), pieces.Rook(team=2)) self.board.set_piece(Vec2(7, 0), pieces.Rook(team=2)) self.board.set_piece(Vec2(1, 0), pieces.Knight(team=2)) self.board.set_piece(Vec2(6, 0), pieces.Knight(team=2)) self.board.set_piece(Vec2(2, 0), pieces.Bishop(team=2)) self.board.set_piece(Vec2(5, 0), pieces.Bishop(team=2)) self.p2_king = self.board.set_piece(Vec2(3, 0), pieces.King(team=2)) self.board.set_piece(Vec2(4, 0), pieces.Queen(team=2)) # Bottom Team for num in range(0, 8): self.board.set_piece(Vec2(num, 6), pieces.Pawn(team=1)) self.board.set_piece(Vec2(0, 7), pieces.Rook(team=1)) self.board.set_piece(Vec2(7, 7), pieces.Rook(team=1)) self.board.set_piece(Vec2(1, 7), pieces.Knight(team=1)) self.board.set_piece(Vec2(6, 7), pieces.Knight(team=1)) self.board.set_piece(Vec2(2, 7), pieces.Bishop(team=1)) self.board.set_piece(Vec2(5, 7), pieces.Bishop(team=1)) self.p1_king = self.board.set_piece(Vec2(4, 7), pieces.King(team=1)) self.board.set_piece(Vec2(3, 7), pieces.Queen(team=1)) # cool stuff self.board.print_all_moves_for_piece(Vec2(4, 7))
def __init__(self, player): self.king = pieces.King(4, 7 if (player.color == "black") else 0, player) self.queen = pieces.Queen(3, 7 if (player.color == "black") else 0, player) self.pawns = [] self.knights = [] self.bishops = [] self.rooks = [] self.knights.append( pieces.Knight(1, 7 if (player.color == "black") else 0, player)) self.knights.append( pieces.Knight(6, 7 if (player.color == "black") else 0, player)) self.bishops.append( pieces.Bishop(2, 7 if (player.color == "black") else 0, player)) self.bishops.append( pieces.Bishop(5, 7 if (player.color == "black") else 0, player)) self.rooks.append( pieces.Rook(0, 7 if (player.color == "black") else 0, player)) self.rooks.append( pieces.Rook(7, 7 if (player.color == "black") else 0, player)) for i in range(8): self.pawns.append( pieces.Pawn(i, 6 if (player.color == "black") else 1, player)) self.allPieces = self.pawns + self.bishops + self.knights + self.rooks + [ self.queen, self.king ]
def fen_to_board(fen): """Returns list of pieces from FEN position Args: fen ([str]): [description] Raises: Exception: [Print 'ass' in polish on unwanted case] Returns: [list]: FEN is the standard chess notation. It is used to describe position of chess game FEN can be stored in single line string""" board = [] i, j = 0, 0 for char in fen: if char.isalpha(): if char == 'p': board.append(pieces.Pawn(i, j, is_black=True)) elif char == 'P': board.append(pieces.Pawn(i, j, is_black=False)) elif char == 'n': board.append(pieces.Knight(i, j, is_black=True)) elif char == 'N': board.append(pieces.Knight(i, j, is_black=False)) elif char == 'b': board.append(pieces.Bishop(i, j, is_black=True)) elif char == 'B': board.append(pieces.Bishop(i, j, is_black=False)) elif char == 'r': board.append(pieces.Rook(i, j, is_black=True)) elif char == 'R': board.append(pieces.Rook(i, j, is_black=False)) elif char == 'q': board.append(pieces.Queen(i, j, is_black=True)) elif char == 'Q': board.append(pieces.Queen(i, j, is_black=False)) elif char == 'k': board.append(pieces.King(i, j, is_black=True)) elif char == 'K': board.append(pieces.King(i, j, is_black=False)) i += 1 elif char == '/': j += 1 i = 0 elif 1 <= int(char) <= 8: i += int(char) else: raise Exception("dupa") return board
def __init__(self, white_name, black_name): self.b = board.Board() self.white = players.Player(name=white_name, color='white', board=self.b) self.black = players.Player(name=black_name, color='black', board=self.b) self.b[0][0].set(pieces.Rook('white')) self.b[1][0].set(pieces.Knight('white')) self.b[2][0].set(pieces.Bishop('white')) self.b[3][0].set(pieces.Queen('white')) self.b[4][0].set(pieces.King('white')) self.b[5][0].set(pieces.Bishop('white')) self.b[6][0].set(pieces.Knight('white')) self.b[7][0].set(pieces.Rook('white')) self.b[0][1].set(pieces.Pawn('white')) self.b[1][1].set(pieces.Pawn('white')) self.b[2][1].set(pieces.Pawn('white')) self.b[3][1].set(pieces.Pawn('white')) self.b[4][1].set(pieces.Pawn('white')) self.b[5][1].set(pieces.Pawn('white')) self.b[6][1].set(pieces.Pawn('white')) self.b[7][1].set(pieces.Pawn('white')) self.b[0][6].set(pieces.Pawn('black')) self.b[1][6].set(pieces.Pawn('black')) self.b[2][6].set(pieces.Pawn('black')) self.b[3][6].set(pieces.Pawn('black')) self.b[4][6].set(pieces.Pawn('black')) self.b[5][6].set(pieces.Pawn('black')) self.b[6][6].set(pieces.Pawn('black')) self.b[7][6].set(pieces.Pawn('black')) self.b[0][7].set(pieces.Rook('black')) self.b[1][7].set(pieces.Knight('black')) self.b[2][7].set(pieces.Bishop('black')) self.b[3][7].set(pieces.Queen('black')) self.b[4][7].set(pieces.King('black')) self.b[5][7].set(pieces.Bishop('black')) self.b[6][7].set(pieces.Knight('black')) self.b[7][7].set(pieces.Rook('black')) # white to move first self.current_player = self.white
def make_piece(self, piece, ra, fi, color): x = self.file_to_x(fi) y = self.rank_to_y(ra) if piece == "": piece = pieces.Pawn(ra, fi, color) elif piece == "R": piece = pieces.Rook(ra, fi, color) elif piece == "N": piece = pieces.Knight(ra, fi, color) elif piece == "B": piece = pieces.Bishop(ra, fi, color) elif piece == "K": piece = pieces.King(ra, fi, color) elif piece == "Q": piece = pieces.Queen(ra, fi, color) elif piece == 0: return piece.set_rect(x, y, self.space_height) piece.set_image(self.space_height) self.board[ra][fi] = piece
def promote(pos, row, col, color): # calculate the position of the rects rectDim = 150 rectRow = None rectCol = None if pos[0] + rectDim >= height: rectRow = pos[0] - rectDim - 1 else: rectRow = pos[0] if pos[1] + rectDim >= width: rectCol = pos[1] - rectDim - 1 else: rectCol = pos[1] rects = [] imgs = [] imgRects = [] # display the rects for x in [rectRow, rectRow + int(rectDim / 2) + 1]: for y in [rectCol, rectCol + int(rectDim / 2) + 1]: rects.append( pygame.draw.rect(screen, (255, 255, 255), (x, y, int(rectDim / 2), int(rectDim / 2)))) pygame.draw.line(screen, (50, 50, 50), (rectRow + int(rectDim / 2), rectCol), (rectRow + int(rectDim / 2), rectCol + rectDim)) pygame.draw.line(screen, (50, 50, 50), (rectRow, rectCol + int(rectDim / 2)), (rectRow + rectDim, rectCol + int(rectDim / 2))) # display the imgs for piece in ["q", "r", "n", "b"]: imgs.append(pygame.image.load("imgs/" + color[0] + piece + ".png")) imgRects.append(imgs[len(imgs) - 1].get_rect()) imgRects[len(imgs) - 1].center = rects[len(imgs) - 1].center screen.blit(imgs[len(imgs) - 1], imgRects[len(imgs) - 1]) pygame.display.update() # transform the pawn into another piece while True: for event in pygame.event.get(): if event.type == pygame.MOUSEBUTTONUP: if rects[0].collidepoint(event.pos): pieces.Queen(row, col, color) board.getPiece(row, col).draw(screen, width, height) return if rects[1].collidepoint(event.pos): pieces.Rook(row, col, color) board.getPiece(row, col).draw(screen, width, height) return if rects[2].collidepoint(event.pos): pieces.Knight(row, col, color) board.getPiece(row, col).draw(screen, width, height) return if rects[3].collidepoint(event.pos): pieces.Bishop(row, col, color) board.getPiece(row, col).draw(screen, width, height) return
def apply_move(self, game_state: state.GameState) -> state.GameState: promoting_piece = game_state.pieces_by_position[self.old_position] captured_piece = game_state.pieces_by_position[self.captured_piece.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 and captured piece game_state.piece_list.remove(promoting_piece) game_state.piece_list.remove(captured_piece) # Add to move list game_state.move_history.append(self) return game_state
def check_pawn(pc): W_success = (pc.type == "pawn") and (globVar.player == "W") and (pc.row == 0) b_success = (pc.type == "pawn") and (globVar.player == "b") and (pc.row == 7) if W_success or b_success: if ((globVar.numPlayers == 1 and globVar.player == "W") or globVar.numPlayers == 2): choice = Canvas.pawn_to_new() else: choice = random.randint(1, 4) color = pc.color label = pc.label row = pc.row col = pc.col if choice == 1: pc = pieces.Rook(color, "rook") elif choice == 2: pc = pieces.Knight(color, "knight") elif choice == 3: pc = pieces.Bishop(color, "bishop") elif choice == 4: pc = pieces.Queen(color, "queen") pc.label = label pc.row = row pc.col = col updatePieces(pc) board.uGrid(pc)
def clickdialog(x,todestroy,board,p2,sel): if(x=='King'): pawn=pieces.King('white') pawn.place(board) elif(x=='Queen'): pawn=pieces.Queen('white') pawn.place(board) elif(x=='Knight'): pawn=pieces.Knight('white') pawn.place(board) elif(x=='Bishop'): pawn=pieces.Bishop('white') pawn.place(board) elif(x=='Rook'): pawn=pieces.Rook('white') pawn.place(board) board[p2]=pawn sel.selected_piece = None sel.hilighted = None sel.pieces = {} sel.chessboard.get_enemy('black') sel.refresh() sel.draw_pieces() sel.canvas.after(200, sel.opponent) sel.canvas.after(200, sel.refresh) sel.canvas.after(200, sel.draw_pieces) todestroy.destroy()
def create_board(self): """ Creates the initial game board required for Chess. Populated spaces are filled with respective pieces from pieces.py. Vacant spaces are filled with 0s. #TODO replace the strings with actual pieces from the pieces class """ board = [[0 for y in range(8)] for x in range(8)] #Placing the Rooks board[0][0] = pieces.Rook('B') board[0][7] = pieces.Rook('B') board[7][0] = pieces.Rook('W') board[7][7] = pieces.Rook('W') #Placing the Knights board[0][1] = pieces.Knight('B') board[0][6] = pieces.Knight('B') board[7][1] = pieces.Knight('W') board[7][6] = pieces.Knight('W') #Placing the Bishops board[0][2] = pieces.Bishop('B') board[0][5] = pieces.Bishop('B') board[7][2] = pieces.Bishop('W') board[7][5] = pieces.Bishop('W') #Placing the Queens board[0][3] = pieces.Queen('B') board[7][3] = pieces.Queen('W') #Placing the Kings board[0][4] = pieces.King('B') board[7][4] = pieces.King('W') #Placing the Pawns for i in range(8): board[1][i] = pieces.Pawn('B') board[6][i] = pieces.Pawn('W') return board
def from_fen(cls, fen): """""" # Parse FEN position, color, castling, en_passant, halfmove, fullmove = fen.split() # Parse position setup = list() # Loop over all ranks in FEN for rank in position.split('/'): # Start new rank setup.append(list()) # Loop over each individual piece for piece in rank: # Get color from piece if piece.islower(): piece_color = pieces.Color.BLACK else: piece_color = pieces.Color.WHITE # Set specific piece if piece.lower() == 'p': setup[-1].append(pieces.Pawn(piece_color)) elif piece.lower() == 'n': setup[-1].append(pieces.Knight(piece_color)) elif piece.lower() == 'b': setup[-1].append(pieces.Bishop(piece_color)) elif piece.lower() == 'r': setup[-1].append(pieces.Rook(piece_color)) elif piece.lower() == 'q': setup[-1].append(pieces.Queen(piece_color)) elif piece.lower() == 'k': setup[-1].append(pieces.King(piece_color)) else: for _ in range(int(piece)): setup[-1].append(None) # Setup board as numpy array setup = np.asarray(setup, dtype=object) # Ensure we have 2 dimensions assert setup.ndim == 2, "FEN notation differed per rank." # Create board board = cls( n_files=setup.shape[0], n_ranks=setup.shape[1], ) # Setup board board.board = setup board.color = color board.castling = castling board.en_passant = en_passant board.halfmove = int(halfmove) board.fullmove = int(fullmove) # Return result return board
def user_perform_move(self, move, status): if status == CASTLE_LEFT: yy = 0 if self.status == WHITE: yy = 7 self.chesspieces[0][yy].x = 3 self.chesspieces[3][yy] = self.chesspieces[0][yy] self.chesspieces[0][yy] = 0 self.chesspieces[4][yy].x = 2 self.chesspieces[2][yy] = self.chesspieces[4][yy] self.chesspieces[4][yy] = 0 return if status == CASTLE_RIGHT: yy = 0 if self.status == WHITE: yy = 7 self.chesspieces[7][yy].x = 5 self.chesspieces[5][yy] = self.chesspieces[7][yy] self.chesspieces[7][yy] = 0 self.chesspieces[4][yy].x = 6 self.chesspieces[6][yy] = self.chesspieces[4][yy] self.chesspieces[4][yy] = 0 return piece = self.chesspieces[move.xfrom][move.yfrom] piece.move += 1 piece.x = move.xto piece.y = move.yto if status == EN_PASSANT: self.chesspieces[move.xto][move.yfrom] = 0 self.pawn_double_move = None if self.pawn_double_move != None: self.pawn_double_move.en_passant = False self.pawn_double_move = None if status == PROMOTION: if move.promotion == None or move.promotion == 'q': self.chesspieces[move.xto][move.yto] = pieces.Queen( move.xto, move.yto, piece.color) if move.promotion == 'b': self.chesspieces[move.xto][move.yto] = pieces.Bishop( move.xto, move.yto, piece.color) elif move.promotion == 'n': self.chesspieces[move.xto][move.yto] = pieces.Knight( move.xto, move.yto, piece.color) elif move.promotion == 'r': self.chesspieces[move.xto][move.yto] = pieces.Rook( move.xto, move.yto, piece.color) self.chesspieces[move.xfrom][move.yfrom] = 0 return if status == PAWN_DOBULE_MOVE: piece.en_passant = True self.pawn_double_move = self.chesspieces[move.xfrom][move.yfrom] elif status == EN_PASSANT: self.chesspieces[move.xto][move.yfrom] = 0 self.chesspieces[move.xto][move.yto] = piece self.chesspieces[move.xfrom][move.yfrom] = 0
def handle_promotion( self, src_rank: int, src_file: int, dst_rank: int, dst_file: int, ) -> None: """Handle moves involving promotion of a piece. This involves the following actions: 1. If a pawn reaches the last rank, prompt the user for a piece to promote to. 2. If the piece is chosen, replace the pawn by said piece. Parameters ---------- src_rank : int Rank of source square to move from. src_file : int File of source square to move from. dst_rank : int Rank of destination square to move to. dst_file : int File of destination square to move to. """ # Check if the move promotes a pawn if self.is_promotion(src_rank, src_file, dst_rank, dst_file): # Get possible promotion pieces possibilities = set('nbrq') # Initialise promotion piece piece = None # Query user until we receive a correct promotion piece while piece not in possibilities: # Query the user for a piece piece = self.query_promotion() # Get color of pawn color = self.board[src_rank, src_file].color # Create new piece if piece == 'q': piece = pieces.Queen(color) elif piece == 'r': piece = pieces.Rook(color) elif piece == 'b': piece = pieces.Bishop(color) elif piece == 'n': piece = pieces.Knight(color) else: raise ValueError( f"Unknown piece {piece}, should be one of {possibilities}") # Transform pawn to piece self.board[src_rank, src_file] = piece
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 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 __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 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 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 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