def register_move_if_needed(self): new_board = chessboard_detection.get_chessboard(self) potential_starts, potential_arrivals = get_potential_moves( self.previous_chessboard_image, new_board, self.we_play_white) valid_move_string1 = self.get_valid_move(potential_starts, potential_arrivals, new_board) #print("Valid move string 1:" + valid_move_string1) if len(valid_move_string1) > 0: time.sleep(0.1) 'Check that we were not in the middle of a move animation' new_board = chessboard_detection.get_chessboard(self) potential_starts, potential_arrivals = get_potential_moves( self.previous_chessboard_image, new_board, self.we_play_white) valid_move_string2 = self.get_valid_move(potential_starts, potential_arrivals, new_board) #print("Valid move string 2:" + valid_move_string2) if valid_move_string2 != valid_move_string1: return False, "The move has changed" valid_move_UCI = chess.Move.from_uci(valid_move_string1) valid_move_registered = self.register_move(valid_move_UCI, new_board) return True, valid_move_string1 return False, "No move found"
def build_fen(self, we_are_white, rochade='KQkq'): position_detection = chessboard_detection.get_chessboard( self, (1024, 1024)) self.previous_chessboard_image = chessboard_detection.get_chessboard( self) # cv2.imshow('dsd',position_detection) # cv2.waitKey(0) # board_basics.is_white_on_bottom(position_detection) self.we_play_white = we_are_white to_move = 'w' if we_are_white else 'b' self.moves_to_detect_before_use_engine = 0 # if v.get() else 1 pieces = sorted( os.listdir('/Users/sebastiankoch/OnlineChessBot/pieces')) vis_glob = np.array([]) piece_notation = [ 'b', 'k', 'n', 'p', 'q', 'r', '*', 'B', 'K', 'N', 'P', 'Q', 'R' ] fen_str = '' # rochade = 'KQkq' order = range(8) if we_are_white else reversed(range(8)) for i in order: vis = np.array([]) image_list = [ get_square_image(i, j, position_detection) for j in (range(8) if we_are_white else reversed(range(8))) ] answers = piece_on_square_list(image_list) for answer in answers: im = cv2.imread( os.path.join('/Users/sebastiankoch/OnlineChessBot/pieces', pieces[answer])) if vis.size == 0: vis = im fen_str += piece_notation[answer] else: if we_are_white: vis = np.concatenate((vis, im), axis=1) else: vis = np.concatenate((im, vis), axis=1) fen_str += piece_notation[answer] fen_str += '/' if vis_glob.size == 0: vis_glob = vis else: if we_are_white: vis_glob = np.concatenate((vis_glob, vis), axis=0) else: vis_glob = np.concatenate((vis, vis_glob), axis=0) fen_str = self.transform_fen(fen_str, to_move, rochade) # fen_str= fen_str.replace(" "," ") return fen_str, vis_glob
def register_move_if_needed(self): new_board = chessboard_detection.get_chessboard(self) old_board = self.previous_chessboard_image diff = cv2.absdiff(new_board, old_board) if diff.mean() == 0: return False, ([], []), (old_board, new_board) cv2.waitKey(50) new_board2 = chessboard_detection.get_chessboard(self) # cv2.waitKey(10) # new_board3 = chessboard_detection.get_chessboard(self) while cv2.absdiff(new_board, new_board2).mean( ) > 0: # or cv2.absdiff(new_board,new_board3).mean()>0: new_board = chessboard_detection.get_chessboard(self) cv2.waitKey(50) new_board2 = chessboard_detection.get_chessboard(self) # cv2.waitKey(10) # new_board3 = chessboard_detection.get_chessboard(self) potential_starts, potential_arrivals = get_potential_moves( self.previous_chessboard_image, new_board, self.we_play_white) if len(potential_starts) > 6 or len(potential_arrivals) > 6: self.previous_chessboard_image = new_board raise PositionChanged # pass valid_move_string1, rest = self.get_valid_move(potential_starts, potential_arrivals, new_board) if rest: # print('premove to process') print("Valid move string 1:" + valid_move_string1) if len(valid_move_string1) > 0: valid_move_UCI = chess.Move.from_uci(valid_move_string1) valid_move_registered = self.register_move( valid_move_UCI, new_board) new_board = chessboard_detection.get_chessboard(self) potential_starts = rest[0] potential_arrivals = rest[1] valid_move_string1, rest = self.get_valid_move( potential_starts, potential_arrivals, new_board) if len(valid_move_string1) > 0: valid_move_UCI = chess.Move.from_uci(valid_move_string1) valid_move_registered = self.register_move( valid_move_UCI, new_board) return True, valid_move_string1, (old_board, new_board) else: print("Valid move string 1:" + valid_move_string1) if len(valid_move_string1) > 0: valid_move_UCI = chess.Move.from_uci(valid_move_string1) valid_move_registered = self.register_move( valid_move_UCI, new_board) return True, valid_move_string1, (old_board, new_board) # if len(potential_arrivals)>0 or len(potential_starts)>0: return False, (potential_starts, potential_arrivals), (old_board, new_board)
def our_side(self): # TODO use pawns to get side position_detection = chessboard_detection.get_chessboard( self, (800, 800)) piece_notation = [ 'b', 'k', 'n', 'p', 'q', 'r', '*', 'B', 'K', 'N', 'P', 'Q', 'R' ] black_king_position = () white_king_position = () order = range(8) for i in order: vis = np.array([]) # order2 = range(8) image_list = [ get_square_image(i, j, position_detection) for j in range(8) ] answers = piece_on_square_list(image_list) if piece_notation.index('k') in answers: black_king_position = (i, 0) if piece_notation.index('K') in answers: white_king_position = (i, 0) if not white_king_position or not black_king_position: raise NoValidPosition if black_king_position[0] < white_king_position[0]: return 'white' elif black_king_position[0] > white_king_position[0]: return 'black' else: return 'unsure'
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))
def build_fen_guess_side(self): position_detection = chessboard_detection.get_chessboard( self, (1024, 1024)) position_detection2 = chessboard_detection.get_chessboard( self, (612, 612)) self.previous_chessboard_image = chessboard_detection.get_chessboard( self) self.moves_to_detect_before_use_engine = 0 # if v.get() else 1 fen_str = '' # position_detection = chessboard_detection.get_chessboard(self, (1024, 1024)) piece_notation = [ 'b', 'k', 'n', 'p', 'q', 'r', '*', 'B', 'K', 'N', 'P', 'Q', 'R' ] pieces = sorted(os.listdir('pieces')) vis_glob = np.array([]) rochade = '-' im_list = [] black_king_position = -1 white_king_position = -1 prediction_time = 0 for i in range(8): for j in range(8): # step_time = time.time() answer = piece_on_square( get_square_image(i, j, position_detection)) answer2 = piece_on_square( get_square_image(i, j, position_detection2)) if answer != answer2: position_detection3 = chessboard_detection.get_chessboard( self, (2048, 2048)) answer = piece_on_square( get_square_image(i, j, position_detection3)) # prediction_time += (time.time() - step_time) if piece_notation.index('k') == answer: black_king_position = i * 8 + j if piece_notation.index('K') == answer: white_king_position = i * 8 + j im = cv2.imread(os.path.join('pieces', pieces[answer])) im_list.append(im) fen_str += piece_notation[answer] fen_str += '/' print(f"-->Prediction: {prediction_time}") if white_king_position == -1 or black_king_position == -1: raise NoValidPosition if black_king_position < white_king_position: for im_row in list(chunks(im_list, 8)): vis = np.array([]) for img in im_row: if vis.size == 0: vis = img else: vis = np.concatenate((vis, img), axis=1) if vis_glob.size == 0: vis_glob = vis else: vis_glob = np.concatenate((vis_glob, vis), axis=0) self.we_play_white = True return True, self.transform_fen(fen_str, 'w', rochade), vis_glob else: for im_row in list(chunks((im_list), 8)): vis = np.array([]) for img in im_row: if vis.size == 0: vis = img else: vis = np.concatenate((vis, img), axis=1) if vis_glob.size == 0: vis_glob = vis else: vis_glob = np.concatenate((vis_glob, vis), axis=0) self.we_play_white = False return False, self.transform_fen(((fen_str[:-1])[::-1] + '/'), 'b', rochade), vis_glob
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))
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)
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)
import board_basics import chessboard_detection import ml_model import chess import copy if __name__ == '__main__': ml_model.init_class() game_state = Game_state() # game_state.board.set_fen('rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1') 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,resolution=(800,800)) # cv2.imshow('test',resized_chessboard) # cv2.waitKey(0) pieces = sorted(os.listdir('/Users/sebastiankoch/OnlineChessBot/pieces')) vis = np.array([]) vis_glob = np.array([]) piece_notation =['b','k','n','p','q','r','*','B','K','N','P','Q','R'] fen_str='' to_move ='w' rochade ='KQkq' en_passant='-' halfmoves='0' move='1' we_are_white = board_basics.is_white_on_bottom(resized_chessboard) to_move = 'w' if we_are_white else 'b'