コード例 #1
0
ファイル: main.py プロジェクト: zeeneddie/chessbot_python
def start_playing():
    game_state = Game_state()
    add_log("Looking for a chessboard...")

    found_chessboard, position = chessboard_detection.find_chessboard()

    if found_chessboard:
        add_log("Found the chessboard " + position.print_custom())
        game_state.board_position_on_screen = position
    else:
        add_log("Could not find the chessboard")
        add_log("Please try again when the board is open on the screen\n")
        return

    button_start = tk.Button(text="Stop playing", command=stop_playing)
    button_start.grid(column=0, row=1)

    add_log("Checking if we are black or white...")
    resized_chessboard = chessboard_detection.get_chessboard(game_state)
    #cv2.imshow('Resized image',resized_chessboard)
    game_state.previous_chessboard_image = resized_chessboard

    we_are_white = board_basics.is_white_on_bottom(resized_chessboard)
    game_state.we_play_white = we_are_white
    if we_are_white:
        add_log("We are white")
        game_state.moves_to_detect_before_use_engine = 0
    else:
        add_log("We are black")
        game_state.moves_to_detect_before_use_engine = 1
        first_move_registered = False
        while first_move_registered == False:
            first_move_string = askstring(
                'First move', 'What was the first move played by white?')
            if len(first_move_string) > 0:
                first_move = chess.Move.from_uci(first_move_string)
                first_move_registered = game_state.register_move(
                    first_move, resized_chessboard)

        add_log("First move played by white :" + first_move_string)

    while True:
        window.update()
        #cv2.imshow('Resized image',game_state.previous_chessboard_image)
        #add_log("Moves to detect before use engine" + str(game_state.moves_to_detect_before_use_engine))
        if game_state.moves_to_detect_before_use_engine == 0:
            #add_log("Our turn to play:")
            game_state.play_next_move()
            #add_log("We are done playing")

        found_move, move = game_state.register_move_if_needed()
        if found_move:
            clear_logs()
            add_log("The board :\n" + str(game_state.board) + "\n")
            add_log("\nAll moves :\n" + str(game_state.executed_moves))
コード例 #2
0
def start_playing():
    global function_parser
    global running
    global slider_str
    global slider_var
    running = True
    strength = slider_str.get() + 1
    variance = slider_var.get() + 1
    slider_str.config(state=tk.DISABLED)
    slider_var.config(state=tk.DISABLED)
    game_state = Game_state()
    add_log(logs_text, "Looking for a chessboard...")

    found_chessboard, position = chessboard_detection.find_chessboard()

    if found_chessboard:
        add_log(logs_text, "Found the chessboard " + position.print_custom())
        game_state.board_position_on_screen = position
    else:
        add_log(logs_text, "Could not find the chessboard")
        add_log(logs_text,
                "Please try again when the board is open on the screen\n")
        return

    button_start = ttk.Button(tab1, text="Stop playing", command=stop_playing)
    button_start.grid(column=0, row=1, pady=10, columnspan=2)

    # add_log(logs_text,"Checking if we are black or white...")
    resized_chessboard = chessboard_detection.get_chessboard(game_state)
    game_state.previous_chessboard_image = resized_chessboard

    we_are_white = v.get()
    game_state.we_play_white = we_are_white
    fen_str, detected_board = game_state.build_fen(we_are_white)
    try:
        game_state.board.set_fen(fen_str)
    except Exception as e:
        print(e)
        stop_playing()
    # print(game_state.get_castling_rights())

    while running:

        window.update()

        if game_state.moves_to_detect_before_use_engine == 0:
            #add_log("Our turn to play:")

            score, move_time = game_state.play_next_move(
                position.factor, strength, variance)

            position_eval = ttk.Label(
                tab1,
                text=f"Score: {score} \t move_time: {move_time}",
                anchor="e",
                wraplength=300)
            position_eval.grid(column=0, row=9, columnspan=2)
            #add_log("We are done playing")
        found_move = False
        try:

            found_move, move, img_boards = game_state.register_move_if_needed()

            if found_move:
                v.set(not v.get())
                diff = abs(img_boards[0] - img_boards[1])
                numpy_horizontal = np.vstack(
                    (img_boards[0], img_boards[1], diff))
                image = cv2.resize(numpy_horizontal, (200, 600))
                img = ImageTk.PhotoImage(Image.fromarray(np.uint8(image)))
                imglabel = tk.Label(tab1, image=img).grid(row=2,
                                                          column=2,
                                                          rowspan=100)

            # else:
            #     if (len(move[0])>0 or len(move[1])>0) and move[0].all()!=move[1][]:
            #         new_fen, detected_board = game_state.build_fen(game_state.we_play_white,game_state.get_castling_rights())
            #         try:
            #             game_state.board.set_fen(new_fen)
            #             resized_chessboard = chessboard_detection.get_chessboard(game_state)
            #             game_state.previous_chessboard_image = resized_chessboard
            #
            #         except Exception as e:
            #             print(e)
            #             stop_playing()
        except Exception as e:
            print(e)
            stop_playing()

        if function_parser:
            move = function_parser
            function_parser = ""
            new_board = chessboard_detection.get_chessboard(game_state)
            valid_move_UCI = chess.Move.from_uci(move)
            valid_move_registered = game_state.register_move(
                valid_move_UCI, new_board)

        if found_move:
            clear_logs(logs_text)
            add_log(logs_text, "The board :\n" + str(game_state.board) + "\n")
            add_log(logs_text,
                    "\nAll moves :\n" + str(game_state.executed_moves))
コード例 #3
0
def puzzel_rush():
    global function_parser
    global running
    global slider_str2
    global slider_var2
    running = True
    strength2 = slider_str2.get() + 1
    variance2 = slider_var2.get() + 1
    slider_str2.config(state=tk.DISABLED)
    slider_var2.config(state=tk.DISABLED)
    game_state = Game_state()

    found_chessboard, position = chessboard_detection.find_chessboard()

    if found_chessboard:
        game_state.board_position_on_screen = position
    else:
        print('no board on screen')
        return

    button_start2 = ttk.Button(tab2, text="Stop playing", command=stop_playing)
    button_start2.grid(column=0, row=0, pady=10)

    resized_chessboard = chessboard_detection.get_chessboard(game_state)
    game_state.previous_chessboard_image = resized_chessboard
    # we_are_white=True
    # try:
    #     side = game_state.our_side()
    #     if side == 'white':
    #         we_are_white = True
    #     elif side == 'black':
    #         we_are_white = False
    #     else:
    #         window.attributes('-topmost', 0)
    #         user_side = askstring('Unclear Color', 'What is our color? [black/white]')
    #         we_are_white = True if user_side =='white' else False
    #         window.attributes('-topmost', 1)
    # except NoValidPosition:
    #     print('cant find kings')
    #     stop_playing()
    # v2.set(we_are_white)
    we_are_white, fen_str, detected_board = game_state.build_fen_guess_side()
    compare = cv2.resize(resized_chessboard, (200, 200))
    detected_board = cv2.resize(detected_board, (200, 200))[..., 0]
    numpy_horizontal = np.vstack((compare, detected_board))

    detected_board = ImageTk.PhotoImage(
        Image.fromarray(np.uint8(numpy_horizontal)))

    imglabel = tk.Label(tab2, image=detected_board).grid(row=7,
                                                         column=0,
                                                         columnspan=2)

    try:
        game_state.board.set_fen(fen_str)
    except:
        stop_playing()

    while running:
        window.update()

        if game_state.moves_to_detect_before_use_engine == 0:
            game_state.play_next_move(position.factor, strength2, variance2)

        found_move = False
        move = "no move"
        img_boards = (game_state.previous_chessboard_image,
                      game_state.previous_chessboard_image)
        try:
            found_move, move, img_boards = game_state.register_move_if_needed()
        except PositionChanged:
            print('postionchanged')
            time.sleep(2)
            # try:
            #     side = game_state.our_side()
            #     if side == 'white':
            #         we_are_white = True
            #     elif side == 'black':
            #         we_are_white = False
            #     else:
            #         window.attributes('-topmost', 0)
            #         user_side = askstring('Unclear Color', 'What is our color? [black/white]')
            #         we_are_white = True if user_side == 'white' else False
            #         window.attributes('-topmost',1)
            # except:
            #     print('cant find kings')
            #     stop_playing()
            # v2.set(we_are_white)
            we_are_white, fen_str, detected_board = game_state.build_fen_guess_side(
            )
            try:
                game_state.board.set_fen(fen_str)
            except:
                stop_playing()
            curr_board = chessboard_detection.get_chessboard(
                game_state, (200, 200))

            detected_board = cv2.resize(detected_board, (200, 200))[..., 0]
            numpy_horizontal = np.vstack((curr_board, detected_board))

            detected_board = ImageTk.PhotoImage(
                Image.fromarray(np.uint8(numpy_horizontal)))

            imglabel = tk.Label(tab2, image=detected_board).grid(row=7,
                                                                 column=0,
                                                                 columnspan=2)

            continue

        if found_move:
            imglabel = tk.Label(tab2, image=detected_board).grid(row=7,
                                                                 column=0,
                                                                 columnspan=2)

        if function_parser:
            move = function_parser
            function_parser = ""
            new_board = chessboard_detection.get_chessboard(game_state)
            valid_move_UCI = chess.Move.from_uci(move)
            valid_move_registered = game_state.register_move(
                valid_move_UCI, new_board)
コード例 #4
0
 def __init__(self):
     time.sleep(5)
     self.position, self.we_play_white = chessboard_detection.find_chessboard(
     )
     self.is_our_turn = self.we_play_white
コード例 #5
0
import numpy as np
import cv2
import pyautogui
from mss import mss
import os
import glob
from game_state_classes import Game_state
import board_basics
import chessboard_detection

if __name__ == '__main__':
    game_state = Game_state()
    found_chessboard, position = chessboard_detection.find_chessboard()
    print(position)
    print(found_chessboard)
    game_state.board_position_on_screen = position
    resized_chessboard = chessboard_detection.get_chessboard(game_state)

    for i in range(8):
        for j in range(8):
            image = board_basics.get_square_image(i, j, resized_chessboard)
            cv2.imwrite(f"pieces/{i}{j}.png", image)