def test_case1(): player1 = MinimaxPlayer(search_depth=1, score_fn=open_move_score) player2 = GreedyPlayer() game = Board(player1, player2, 9, 9) game._board_state = [0, 0, 0, 0, 0, 0, 0, 0, 0, \ 0, 0, 0, 0, 0, 0, 0, 0, 0, \ 0, 0, 1, 1, 0, 0, 1, 0, 0, \ 0, 0, 0, 0, 1, 1, 0, 0, 0, \ 0, 1, 0, 1, 1, 1, 0, 0, 0, \ 0, 0, 1, 0, 1, 0, 0, 0, 0, \ 0, 0, 1, 1, 1, 0, 0, 0, 0, \ 0, 0, 0, 0, 0, 0, 0, 0, 0, \ 0, 0, 0, 0, 0, 0, 0, 0, 0, \ 0, 37, 57] print('Current game') print(game.to_string()) print('Active player {}'.format(game.active_player)) print('Legal moves\n\t{}'.format(game.get_legal_moves())) time_limit = 10 time_millis = lambda: 1000 * timeit.default_timer() move_start = time_millis() time_left = lambda: 20 game_copy = game.copy() print('Next move: \n\t{}'.format(game.active_player.get_move(game_copy, time_left))) print('Expected move is (5,5), please verify!!!')
def test2(): player1 = AlphaBetaPlayer() player2 = AlphaBetaPlayer() game = Board(player1, player2, 9, 9) game._board_state = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 1, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 1, 0, 1, 1, 1, 0, 0, 0, 0, 1, 1, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 31, 22] #print(player1.get_move(game, 6)) moves = game.get_legal_moves() for m in moves: fm = game.forecast_move(m).get_legal_moves() print (str(m) + " -->" + str(fm) + str(player1.score(game.forecast_move(m),player1))) print (player1.get_move(game, 6))
def test_case3(): with open('dbg.pkl', 'rb') as f: dbg_dict = pickle.load(f) # print(dbg_dict['hist']) # print(dbg_dict['board']) # print(dbg_dict['player1']) # print(dbg_dict['player2']) player1 = AlphaBetaPlayer(score_fn=improved_score) player2 = RandomPlayer() game = Board(player1, player2) game._board_state = dbg_dict['board'] game.play_next_move()
def test_case2(state, alpha, beta): player1 = AlphaBetaPlayer(search_depth=2, score_fn = open_move_score) player2 = GreedyPlayer() game = Board(player1, player2, 9, 9) game._board_state = state time_limit = 150 time_millis = lambda: 1000 * timeit.default_timer() move_start = time_millis() time_left = lambda : time_limit - (time_millis() - move_start) game_copy = game.copy() player1.time_left = time_left print('Next move: \n\t{}'.format(player1.alphabeta(game_copy, 2, alpha, beta))) print('Time left/limit ({:.2f}/{:.2f}) ms'.format(time_left(), time_limit))
# print(game.get_legal_moves()) # print(len(game.get_legal_moves())) score = custom_score_2(game, player1) time_millis = lambda: 1000 * timeit.default_timer() move_start = time_millis() time_left = lambda: 100 - (time_millis() - move_start) # next_move = player1.get_move(game, time_left) # print(next_move) failed_test_case_7 = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 1, 1, 0, 0, 0, 0, 1, 1, 1, 0, 1, 0, 1, 0, 0, 0, 1, 1, 1, 1, 1, 0, 0, 0, 1, 1, 1, 0, 1, 0, 0, 0, 0, 1, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 51, 23] game1 = Board(player1, player2, 9, 9) game1._board_state = failed_test_case_7 next_move = player1.get_move(game1, time_left) print(next_move) print(game1.get_legal_moves())
NUM_MATCHES = 5 # number of matches against each opponent TIME_LIMIT = 150 # number of milliseconds before timeout Agent = namedtuple("Agent", ["player", "name"]) Agent1 = Agent(AlphaBetaPlayer(score_fn=open_move_score), "MM_Open") #Agent1 = Agent(MinimaxPlayer(score_fn=open_move_score), "MM_Open") #Agent2 = Agent(RandomPlayer(), "Random") Agent2 = Agent(AlphaBetaPlayer(score_fn=improved_score), "AB_Improved") game = Board(Agent1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, 0, 0, 0, 0, 0, 0, 47, 51] print(game.to_string()) legal_player_moves = game.get_legal_moves() legal_player_moves.sort() print("legal_player_moves:",legal_player_moves) move_history = [] time_limit = 150 time_millis = lambda: 1000 * timeit.default_timer() game_copy = game.copy() move_start = time_millis() # initialize starting point each time time_left = lambda : time_limit - (time_millis() - move_start)
from isolation import Board from sample_players import GreedyPlayer from game_agent import AlphaBetaPlayer player1 = AlphaBetaPlayer() player2 = AlphaBetaPlayer() game = Board(player1, player2, 9, 9) print(game.to_string()) game._board_state = [ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 67, 49 ] print(game.to_string()) assert (player1 == game.active_player) print(game.get_legal_moves()) # new_game = game.forecast_move((1, 1)) # assert(new_game.to_string() != game.to_string()) # print("\nOld state:\n{}".format(game.to_string())) # print("\nNew state:\n{}".format(new_game.to_string())) winner, history, outcome = game.play() print("\nWinner: {}\nOutcome: {}".format(winner, outcome)) print(game.to_string()) # print("Move history:\n{!s}".format(history))
TIME_LIMIT = 150 # number of milliseconds before timeout Agent = namedtuple("Agent", ["player", "name"]) Agent1 = Agent(AlphaBetaPlayer(score_fn=open_move_score), "MM_Open") #Agent1 = Agent(MinimaxPlayer(score_fn=open_move_score), "MM_Open") #Agent2 = Agent(RandomPlayer(), "Random") Agent2 = Agent(AlphaBetaPlayer(score_fn=improved_score), "AB_Improved") game = Board(Agent1.player, Agent2.player, 9, 9) #game._board_state = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 14, 51] #game._board_state = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 1, 1, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 24, 32] #game._board_state = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 1, 0, 1, 1, 0, 0, 0, 0, 0, 1, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 13, 51] game._board_state = [ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 1, 0, 1, 1, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 47, 51 ] print(game.to_string()) legal_player_moves = game.get_legal_moves() legal_player_moves.sort() print("legal_player_moves:", legal_player_moves) move_history = [] time_limit = 150 time_millis = lambda: 1000 * timeit.default_timer() game_copy = game.copy() move_start = time_millis() # initialize starting point each time
from isolation import Board from game_agent import * from sample_players import HumanPlayer import timeit player1 = MinimaxPlayer(score_fn=custom_score) player2 = AlphaBetaPlayer(score_fn=custom_score) board = Board(player1, player2) board._board_state = [ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1, 1, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 41, 57 ] print(board.play(250000)) print(board.to_string()) print(board.get_legal_moves(player1)) print(board.get_legal_moves(player2)) ''' opening_book_p2 = {(0, 0): (5, 2), (1, 0): (6, 5), (2, 0): (6, 5), (3, 0): (6, 5), \ (0, 1): (6, 5), (1, 1): (6, 5), (2, 1): (6, 5), (3, 1): (6, 5), \ (0, 2): (6, 5), (1, 2): (6, 5), (2, 2): (6, 5), (3, 2): (6, 5), \ (0, 3): (6, 5), (1, 3): (6, 5), (2, 3): (6, 5), (3, 3): (6, 5)} for box in opening_book_p2.keys(): new_board = board.forecast_move(box) move_history = []