def start_play_with_UI(self, AI: mcts_alphaZero.MCTSPlayer, start_player=0): """ a GUI for playing """ AI.reset_player() self.board.init_board() current_player = SP = start_player UI = GUI(self.board.width) end = False while True: print("current_player", current_player) if current_player == 0: UI.show_messages("Your turn") else: UI.show_messages("AI's turn") if current_player == 1 and not end: move, move_probs = AI.get_action(self.board, is_selfplay=False, print_probs_value=1) else: inp = UI.get_input() if inp[0] == "move" and not end: if type(inp[1]) != int: move = UI.loc_2_move(inp[1]) else: move = inp[1] elif inp[0] == "RestartGame": end = False current_player = SP self.board.init_board() UI.restart_game() AI.reset_player() continue elif inp[0] == "ResetScore": UI.reset_score() continue elif inp[0] == "quit": exit() continue elif inp[0] == "SwitchPlayer": end = False self.board.init_board() UI.restart_game(False) UI.reset_score() AI.reset_player() SP = (SP + 1) % 2 current_player = SP continue else: # print('ignored inp:', inp) continue # print('player %r move : %r'%(current_player,[move//self.board.width,move%self.board.width])) if not end: # print(move, type(move), current_player) UI.render_step(move, self.board.current_player) self.board.do_move(move) # print('move', move) # print(2, self.board.get_current_player()) current_player = (current_player + 1) % 2 # UI.render_step(move, current_player) end, winner = self.board.game_end() if end: if winner != -1: print("Game end. Winner is player", winner) UI.add_score(winner) else: print("Game end. Tie") print(UI.score) print()
def start_play_with_UI(self, AI=None, AI2=None, start_player=1): ''' a GUI for playing ''' if AI: AI.reset_player() self.board.init_board() current_player = SP = start_player UI = GUI(self.board.width) end = False while True: print('current_player', current_player) if current_player == 0: UI.show_messages('white turn') else: UI.show_messages('black turn') if AI and current_player == 1 and not end: move = AI.get_action(self.board) else: if AI2 and not end: move = AI2.get_action(self.board) else: inp = UI.get_input() if not AI2 and inp[0] == 'move' and not end: if type(inp[1]) != int: move = UI.loc_2_move(inp[1]) else: move = inp[1] elif inp[0] == 'RestartGame': end = False current_player = SP self.board.init_board() UI.restart_game() if AI: AI.reset_player() if AI2: AI2.reset_player() continue elif inp[0] == 'ResetScore': UI.reset_score() continue elif inp[0] == 'quit': exit() continue elif inp[0] == 'SwitchPlayer': end = False self.board.init_board() UI.restart_game(False) UI.reset_score() if AI: AI.reset_player() if AI2: AI2.reset_player() SP = (SP + 1) % 2 current_player = SP continue else: # print('ignored inp:', inp) continue # print('player %r move : %r'%(current_player,[move//self.board.width,move%self.board.width])) if not end: # print(move, type(move), current_player) if move in self.board.availables: UI.render_step(move, self.board.current_player) self.board.do_move(move) print('move', move % self.board.width, move // self.board.width, '\n') # print(2, self.board.get_current_player()) current_player = (current_player + 1) % 2 # UI.render_step(move, current_player) end, winner = self.board.game_end() if end: if winner != -1: print("Game end. Winner is player", winner) UI.add_score(winner) else: print("Game end. Tie") print(UI.score) print() else: print('forbid hand!')
def start_play_with_UI(self, AI, start_player=0): ''' a GUI for playing ''' AI.reset_player() self.board.init_board() current_player = SP = start_player UI = GUI(self.board.width) end = False while True: print('current_player', current_player) if current_player == 0: UI.show_messages('Your turn') else: UI.show_messages('AI\'s turn') if current_player == 1 and not end: move, move_probs = AI.get_action(self.board, is_selfplay=False, show_probs_value=True) else: # move, move_probs = AI.get_action(self.board, is_selfplay=False, show_probs_value=1) inp = UI.get_input() if inp[0] == 'move' and not end: if type(inp[1]) != int: move = UI.loc_2_move(inp[1]) else: move = inp[1] elif inp[0] == 'RestartGame': end = False current_player = SP self.board.init_board() UI.restart_game() AI.reset_player() continue elif inp[0] == 'ResetScore': UI.reset_score() continue elif inp[0] == 'quit': exit() continue elif inp[0] == 'SwitchPlayer': end = False self.board.init_board() UI.restart_game(False) UI.reset_score() AI.reset_player() SP = (SP + 1) % 2 current_player = SP continue else: # print('ignored inp:', inp) continue # print('player %r move : %r'%(current_player,[move//self.board.width,move%self.board.width])) if not end: # print(move, type(move), current_player) UI.render_step(move, self.board.current_player) self.board.do_move(move) # print('move', move) # print(2, self.board.get_current_player()) current_player = (current_player + 1) % 2 # UI.render_step(move, current_player) end, winner = self.board.game_end() if end: if winner != -1: # print("Game end. Winner is player", winner) UI.add_score(winner) # else: # print("Game end. Tie") print(UI.score) print()
def start_play_with_UI(start_player=0): # run a gomoku game with AI in GUI bcast_move = -1 # init game and player board.init_board() player2.reset_player() current_player_num = start_player restart = 0 end = False if rank == 0: SP = start_player UI = GUI(board.width) while True: if rank == 0: if current_player_num == 0: UI.show_messages("Your turn") else: UI.show_messages("AI's turn") # AI's turn if current_player_num == 1 and not end: # reset the search tree player2.reset_player() if rank == 0: # print prior probabilities gather_move, move_probs = player2.get_action( board=board, is_selfplay=False, print_probs_value=True) else: gather_move, move_probs = player2.get_action( board=board, is_selfplay=False, print_probs_value=False) gather_move_list = comm.gather(gather_move, root=0) # print('list is', gather_move_list) if rank == 0: # gather ecah rank's move and get the most selected one print("list is", gather_move_list) bcast_move = Counter(gather_move_list).most_common()[0][0] # print(board.move_to_location(bcast_move)) # human's turn else: if rank == 0: inp = UI.get_input() if inp[0] == "move" and not end: if type(inp[1]) != int: bcast_move = UI.loc_2_move(inp[1]) else: bcast_move = inp[1] elif inp[0] == "RestartGame": UI.restart_game() restart = SP + 1 elif inp[0] == "ResetScore": UI.reset_score() continue elif inp[0] == "quit": restart = "exit" elif inp[0] == "SwitchPlayer": SP = (SP + 1) % 2 UI.restart_game(False) UI.reset_score() restart = SP + 1 else: # print('ignored inp:', inp) continue restart = comm.bcast(restart, root=0) if not end and not restart: # bcast the move to other ranks bcast_move = comm.bcast(bcast_move, root=0) # print('!'*10,rank,bcast_move) if rank == 0: print(board.move_to_location(bcast_move)) UI.render_step(bcast_move, board.current_player) # human do move board.do_move(bcast_move) # print('rank:', rank, board.availables) current_player_num = (current_player_num + 1) % 2 end, winner = board.game_end() # check if game end if end: if rank == 0: if winner != -1: print("Game end. Winner is ", winner) UI.add_score(winner) else: print("Game end. Tie") else: if restart: if restart == "exit": exit() board.init_board() player2.reset_player() current_player_num = restart - 1 restart = 0 end = False
def start_play_with_UI(self, AI, start_player=0): ''' a GUI for playing ''' AI.reset_player() self.board.init_board() current_player = SP = start_player+1 UI = GUI(self.board.width) UI.SP = SP end = False while True: if SP == 1: UI._draw_text("Human(black)", (765, 150), text_height=UI.TestSize) UI._draw_text("AlphaZero(white)", (925, 150), text_height=UI.TestSize) else: UI._draw_text("AlphaZero(black)", (775, 150), text_height=UI.TestSize) UI._draw_text("Human(white)", (925, 150), text_height=UI.TestSize) print('current_player', current_player) if current_player == 1: UI.show_messages('Your turn') else: UI.show_messages('AI\'s turn') for move_availables in self.board.availables: i, j = self.board.move_to_location(move_availables) ban = self.forbidden(i, j) if (ban == 1 or ban == 3 or ban == 4) and not end: print("ban at ",move_availables) UI._draw_ban((i, j)) if current_player == 2 and not end: move, move_probs = AI.get_action(self.board, is_selfplay=False, print_probs_value=1) if current_player == SP: availables = [i for i in self.board.availables] i, j = self.board.move_to_location(move) ban = self.forbidden(i, j) while ban == 1 or ban == 3 or ban == 4: print("禁手!!", i, j) self.board.availables.remove(move) move, move_probs = AI.get_action(self.board, is_selfplay=False, print_probs_value=1) i, j = self.board.move_to_location(move) ban = self.forbidden(i, j) self.board.availables = [i for i in availables] else: inp = UI.get_input() if inp[0] == 'move' and not end: move = inp[1] if current_player == SP: ban = self.forbidden(inp[2],inp[3]) if ban == 1 or ban == 3 or ban == 4: continue elif inp[0] == 'RestartGame': end = False current_player = SP self.board.init_board() UI.restart_game() AI.reset_player() continue elif inp[0] == 'ResetScore': UI.reset_score() continue elif inp[0] == 'quit': exit() continue elif inp[0] == 'SwitchPlayer': end = False self.board.init_board() UI.restart_game(False) UI.reset_score() AI.reset_player() SP = self.board.players[0] if SP == self.board.players[1] else self.board.players[1] current_player = SP UI.SP=SP continue else: # print('ignored inp:', inp) continue # print('player %r move : %r'%(current_player,[move//self.board.width,move%self.board.width])) if not end: # print(move, type(move), current_player) UI.render_step(move, self.board.current_player) self.board.do_move(move) UI.show_messages('AI\'s turn') # print('move', move) # print(2, self.board.get_current_player()) current_player = self.board.players[0] if current_player == self.board.players[1] else self.board.players[1] # UI.render_step(move, current_player) end, winner = self.board.game_end() if end: if winner != -1: print("Game end. Winner is player", winner) UI.add_score(winner) """ if winner == 1: #UI.show_messages("Game end. Winner is 1 ") UI._draw_text("Game end. Winner is player 1 ", (500, 690), text_height=UI.TestSize) elif winner == 2: #UI.show_messages("Game end. Winner is 2 ") UI._draw_text("Game end. Winner is player 2 ", (500, 690), text_height=UI.TestSize) """ else: print("Game end. Tie") #UI._draw_text("Game end. Tie ", (775, 750), text_height=UI.TestSize) UI._show_endmsg(winner) #結束提示 print(UI.score) print()