def get_action(gameState, depth_limit): # TODO: Implement a function that calls minimax_decision # for each depth from 1...depth_limit (inclusive of both endpoints) m = None for d in range(1, depth_limit + 1): m = minimax_decision(gameState, d) return m
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]
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]
import minimax import gamestate as game best_moves = set([(0, 0), (2, 0), (0, 1)]) rootNode = game.GameState() minimax_move = minimax.minimax_decision(rootNode) print("Best move choices: {}".format(list(best_moves))) print("Your code chose: {}".format(minimax_move)) if minimax_move in best_moves: print("That's one of the best move choices. Looks like your minimax-decision function worked!") else: print("Uh oh...looks like there may be a problem.")
import minimax import gamestate as game # Test the depth limit by checking the number of nodes visited # -- recall that minimax visits every node in the search tree, # so if we search depth one on an empty board then minimax should # visit each of the five open spaces depth_limit = 1 expected_node_count = 5 rootNode = game.GameState() _ = minimax.minimax_decision(rootNode, depth_limit) print("Expected node count: {}".format(expected_node_count)) print("Your node count: {}".format(game.call_counter)) if game.call_counter == expected_node_count: print("That's right! Looks like your depth limit is working!") else: print("Uh oh...looks like there may be a problem.")
def test_minimax_decision(self): game = gamestate.GameState() self.assertIn(minimax.minimax_decision(game), [(0, 1), (2, 0), (0, 0)])
def get_action(gameState, depth_limit): # Turns out "iterative deepening" is just a for loop... best_move = None for depth in range(1, depth_limit + 1): best_move = minimax_decision(gameState, depth) return best_move
def get_action(gameState, depth_limit): ''' implement iterative deepening ''' best_move = None for depth in range(1, depth_limit + 1): best_move = minimax_decision(gameState, depth) return best_move
def get_action(gameState, depth_limit): best_move = None for level in range(1, depth_limit + 1): best_move = minimax_decision(gameState, level) return best_move