def play_wo_human(agent1, agent2, first_player): ''' Default: agent 1 plays RED agent 2 plays YELLOW ''' turn_dict = {1: RED, 2: YELLOW} agent_turn_dict = {RED: agent1.name, YELLOW: agent2.name} times = {agent1.name: 0, agent2.name: 0} count_moves = 0 winner = None g = Game() # #randomize turn who starts first # turn_ind = random.randint(1,2) # turn = turn_dict[turn_ind] turn = turn_dict[first_player] print "Starting with turn = ", agent_turn_dict[turn] while True: g.printBoard() if turn == RED: print agent1.name, "'s turn" start = time.time() move = agent1.play_move() end = time.time() times[agent1.name] += (end - start) if move is None: print "DRAW GAME" break w = g.insert(move, turn) if w: winner = agent_turn_dict[w] print "####################WINNER: ", winner, " TOTAL MOVES: ", count_moves break agent2.play_opponent_move(move) else: print agent2.name, "'s turn" start = time.time() move = agent2.play_move() end = time.time() times[agent2.name] += (end - start) if move is None: print "DRAW GAME" break w = g.insert(move, turn) if w: winner = agent_turn_dict[w] print "#################WINNER: ", winner, " TOTAL MOVES: ", count_moves break agent1.play_opponent_move(move) count_moves += 1 turn = YELLOW if turn == RED else RED return winner, count_moves, times
def play_w_human(): g = Game() # agent = MinimaxAgent() #MCTSAgent() agent = QLearningAgent('q_values') turn = RED while True: g.printBoard() if turn == RED: row = input( '{}\'s turn: '.format('Red' if turn == RED else 'Yellow')) w = g.insert(int(row), turn) agent.play_opponent_move(int(row)) else: move = agent.play_move() w = g.insert(move, turn) if w: print "WINNER: ", w break turn = YELLOW if turn == RED else RED
def main(): net = Net() net.load_state_dict(torch.load(PATH)) g = Game() red = True moves = 0 while not g.won(): board = g.to_tensor(red) prediction = request_move(net, board, chance=1 if red else 0.0) max_value = torch.max(prediction) move = np.zeros((1, 7)) for i, element in enumerate(prediction): if element == max_value: move[:, i] = 1 prediction[i] = 0. column = move.tolist()[0].index(1.) for iter in range(7): try: g.insert(column, RED if red else YELLOW) moves += 1 break except Exception as e: column = (column + 1) % 7 else: return False, moves if g.won(): g.print_board() return red, moves # switch turn if red: red = False else: red = True
def play(games, win, ai): player1_wins = 0 player2_wins = 0 ties = 0 last_winner = -1 num_games = 1 while player1_wins < win and player2_wins < win and num_games <= games: g = Game(7, 6, 4) turns_played = 0 if last_winner == -1: turn = random.randint(1, 2) else: turn = last_winner # winner gets to go first while g.get_winner() is None and turns_played < g.rows * g.cols - 1: if turn == YELLOW: next_move = minimax_decision(g, YELLOW, ai) g.insert(next_move, turn) else: g.print_board() next_move = input( '{}\'s turn: '.format('Red' if turn == RED else 'Yellow')) g.insert(int(next_move), turn) # time.sleep(0.1) turns_played += 1 # g.print_board() turn = YELLOW if turn == RED else RED num_games += 1 winner = g.get_winner() if winner == 1: player1_wins += 1 elif winner == 2: player2_wins += 1 else: ties += 1 return [player1_wins, player2_wins, ties]
def play(games, win, player1, player2): player1_wins = 0 player2_wins = 0 ties = 0 last_winner = -1 num_games = 1 while player1_wins < win and player2_wins < win and num_games < games: g = Game(7, 6, 4) turns_played = 0 if last_winner == -1: turn = random.randint(1, 2) else: turn = last_winner # winner gets to go first while g.get_winner() is None and turns_played < g.rows * g.cols - 1: if turn == YELLOW: next_move = minimax_decision(g, YELLOW, player2) g.insert(next_move, turn) else: next_move = minimax_decision(g, RED, player1) g.insert(next_move, turn) # time.sleep(0.1) turns_played += 1 # g.print_board() turn = YELLOW if turn == RED else RED num_games += 1 winner = g.get_winner() last_winner = winner if winner == 1: player1_wins += 1 elif winner == 2: player2_wins += 1 else: ties += 1 return [player1_wins, player2_wins, ties]
def play(games, win, player1, player2): player1_wins = 0 player2_wins = 0 ties = 0 last_winner = -1 num_games = 1 while player1_wins < win and player2_wins < win and num_games < games: g = Game(7, 6, 4) turns_played = 0 if last_winner == -1: turn = random.randint(1,2) else: turn = last_winner # winner gets to go first while g.get_winner() is None and turns_played < g.rows * g.cols - 1: if turn == YELLOW: next_move = minimax_decision(g,YELLOW,player2) g.insert(next_move, turn) else: next_move = minimax_decision(g,RED,player1) g.insert(next_move, turn) # time.sleep(0.1) turns_played += 1 # g.print_board() turn = YELLOW if turn == RED else RED num_games += 1 winner = g.get_winner() last_winner = winner if winner == 1: player1_wins += 1 elif winner == 2: player2_wins += 1 else: ties += 1 return [player1_wins, player2_wins, ties]
def play(games, win, ai): player1_wins = 0 player2_wins = 0 ties = 0 last_winner = -1 num_games = 1 while player1_wins < win and player2_wins < win and num_games <= games: g = Game(7, 6, 4) turns_played = 0 if last_winner == -1: turn = random.randint(1,2) else: turn = last_winner # winner gets to go first while g.get_winner() is None and turns_played < g.rows * g.cols - 1: if turn == YELLOW: next_move = minimax_decision(g,YELLOW,ai) g.insert(next_move, turn) else: g.print_board() next_move = input('{}\'s turn: '.format('Red' if turn == RED else 'Yellow')) g.insert(int(next_move), turn) # time.sleep(0.1) turns_played += 1 # g.print_board() turn = YELLOW if turn == RED else RED num_games += 1 winner = g.get_winner() if winner == 1: player1_wins += 1 elif winner == 2: player2_wins += 1 else: ties += 1 return [player1_wins, player2_wins, ties]