import chess import chess.pgn from stockfish import Stockfish from lama2923 import clear_screen, llinput import datetime def get_elo(wend=""): return int(llinput("ELO değerini girin: ", wend=wend, forceint=True, forceinput=True, min_length=1, max_length=4)) def Custom_FEN(): def check_fen(fen): try: chess.Board(fen) return True except ValueError: return False return llinput("FEN: ", forceinput=True, min_length=1, max_length=71, custom_enter_check_func=check_fen) stockfish = Stockfish(path="/usr/games/stockfish") #! stockfish PATH | girin heeeeee girmezsen olmaz !!!!!!!! stockfish.set_depth(15) #? kendine göre ayarla (20 den fazlasını önermem) white_elo_rating = get_elo(" (Beyaz)") black_elo_rating = get_elo(" (Siyah)") if llinput("Özel FEN kullanmak ister misiniz? (e/h): ", forceinput=True, choices=(["e", "h"], False)).lower() == "e": FEN = Custom_FEN() board = chess.Board(FEN) else: FEN = chess.Board().fen() board = chess.Board() def get_skill_level(elo_rating): return max(0, min(20, elo_rating // 50)) move_history = [] unicode_pieces = { "P": "\033[1m♟︎\033[0m", "N": "\033[1m♞\033[0m", "B": "\033[1m♝\033[0m", "R": "\033[1m♜\033[0m", "Q": "\033[1m♛\033[0m", "K": "\033[1m♚\033[0m", "p": "\033[1m♙\033[0m", "n": "\033[1m♘\033[0m", "b": "\033[1m♗\033[0m", "r": "\033[1m♖\033[0m", "q": "\033[1m♕\033[0m", "k": "\033[1m♔\033[0m" } def print_board(): stockfish_evaluation = stockfish.get_evaluation() if stockfish_evaluation["type"] == "mate": eval_display = f"# {'+' if stockfish_evaluation['value'] > 0 else '-'}{abs(stockfish_evaluation['value'])}" else: eval_score = stockfish_evaluation["value"] eval_score = max(min(eval_score, 1000), -1000) eval_display = f"{eval_score / 100:+.2f}" initial_counts = {chess.PAWN: 8, chess.KNIGHT: 2, chess.BISHOP: 2, chess.ROOK: 2, chess.QUEEN: 1} piece_values = {chess.PAWN: 1, chess.KNIGHT: 3, chess.BISHOP: 3, chess.ROOK: 5, chess.QUEEN: 9} white_captured, black_captured = [], [] material_difference = 0 for piece_type, count in initial_counts.items(): white_on_board = len(board.pieces(piece_type, chess.WHITE)) black_on_board = len(board.pieces(piece_type, chess.BLACK)) white_captured_count = count - white_on_board black_captured_count = count - black_on_board material_difference += (white_captured_count - black_captured_count) * piece_values[piece_type] white_captured.extend([unicode_pieces[chess.Piece(piece_type, chess.BLACK).symbol()]] * black_captured_count) black_captured.extend([unicode_pieces[chess.Piece(piece_type, chess.WHITE).symbol()]] * white_captured_count) white_score = abs(material_difference) if material_difference < 0 else 0 black_score = abs(material_difference) if material_difference > 0 else 0 board_lines = [ "\n a b c d e f g h", " ┌───┬───┬───┬───┬───┬───┬───┬───┐" ] for rank in range(7, -1, -1): row = f" {rank + 1} │" for file in range(8): piece = board.piece_at(chess.square(file, rank)) row += f" {unicode_pieces.get(piece.symbol(), ' ')} │" if piece else " │" board_lines.append(row) if rank > 0: board_lines.append(" ├───┼───┼───┼───┼───┼───┼───┼───┤") board_lines.append(" └───┴───┴───┴───┴───┴───┴───┴───┘\n") move_columns = [] num_moves = len(move_history) for start in range(0, num_moves, 32): column = [] for i in range(32): index = start + i if index >= num_moves: break move_number = index // 2 + 1 if index % 2 == 0: column.append(f"{move_number:>2}. {move_history[index]}") else: column[-1] += f" {move_history[index]}" move_columns.append(column) max_rows = max(len(board_lines), max(len(col) for col in move_columns)) clear_screen() print(f"Eval: {eval_display}") for row in range(max_rows): board_part = board_lines[row] if row < len(board_lines) else "" if row == 0: print(f"{board_part:<35}") else: move_row = row - 1 columns_part = " | ".join( move_columns[col][move_row] if move_row < len(move_columns[col]) else "" for col in range(len(move_columns)) ).strip(" |") print(f"{board_part:<35} | {columns_part}" if columns_part else board_part) print("\nCaptured pieces:") print(f"White: {' '.join(white_captured)} {'+{}'.format(white_score) if white_score > 0 else ''}") print(f"Black: {' '.join(black_captured)} {'+{}'.format(black_score) if black_score > 0 else ''}\n") print(f"Sıra: {'Beyaz' if board.turn == chess.WHITE else 'Siyah'}") print("white ELO:", white_elo_rating, "black ELO:", black_elo_rating) print("FEN:", board.fen()) def save_pgn(): if FEN: game = chess.pgn.Game() game.headers["Event"] = f"Stockfish Game (ELO Match) - Stockfish vs Stockfish (Depth {stockfish.depth})" game.headers["White"] = f"Stockfish (ELO {white_elo_rating})" game.headers["Black"] = f"Stockfish (ELO {black_elo_rating})" game.headers["Result"] = board.result() game.headers["Date"] = datetime.datetime.now().strftime("%Y.%m.%d") game.headers["Round"] = len(move_history) // 2 + 1 game.headers["Site"] = "Stockfish LOCAL (python)" game.headers["FEN"] = FEN board_position = chess.Board(FEN) node = game for move_uci in move_history: move = chess.Move.from_uci(move_uci) if move in board_position.legal_moves: node = node.add_variation(move) board_position.push(move) else: print(f"illegal move: {move_uci}") with open("game_history.pgn", "w") as pgn_file: print(game, file=pgn_file) print("Game history saved as PGN.") def save_game_history(): save_pgn() while not board.is_game_over(): elo_rating = white_elo_rating if board.turn == chess.WHITE else black_elo_rating stockfish.set_elo_rating(elo_rating) stockfish.set_skill_level(get_skill_level(elo_rating)) stockfish.set_fen_position(board.fen()) best_move = stockfish.get_best_move() if best_move is None: print("Oyun bitti!") break board.push_uci(best_move) move_history.append(best_move) print_board() stockfish.set_fen_position(board.fen()) if board.is_checkmate(): print(f"Oyun mat ile sona erdi! Kazanan: {('Siyah' if board.turn == chess.WHITE else 'Beyaz')}") elif board.is_stalemate(): print("Oyun pat ile sona erdi!") elif board.is_insufficient_material(): print("Yetersiz materyal nedeniyle oyun berabere!") elif board.is_seventyfive_moves(): print("75 hamle kuralı nedeniyle oyun berabere!") elif board.is_fivefold_repetition(): print("Beş kez aynı pozisyon tekrarı nedeniyle oyun berabere!") elif board.is_variant_draw(): print("Oyun varyant kuralına göre berabere!") else: print("Oyun diğer sebeplerle sona erdi!") save_game_history()