Untitled
unknown
plain_text
2 months ago
4.9 kB
6
Indexable
import tkinter as tk import chess import random # Function to evaluate the board (simple material evaluation) def evaluate_board(board): piece_values = { chess.PAWN: 1, chess.KNIGHT: 3, chess.BISHOP: 3, chess.ROOK: 5, chess.QUEEN: 9, chess.KING: 0 } evaluation = 0 for square in chess.SQUARES: piece = board.piece_at(square) if piece: piece_value = piece_values.get(piece.piece_type, 0) if piece.color == chess.WHITE: evaluation += piece_value else: evaluation -= piece_value return evaluation # Minimax algorithm with Alpha-Beta Pruning def minimax(board, depth, alpha, beta, maximizing_player): if depth == 0 or board.is_game_over(): return evaluate_board(board) if maximizing_player: max_eval = float('-inf') for move in board.legal_moves: board.push(move) eval = minimax(board, depth-1, alpha, beta, False) max_eval = max(max_eval, eval) alpha = max(alpha, eval) if beta <= alpha: break board.pop() return max_eval else: min_eval = float('inf') for move in board.legal_moves: board.push(move) eval = minimax(board, depth-1, alpha, beta, True) min_eval = min(min_eval, eval) beta = min(beta, eval) if beta <= alpha: break board.pop() return min_eval # Function to choose the best move def best_move(board): best_move = None best_value = float('-inf') alpha = float('-inf') beta = float('inf') for move in board.legal_moves: board.push(move) move_value = minimax(board, 4, alpha, beta, False) # Searching 4 moves ahead if move_value > best_value: best_value = move_value best_move = move board.pop() return best_move # GUI Application for Chess class ChessApp: def __init__(self, root): self.root = root self.root.title("Chess Game") # Initialize the chess board self.board = chess.Board() # Create a canvas to draw the chessboard self.canvas = tk.Canvas(self.root, width=800, height=800) self.canvas.pack() # Draw the initial board self.draw_board() # Bind mouse click events for moves self.canvas.bind("<Button-1>", self.on_click) # Initialize selected square for user move self.selected_square = None def draw_board(self): # Draw the chessboard and pieces for row in range(8): for col in range(8): x1 = col * 100 y1 = row * 100 x2 = x1 + 100 y2 = y1 + 100 color = "white" if (row + col) % 2 == 0 else "black" self.canvas.create_rectangle(x1, y1, x2, y2, fill=color) piece = self.board.piece_at(chess.square(col, 7-row)) if piece: piece_name = self.get_piece_unicode(piece) self.canvas.create_text(x1 + 50, y1 + 50, text=piece_name, font=("Arial", 48)) def get_piece_unicode(self, piece): # Returns the Unicode character for the piece piece_unicode = { chess.PAWN: {chess.WHITE: "♙", chess.BLACK: "♟"}, chess.KNIGHT: {chess.WHITE: "♘", chess.BLACK: "♞"}, chess.BISHOP: {chess.WHITE: "♗", chess.BLACK: "♝"}, chess.ROOK: {chess.WHITE: "♖", chess.BLACK: "♜"}, chess.QUEEN: {chess.WHITE: "♕", chess.BLACK: "♛"}, chess.KING: {chess.WHITE: "♔", chess.BLACK: "♚"} } return piece_unicode.get(piece.piece_type, {}).get(piece.color, "") def on_click(self, event): # Handle user clicking on the board to make a move col = event.x // 100 row = 7 - (event.y // 100) # Convert from pixel to row (flipped y-axis) square = chess.square(col, row) if self.selected_square: move = chess.Move(self.selected_square, square) if move in self.board.legal_moves: self.board.push(move) self.draw_board() # Redraw the board after the move self.selected_square = None self.ai_move() # Let the AI play its move else: self.selected_square = square def ai_move(self): # Let the AI play after the user move print("AI's move (White):") move = best_move(self.board) self.board.push(move) self.draw_board() # Redraw the board after the AI move # Run the app root = tk.Tk() app = ChessApp(root) root.mainloop()
Editor is loading...
Leave a Comment