def botMove(): global board_state clearMessage() t1 = time.time() dispMessage(tk.END, "Searching...") if engine.getGameCount() == 0: bot_move = Bot.generateFirstMove() elif engine.getGameCount() > 0: bot_move = Bot.generateMove(board_state) t2 = time.time() #dispMessage(tk.END, "Search time: " + str(t2-t1)) board_state, result, game_count = engine.updateBoardState(bot_move, plyr_id=2) with open('engine/save_file', 'wb') as file: # Save the game state after computer plays game_state = [board_state, player_colors, engine.getGameCount()] pickle.dump(game_state, file) updateTerminal(prev_move=bot_move, player_to_move='USER', game_status=result, search_time=t2 - t1) begin_search_btn.grid_forget() enter_btn.grid(row=16, column=25, sticky='w')
def updateTerminal(prev_move, player_to_move, game_status, search_time=None): clearMessage() renderBoard() if game_status[0] is None: game_status = 'Game in progress' elif game_status[0] == 'WC1-win' or game_status[0] == 'WC2-win': if game_status[1] == 'p1': game_status = "USER WINS!" dispMessage(1, "Game status: " + str(game_status)) return elif game_status[1] == 'p2': game_status = "COMPUTER WINS!" dispMessage(1, "Game status: " + str(game_status)) return last_move_by = '' if player_to_move == 'USER': last_move_by = 'COMPUTER' elif player_to_move == 'COMPUTER': last_move_by = 'USER' game_count_label.config(text=str(engine.getGameCount())) first_move = False if engine.getGameCount() == 0: first_move = True allowed_moves = engine.getAvailableMoves(board_state, first_move) if use_alt_coord: if prev_move: prev_move = def_to_alt_mapping[prev_move] prev_move = prev_move[0] + str(prev_move[1]) allowed_moves_alt = [] for am in allowed_moves: am_alt = def_to_alt_mapping[am] am_alt = am_alt[0] + str(am_alt[1]) allowed_moves_alt.append(am_alt) allowed_moves = allowed_moves_alt dispMessage(1, "GAME STATUS: " + str(game_status)) dispMessage(2, '') dispMessage(3, 'PLAYER-1 (USER): ' + player_colors[0]) dispMessage(4, '') dispMessage(5, 'PLAYER-2 (COMPUTER): ' + player_colors[1]) dispMessage(6, '') dispMessage(7, '-------------------------------') dispMessage(8, '') if prev_move: dispMessage(9, last_move_by + '\'s move: ' + str(prev_move)) if search_time: dispMessage(10, "Search time: " + str(search_time)) dispMessage(11, '') dispMessage(12, '') dispMessage(13, ">>> " + str(player_to_move) + "'s turn --") dispMessage(14, '') dispMessage(15, "Allowed moves: " + str(allowed_moves))
def userMove(): global board_state clearMessage() allowed_moves = engine.getAvailableMoves(board_state) user_move = user_ip.get() if not use_alt_coord: user_move = user_move.split(',') user_move = tuple([int(i) for i in user_move]) if engine.getGameCount() > 0 and user_move not in allowed_moves: dispMessage(tk.END, "Invalid move! Try again") dispMessage(tk.END, "Available moves: " + str(allowed_moves)) return else: user_move_a = user_move[0].upper() user_move_1 = int(user_move[1:]) user_move = (user_move_a, user_move_1) user_move = alt_to_def_mapping[user_move] allowed_moves_alt = [] for am in allowed_moves: am_alt = def_to_alt_mapping[am] am_alt = am_alt[0] + str(am_alt[1]) allowed_moves_alt.append(am_alt) if engine.getGameCount() > 0 and user_move not in allowed_moves: dispMessage(tk.END, "Invalid move! Try again") dispMessage(tk.END, "Available moves: " + str(allowed_moves_alt)) return board_state, result, game_count = engine.updateBoardState(user_move, plyr_id=1) with open('engine/save_file', 'wb') as file: # Save the game state after user plays game_state = [board_state, player_colors, engine.getGameCount()] pickle.dump(game_state, file) updateTerminal(prev_move=user_move, player_to_move='COMPUTER', game_status=result) #allowed_moves = engine.getAvailableMoves(board_state, first_move='False') begin_search_btn.grid(row=19, column=24, sticky='w') enter_btn.grid_forget()
def __init__(self, bot='random', depth=2, ID_max_depth=None, ID_max_time=None, eval_fn='Fn1', bot_id=2): self.bot_id = bot_id # BOT is by default the Player-2 self.board_state = engine.getInitialBoardState() self.prev_board_state = None self.player_colors = engine.getPlayerColors( ) # player_colors[0] => user_color self.bot = bot self.depth = depth self.eval_fn = eval_fn self.ID_max_depth = ID_max_depth self.ID_max_time = ID_max_time self.game_count = engine.getGameCount()
dispMessage(9, last_move_by + '\'s move: ' + str(prev_move)) if search_time: dispMessage(10, "Search time: " + str(search_time)) dispMessage(11, '') dispMessage(12, '') dispMessage(13, ">>> " + str(player_to_move) + "'s turn --") dispMessage(14, '') dispMessage(15, "Allowed moves: " + str(allowed_moves)) #------------------------------------------------------------------------------- # Game Count game_count_label = tk.Label(game_window, text='GAME STEP -') game_count_label.grid(row=0, column=10, pady=10) game_count_label = tk.Label(game_window, text=str(engine.getGameCount())) game_count_label.grid(row=0, column=11, sticky='w') #------------------------------------------------------------------------------- # Display box disp_box_label = tk.Label(game_window, text='MESSAGE') disp_box_label.grid(row=2, column=22, sticky='w') disp_box = tk.Listbox(game_window, height=20, width=50) disp_box.grid(row=3, column=22, rowspan=10, columnspan=10) scroll_bar = tk.Scrollbar(game_window) scroll_bar.grid(row=3, column=33, rowspan=10, padx=10, pady=10) disp_box.configure(xscrollcommand=scroll_bar.set) scroll_bar.configure(command=disp_box.xview)