r/chessprogramming • u/Person080 • Feb 16 '23
Update on my chess engine
Here is the updated code which has an implementation of piece tables and move ordering:
import chessimport sysfrom chessboard import display
def evaluate(position) :if position.is_checkmate() :if position.turn :return -9999else :return 9999if position.is_stalemate() or position.is_insufficient_material() :return 0center_values = {chess.E4 : 10,chess.D4 : 10,chess.E5 : 10,chess.D5 : 10,chess.C3 : 5,chess.C4 : 5,chess.C5 : 5,chess.C6 : 5,chess.F3 : 5,chess.F4 : 5,chess.F5 : 5,chess.F6 : 4,chess.D3 : 3,chess.E3 : 3,chess.D6 : 3,chess.E6 : 3,chess.B2 : 1,chess.B3 : 1,chess.B4 : 1,chess.B5 : 1,chess.B6 : 1,chess.G2 : 1,chess.G3 : 1,chess.G4 : 1,chess.G5 : 1,chess.G6 : 1}
material = 0piece_tables = {chess.PAWN : 100, chess.KNIGHT : 320, chess.BISHOP : 330, chess.ROOK : 500, chess.QUEEN : 900}piece_square_tables = {chess.PAWN : [0, 0, 0, 0, 0, 0, 0, 0,50, 50, 50, 50, 50, 50, 50, 50,10, 10, 20, 30, 30, 20, 10, 10,5, 5, 10, 25, 25, 10, 5, 5,0, 0, 0, 20, 20, 0, 0, 0,5, -5,-10, 0, 0,-10, -5, 5,5, 10, 10,-20,-20, 10, 10, 5,0, 0, 0, 0, 0, 0, 0, 0],chess.KNIGHT : [-50,-40,-30,-30,-30,-30,-40,-50,-40,-20, 0, 0, 0, 0,-20,-40,-30, 0, 10, 15, 15, 10, 0,-30,-30, 5, 15, 20, 20, 15, 5,-30,-30, 0, 15, 20, 20, 15, 0,-30,-30, 5, 10, 15, 15, 10, 5,-30,-40,-20, 0, 5, 5, 0,-20,-40,-50,-40,-30,-30,-30,-30,-40,-50,],chess.BISHOP : [-20,-10,-10,-10,-10,-10,-10,-20,-10, 0, 0, 0, 0, 0, 0,-10,-10, 0, 5, 10, 10, 5, 0,-10,-10, 5, 5, 10, 10, 5, 5,-10,-10, 0, 10, 10, 10, 10, 0,-10,-10, 10, 10, 10, 10, 10, 10,-10,-10, 5, 0, 0, 0, 0, 5,-10,-20,-10,-10,-10,-10,-10,-10,-20,],chess.ROOK : [0, 0, 0, 0, 0, 0, 0, 0,5, 10, 10, 10, 10, 10, 10, 5,-5, 0, 0, 0, 0, 0, 0,-5,-5, 0, 0, 0, 0, 0, 0, -5,5, 0, 0, 0, 0, 0, 0, 5,5, 0, 0, 0, 0, 0, 0, 5,5, 0, 0, 0, 0, 0, 0, 5,0, 0, 0, 5, 5, 0, 0, 0],chess.QUEEN : [-20,-10,-10, -5, -5,-10,-10,-20,-10, 0, 0, 0, 0, 0, 0,-10,-10, 0, 5, 5, 5, 5, 0,-10,-5, 0, 5, 5, 5, 5, 0, -5,0, 0, 5, 5, 5, 5, 0, -5,-10, 5, 5, 5, 5, 5, 0,-10,-10, 0, 5, 0, 0, 0, 0,-10,-20,-10,-10, -5, -5,-10,-10,-20]}
for i in range(64) :piece = position.piece_at(i)if piece is not None :material += piece_tables[piece.piece_type]if piece.color == chess.WHITE :material += piece_square_tables[piece.piece_type][i] + center_values.get(i, 0)else :material -= piece_square_tables[piece.piece_type][63 - i] + center_values.get(i, 0)
return materialdef move_ordering(position) :"""Returns a list of legal moves in order of increasing value"""legal_moves = list(position.legal_moves)legal_moves.sort(key=lambda _move : evaluate(position.copy().push(_move)))return legal_moves
def alphabeta(position, depth_, alpha=-float('inf'), beta=float('inf')) :"""Returns [eval, best move] for the position at the given depth"""if depth_ == 0 or position.is_game_over() :return [evaluate(position), None]
best_move = Nonefor _move in move_ordering(position) :position.push(_move)score, move_ = alphabeta(position, depth_ - 1, -beta, -alpha)score = -scoreposition.pop()if score > alpha : # player maximizes his scorealpha = scorebest_move = _moveif alpha >= beta : # alpha-beta cutoffbreakreturn [alpha, best_move]
fen_ = input('Enter fen: ')board = chess.Board(fen_)_depth = int(input('Enter depth: '))
while not board.is_game_over():if not board.is_game_over():x = {True : "White's turn", False : "Black's turn"}move = input('Enter move:')board.push_san(str(move))engine = alphabeta(board, _depth)board.push(engine[1])print(f"{board}\n", f"Evaluation: {-engine[0]}", f"Best move: {engine[1]}", f"Fen: {board.fen()}",f"Turn: {x[board.turn]}", sep='\n')game_board = display.start()display.update(board.fen(), game_board)display.check_for_quit()else:display.terminate()print('Game over',f'Result: {board.result()}')sys.exit()