niklasf / python-chess

A chess library for Python, with move generation and validation, PGN parsing and writing, Polyglot opening book reading, Gaviota tablebase probing, Syzygy tablebase probing, and UCI/XBoard engine communication
https://python-chess.readthedocs.io/en/latest/
GNU General Public License v3.0
2.4k stars 526 forks source link

board.mirror() #1054

Closed lucasssoh closed 9 months ago

lucasssoh commented 10 months ago

mirror() seems like to reverse the board , but cols names don't change, only the rows so writing the correct move as black make it a little bit wonky:

import chess
import chess.engine

engine = chess.engine.SimpleEngine.popen_uci(r"c:\Users\lucas.ram\code\codelearn\Stockfish_Versions\stockfish16.exe")

def get_user_input(prompt, valid_inputs):
    while True:
        user_input = input(prompt).upper()
        if user_input in valid_inputs:
            return user_input
        print("Invalid input. Please try again.")

def play_game():
    board = chess.Board()

    difficulty = get_user_input("Select a difficulty of the game : "+"\n\n"+
                                "    - Noob        (N)"+"\n"+
                                "    - Easy        (E)"+"\n"+
                                "    - Medium      (M)"+"\n"+
                                "    - GrandMaster (G)"+"\n"+
                                "    - Bot         (B)"+"\n\n"+
                                "=> ", ["N", "E", "M", "G", "B"])

    skill_level = {"N": 3, "E": 7, "M": 12, "G": 15, "B": 20}[difficulty]
    engine.configure({"Skill Level": skill_level})

    user_color = get_user_input("Choose the color you want to play with :"+"\n\n"+ 
                                "   - White (W)"+"\n"+
                                "   - Black (B)"+"\n\n"+
                                "=> ", ["W", "B"])

    engine_color = "W" if user_color == "B" else "B" 

    while not board.is_game_over():    
        evaluation = engine.analyse(board, chess.engine.Limit(time=1.0))
        current_score = evaluation['score'].relative.score() / 100

        print("---------------")
        if user_color == "B":
            print(str(board.mirror()))
        else:
            print(board)
        print("---------------")

        print(current_score)

        if (user_color == "W" and board.turn) or (user_color == "B" and not board.turn):
            move = input("\n"+"Enter a move : ")
            if move.lower() == "resign":
                print("Engine won by resignation.")
                break 
            try:
                if board.parse_san(move) in board.legal_moves:
                    board.push_san(move)
                else:
                    print("Illegal move, try again")
            except:
                print("Invalid input")
            print("\n")

        else:
            result = engine.play(board, chess.engine.Limit(time=3.0))

            if (engine_color == "B" and current_score > 4) or \
               (engine_color == "W" and current_score < -4):
                print("You won by resignation.")
                break 

            move = result.move
            board.push(move)

            if board.is_game_over():
                break

            print("\n")

    engine.quit()

play_game()
niklasf commented 10 months ago

chess.Board() really represents an abstract/mathematical chess position, not a physical display of a board. board.mirror() mirrors the position.

Maybe what you need is board.unicode(orientation=chess.BLACK) which has options to alter the display?