def play_round(self): logger.info("Starting game round...") if np.random.uniform(0, 1) <= 0.5: white = self.current black = self.best w = "current" b = "best" else: white = self.best black = self.current w = "best" b = "current" current_board = cboard() checkmate = False dataset = [] value = 0 t = 0.1 while checkmate == False and current_board.actions() != []: dataset.append(copy.deepcopy(ed.encode_board(current_board))) print("") print(current_board.current_board) if current_board.player == 0: root = UCT_search(current_board, 777, white, t) policy = get_policy(root, t) print("Policy: ", policy, "white = %s" % (str(w))) elif current_board.player == 1: root = UCT_search(current_board, 777, black, t) policy = get_policy(root, t) print("Policy: ", policy, "black = %s" % (str(b))) current_board = do_decode_n_move_pieces(current_board,\ np.random.choice(np.array([0,1,2,3,4,5,6,7,8]), \ p = policy)) # decode move and move piece(s) if current_board.check_winner() == True: # someone wins if current_board.player == 0: # black wins value = -1 elif current_board.player == 1: # white wins value = 1 checkmate = True dataset.append(ed.encode_board(current_board)) if value == -1: dataset.append(f"{b} as black wins") return b, dataset elif value == 1: dataset.append(f"{w} as white wins") return w, dataset else: dataset.append("Nobody wins") return None, dataset
def play_ia(game, options): made_moves, convertedMatrix = convert_matrix_to_alpha_zero(game) print(convertedMatrix) ######################################################################### # AlphaZero ######################################################################### #best_net="c4_current_net_trained2_iter7.pth.tar" best_net="c4_current_net_trained_iter8.pth.tar" best_net_filename = os.path.join("C:\\Users\\Francesco\\Documents\\2_Schule\\HYU\\HYU-software-engineering-AI\\src_backend\\src\\ai_modules\\AlphaZero_Connect4\\src\\model_data\\", best_net) best_cnet = ConnectNet() cuda = torch.cuda.is_available() if cuda: best_cnet.cuda() best_cnet.eval() checkpoint = torch.load(best_net_filename) best_cnet.load_state_dict(checkpoint['state_dict']) net = best_cnet white = None black = net current_board = cboard() current_board.current_board = np.array(convertedMatrix) checkmate = False dataset = [] value = 0; t = 0.1; moves_count = made_moves moves_count += 1 dataset.append(copy.deepcopy(ed.encode_board(current_board))) print("AI is thinking.............") root = UCT_search(current_board,777,black,t) policy = get_policy(root, t) current_board = do_decode_n_move_pieces(current_board, np.random.choice(np.array([0,1,2,3,4,5,6]), p = policy)) # decode move and move piece(s) print(current_board.current_board); print(" ") return find_which_move_ai_made(convertedMatrix, current_board.current_board)
def play_game(net): # Asks human what he/she wanna play as white = None black = None while (True): play_as = input("What do you wanna play as? (\"O\"/\"X\")? Note: \"O\" starts first, \"X\" starts second\n") if play_as == "O": black = net; break elif play_as == "X": white = net; break else: print("I didn't get that.") current_board = cboard() checkmate = False dataset = [] value = 0 t = 0.1 moves_count = 0 while not checkmate and current_board.actions() != []: t = 1 if moves_count <= 5 else 0.1 moves_count += 1 dataset.append(copy.deepcopy(ed.encode_board(current_board))) print(current_board.current_board) print(" ") if current_board.player == 0 and white != None: print("AI is thinking........") root = UCT_search(current_board,777,white,t) policy = get_policy(root, t) elif ( current_board.player == 0 or current_board.player == 1 and black is None ): while(True): col = input("Which column do you wanna drop your piece? (Enter 1-7)\n") if int(col) in [1,2,3,4,5,6,7]: policy = np.zeros([7], dtype=np.float32); policy[int(col)-1] += 1 break elif current_board.player == 1: print("AI is thinking.............") root = UCT_search(current_board,777,black,t) policy = get_policy(root, t) current_board = do_decode_n_move_pieces(current_board,\ np.random.choice(np.array([0,1,2,3,4,5,6]), \ p = policy)) # decode move and move piece(s) if current_board.check_winner() == True: # someone wins if current_board.player == 0: # black wins value = -1 elif current_board.player == 1: # white wins value = 1 checkmate = True dataset.append(ed.encode_board(current_board)) print(current_board.current_board) print(" ") if value == -1: if play_as == "O": dataset.append('AI as black wins') print("YOU LOSE!!!!!!!") else: dataset.append('Human as black wins') print("YOU WIN!!!!!!!") return "black", dataset elif value == 1: if play_as == "O": dataset.append('Human as white wins') print("YOU WIN!!!!!!!!!!!") else: dataset.append('AI as white wins') print("YOU LOSE!!!!!!!") return "white", dataset else: dataset.append("Nobody wins") print("DRAW!!!!!") return None, dataset