def random_board(simulate_till=4):
    def time_left():
        return 100000

    randomAgent1 = RandomPlayer()
    randomAgent2 = RandomPlayer()

    game = Board(randomAgent1, randomAgent2)
    move_history = []
    for move_idx in range(simulate_till):
        if move_idx == 0:
            curr_move = (3, 3, False)
        elif move_idx == 1:  # Non mirrorable moves
            curr_move = random.choice(((1, 2, False), (1, 4, False), (2, 5, False), (4, 5, False), \
                                       (5, 4, False), (5, 2, False), (4, 1, False), (2, 1, False)))
        else:
            curr_move = game.__active_player__.move(game, time_left)
            curr_move = (curr_move[0], curr_move[1], bool(curr_move[2]))

        if curr_move not in game.get_active_moves():
            raise Exception("Illegal move played")

        # Append new move to game history
        if game.__active_player__ == game.__player_1__:
            move_history.append([curr_move])
        else:
            move_history[-1].append(curr_move)

        is_over, winner = game.__apply_move__(curr_move)

        if is_over:
            raise ("Game over while simulating board")

    return game, move_history
Example #2
0
def testUtility():
    try:
        sample_board = Board(RandomPlayer(), RandomPlayer())
        # setting up the board as though we've been playing
        sample_board.move_count = 4
        sample_board.__board_state__ = [[11, 0, 0, 0, 21, 0, 0],
                                        [0, 0, 0, 0, 0, 0, 0],
                                        [0, 0, 22, 0, 0, 0, 0],
                                        [0, 0, 0, 0, 0, 0, 0],
                                        [0, 0, 0, 0, 0, 12, 0],
                                        [0, 0, 0, 0, 0, 0, 0],
                                        [0, 0, 0, 0, 0, 0, 0]]
        sample_board.__last_queen_move__ = {
            sample_board.queen_11: (0, 0),
            sample_board.queen_12: (4, 5),
            sample_board.queen_21: (0, 4),
            sample_board.queen_22: (2, 2)
        }
        test = sample_board.get_legal_moves()
        h = OpenMoveEvalFn()
        print 'OpenMoveEvalFn Test: This board has a score of %s.' % (
            h.score(sample_board))
        sample_board.print_board()
    except NotImplementedError:
        print 'OpenMoveEvalFn Test: Not implemented'
    except:
        print 'OpenMoveEvalFn Test: ERROR OCCURRED'
        print traceback.format_exc()
def correctOpenEvalFn(yourOpenEvalFn):
    print()
    try:
        sample_board = Board(RandomPlayer(), RandomPlayer())
        # setting up the board as though we've been playing
        board_state = [
            ["Q1", " ", " ", " ", " ", " ", " "],
            [" ", " ", " ", " ", " ", " ", " "],
            [" ", " ", " ", " ", " ", " ", " "],
            [" ", " ", " ", " ", " ", " ", " "],
            [" ", " ", " ", "Q2", " ", " ", " "],
            [" ", " ", " ", " ", " ", " ", " "],
            [" ", " ", " ", " ", " ", " ", " "]
        ]
        sample_board.set_state(board_state, True)
        #test = sample_board.get_legal_moves()
        h = yourOpenEvalFn()
        print('OpenMoveEvalFn Test: This board has a score of %s.' % (h.score(sample_board, sample_board.get_active_player())))
    except NotImplementedError:
        print('OpenMoveEvalFn Test: Not implemented')
    except:
        print('OpenMoveEvalFn Test: ERROR OCCURRED')
        print(traceback.format_exc())

    print()
Example #4
0
def testMiniMax():
    try:
        """Example test to make sure
        your minimax works, using the
        #computer_player_moves - opponent_moves evaluation function."""
        # create dummy 3x3 board

        p1 = RandomPlayer()
        p2 = CustomPlayerAB(search_depth=3)
        #p2 = HumanPlayer()
        b = Board(p1, p2, 5, 5)
        b.__board_state__ = [[0, 21, 0, 0, 0], [0, 0, 11, 0, 0],
                             [0, 0, 12, 0, 0], [0, 0, 0, 0, 0],
                             [0, 22, 0, 0, 0]]
        b.__last_queen_move__["queen11"] = (1, 2)
        b.__last_queen_move__["queen21"] = (0, 1)
        b.__last_queen_move__["queen12"] = (2, 2)
        b.__last_queen_move__["queen22"] = (4, 1)

        b.move_count = 4

        output_b = b.copy()
        winner, move_history, queen_history, termination = b.play_isolation(
            1000, True)
        print 'Minimax Test: Runs Successfully'
        # Uncomment to see example game
        print game_as_text(winner, move_history, queen_history,
                           b.output_history, termination, output_b)
    except NotImplementedError:
        print 'Minimax Test: Not Implemented'
    except:
        print 'Minimax Test: ERROR OCCURRED'
        print traceback.format_exc()
Example #5
0
def main():
    print("Starting game:")

    from test_players import RandomPlayer
    from test_players import HumanPlayer

    board = Board(RandomPlayer(), HumanPlayer())
    board_copy = board.copy()
    winner, move_history, queen_history, termination = board.play_isolation(time_limit=30000, print_moves=True)
    print game_as_text(winner, move_history, queen_history, board.output_history, termination, board_copy)
Example #6
0
def testCustomABPlayRandom():
    """Example test you can run
    to make sure your AI does better
    than random."""
    try:
        r = CustomPlayerAB(search_depth=10)
        h = RandomPlayer()
        game = Board(r, h, 7, 7)
        output_b = game.copy()
        winner, move_history, queen_history, termination = game.play_isolation(
            1000, True)
        game.print_board()
        print game_as_text(winner, move_history, queen_history,
                           game.output_history, termination, output_b)
    except NotImplementedError:
        print 'CustomPlayer Test: Not Implemented'
    except:
        print 'CustomPlayer Test: ERROR OCCURRED'
        print traceback.format_exc()
Example #7
0
def beatRandom(yourAgent):
    """Example test you can run
    to make sure your AI does better
    than random."""

    print("")
    try:
        r = RandomPlayer()
        p = yourAgent()
        game = Board(r, p, 7, 7)
        output_b = game.copy()
        winner, move_history, termination = game.play_isolation(
            time_limit=1000, print_moves=True)
        print("\n", winner, " has won. Reason: ", termination)
        # Uncomment to see game
        # print game_as_text(winner, move_history, termination, output_b)
    except NotImplementedError:
        print('CustomPlayer Test: Not Implemented')
    except:
        print('CustomPlayer Test: ERROR OCCURRED')
        print(traceback.format_exc())

    print()
Example #8
0
def minimaxTest(yourAgent, minimax_fn):
    """Example test to make sure
    your minimax works, using the
    OpenMoveEvalFunction evaluation function.
    This can be used for debugging your code
    with different model Board states.
    Especially important to check alphabeta
    pruning"""

    # create dummy 5x5 board
    print("Now running the Minimax test.")
    print()
    try:

        def time_left():  # For these testing purposes, let's ignore timeouts
            return 10000

        player = yourAgent()  #using as a dummy player to create a board
        sample_board = Board(player, RandomPlayer())
        # setting up the board as though we've been playing
        board_state = [["Q1", " ", " ", " ", " ", "X", " "],
                       [" ", " ", " ", " ", " ", " ", " "],
                       ["X", " ", " ", " ", " ", " ", " "],
                       [" ", " ", "X", "Q2", "X", " ", " "],
                       ["X", "X", "X", " ", "X", " ", " "],
                       [" ", " ", "X", " ", "X", " ", " "],
                       [" ", " ", "X", " ", "X", " ", " "]]
        sample_board.set_state(board_state, True)

        test_pass = True

        expected_depth_scores = [(1, 4), (2, -2), (3, 4), (4, -2), (5, 2)]

        for depth, exp_score in expected_depth_scores:
            move, score = minimax_fn(player,
                                     sample_board,
                                     time_left,
                                     depth=depth,
                                     my_turn=True)
            if exp_score != score:
                print("Minimax failed for depth: ", depth)
                test_pass = False

        if test_pass:
            player = yourAgent()
            sample_board = Board(player, RandomPlayer())
            # setting up the board as though we've been playing
            board_state = [[" ", " ", " ", " ", "X", " ", "X"],
                           ["X", "X", "X", " ", "X", "Q2", " "],
                           [" ", "X", "X", " ", "X", " ", " "],
                           ["X", "X", "X", " ", "X", "X", " "],
                           ["X", " ", "Q1", " ", "X", " ", "X"],
                           ["X", " ", " ", " ", "X", "X", " "],
                           ["X", " ", " ", " ", "X", " ", " "]]
            sample_board.set_state(board_state, True)

            test_pass = True

            expected_depth_scores = [(1, 5), (2, 5), (3, 5), (4, 6), (5, 6)]

            for depth, exp_score in expected_depth_scores:
                move, score = minimax_fn(player,
                                         sample_board,
                                         time_left,
                                         depth=depth,
                                         my_turn=True)
                if exp_score != score:
                    print("Minimax failed for depth: ", depth)
                    test_pass = False

        if test_pass:
            print("Minimax Test: Runs Successfully!")

    except NotImplementedError:
        print('Minimax Test: Not implemented')
    except:
        print('Minimax Test: ERROR OCCURRED')
        print(traceback.format_exc())
Example #9
0
        ans.write(queen_history[k][0] + "  player1 " + "%d." % i +
                  " (%d,%d)\r\n" % p1_move)
        if p1_move != Board.NOT_MOVED:
            board.__apply_move_write__(p1_move, queen_history[k][0])
        ans.write(board.print_board())

        if len(move1) > 1:
            p2_move = move1[1]
            ans.write(queen_history[k][1] + " player2 " + "%d. ..." % i +
                      " (%d,%d)\r\n" % p2_move)
            if p2_move != Board.NOT_MOVED:
                board.__apply_move_write__(p2_move, queen_history[k][1])
            ans.write(board.print_board())
        k = k + 1
    ans.write(termination + "\r\n")
    ans.write("Winner: " + str(winner) + "\r\n")

    return ans.getvalue()


if __name__ == '__main__':

    print("Starting game:")

    from test_players import RandomPlayer
    from test_players import HumanPlayer

    board = Board(RandomPlayer(), HumanPlayer())
    winner, move_history, queen_history, termination = board.play_isolation()
    print(game_as_text(winner, move_history, queen_history, termination))
Example #10
0
def main():


    # try:
    #     sample_board = Board(RandomPlayer(), RandomPlayer())
    #     # setting up the board as though we've been playing
    #     sample_board.move_count = 1
    #     sample_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, 0, 0, 'Q', 0, 0, 0],
    #         [0, 0, 0, 0, 0, 0, 0],
    #         [0, 0, 0, 0, 0, 0, 0],
    #         [0, 0, 0, 0, 0, 0, 0]
    #     ]
    #     sample_board.__last_queen_move__ = (3,3)
    #     test = sample_board.get_legal_moves()
    #     h = OpenMoveEvalFn()
    #     print 'OpenMoveEvalFn Test: This board has a score of %s.' % (h.score(sample_board))
    # except NotImplementedError:
    #     print 'OpenMoveEvalFn Test: Not implemented'
    # except:
    #     print 'OpenMoveEvalFn Test: ERROR OCCURRED'
    #     print traceback.format_exc()
    #
    #
    # try:
    #     """Example test to make sure
    #     your minimax works, using the
    #     #computer_player_moves."""
    #     # create dummy 5x5 board
    #
    #     p1 = CustomPlayer()
    #     p2 = CustomPlayer(search_depth=3)
    #     #p2 = HumanPlayer()
    #     b = Board(p1, p2, 5, 5)
    #     b.__board_state__ = [
    #         [0, 0, 0, 0, 0],
    #         [0, 0, 0, 0, 0],
    #         [0, 0, 'Q', 0, 0],
    #         [0, 0, 0, 0, 0],
    #         [0, 0, 0, 0, 0]
    #     ]
    #     b.__last_queen_move__ = (2, 2)
    #
    #     b.move_count = 1
    #
    #     output_b = b.copy()
    #     winner, move_history, termination = b.play_isolation_name_changed()
    #     print 'Minimax Test: Runs Successfully'
    #     print winner
    #     # Uncomment to see example game
    #     # print game_as_text(winner, move_history,  termination, output_b)
    # except NotImplementedError:
    #     print 'Minimax Test: Not Implemented'
    # except:
    #     print 'Minimax Test: ERROR OCCURRED'
    #     print traceback.format_exc()
    #

    win = 0
    lose = 0
    for i in range(1, 101, 1):
        """Example test you can run
        to make sure your AI does better
        than random."""
        try:
            r = CustomPlayer()
            h = RandomPlayer()
            game = Board(h, r, 7, 7)
            output_b = game.copy()
            winner, move_history, termination = game.play_isolation_name_changed()
            if 'CustomPlayer' in str(winner):
                print 'CustomPlayer Test: CustomPlayer Won'
                win += 1
            else:
                print 'CustomPlayer Test: CustomPlayer Lost'
                lose += 1
            # Uncomment to see game
            # print game_as_text(winner, move_history, termination, output_b)
        except NotImplementedError:
            print 'CustomPlayer Test: Not Implemented'
        except:
            print 'CustomPlayer Test: ERROR OCCURRED'
            print traceback.format_exc()

    print 'Win:', win, 'Lost:', lose
Example #11
0
def main():


    try:
        sample_board = Board(RandomPlayer(), RandomPlayer())
        # setting up the board as though we've been playing
        sample_board.move_count = 1
        sample_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, 0, 0, 'Q', 0, 0, 0],
            [0, 0, 0, 0, 0, 0, 0],
            [0, 0, 0, 0, 0, 0, 0],
            [0, 0, 0, 0, 0, 0, 0]
        ]
        sample_board.__last_queen_move__ = (3,3)
        test = sample_board.get_legal_moves()
        #h = OpenMoveEvalFn()
        h = CustomEvalFn()
        print 'OpenMoveEvalFn Test: This board has a score of %s.' % (h.score(sample_board))
    except NotImplementedError:
        print 'OpenMoveEvalFn Test: Not implemented'
    except:
        print 'OpenMoveEvalFn Test: ERROR OCCURRED'
        print traceback.format_exc()


    try:
        """Example test to make sure
        your minimax works, using the
        #computer_player_moves."""
        # create dummy 5x5 board

        p1 = CustomPlayer()
        p2 = CustomPlayer(search_depth=3)
        #p2 = HumanPlayer()
        b = Board(p1, p2, 5, 5)
        b.__board_state__ = [
            [0, 0, 0, 0, 0],
            [0, 0, 0, 0, 0],
            [0, 0, 'Q', 0, 0],
            [0, 0, 0, 0, 0],
            [0, 0, 0, 0, 0]
        ]
        b.__last_queen_move__ = (2, 2)
       
        b.move_count = 1

        output_b = b.copy()
        winner, move_history, termination = b.play_isolation_name_changed()
        print 'Minimax Test: Runs Successfully'
	print winner
        # Uncomment to see example game
        #print game_as_text(winner, move_history,  termination, output_b)
    except NotImplementedError:
        print 'Minimax Test: Not Implemented'
    except:
        print 'Minimax Test: ERROR OCCURRED'
        print traceback.format_exc()



    """Example test you can run
    to make sure your AI does better
    than random."""
    try:
        r = CustomPlayer_1(8)
#        h = RandomPlayer()
        
        h = CustomPlayer()
        #r = RandomPlayer()
        game = Board(r, h, 7, 7)
        output_b = game.copy()
        winner, move_history, termination = game.play_isolation_name_changed()
        if 'CustomPlayer' in str(winner):
            print 'CustomPlayer Test: CustomPlayer Won'
        else:
            print 'CustomPlayer Test: CustomPlayer Lost'
        # Uncomment to see game
        print game_as_text(winner, move_history, termination, output_b)
    except NotImplementedError:
        print 'CustomPlayer Test: Not Implemented'
    except:
        print 'CustomPlayer Test: ERROR OCCURRED'
        print traceback.format_exc()
Example #12
0
        if p1_move != Board.NOT_MOVED:
            board.__apply_move__(p1_move)
        ans.write(board.print_board())

        if len(move) > 1:
            p2_move = move[1]
            ans.write("%d. ..." % i + " (%d,%d)\n" % p2_move)
            if p2_move != Board.NOT_MOVED:
                board.__apply_move__(p2_move)
            ans.write(board.print_board())

    ans.write(termination + "\n")

    ans.write("Winner: " + str(winner) + "\n")

    return ans.getvalue()


if __name__ == '__main__':

    print("Starting game:")

    from test_players import RandomPlayer
    from test_players import HumanPlayer
    from player_submission import CustomPlayer

    board = Board(RandomPlayer(), CustomPlayer(), 3, 3)
    winner, move_history, termination = board.play_isolation()
    print board.print_board()
    print game_as_text(winner, move_history, termination)
Example #13
0
def main():
    # print ""
    # try:
    #     sample_board = Board(RandomPlayer(), RandomPlayer())
    #     # setting up the board as though we've been playing
    #     sample_board.move_count = 2
    #     sample_board.__board_state__ = [
    #         ["Q1", " ", " ", " ", " ", " ", " "],
    #         [" ", " ", " ", " ", " ", " ", " "],
    #         [" ", " ", " ", " ", " ", " ", " "],
    #         [" ", " ", " ", "Q2", " ", " ", " "],
    #         [" ", " ", " ", " ", " ", " ", " "],
    #         [" ", " ", " ", " ", " ", " ", " "],
    #         [" ", " ", " ", " ", " ", " ", " "]
    #     ]
    #     sample_board.__last_queen_move__ = {sample_board.__queen_1__: (0, 0, False), \
    #                                         sample_board.__queen_2__: (3, 3, False)}
    #     test = sample_board.get_legal_moves()
    #     h = OpenMoveEvalFn()
    #     print 'OpenMoveEvalFn Test: This board has a score of %s.' % (h.score(sample_board))
    # except NotImplementedError:
    #     print 'OpenMoveEvalFn Test: Not implemented'
    # except:
    #     print 'OpenMoveEvalFn Test: ERROR OCCURRED'
    #     print traceback.format_exc()
    #
    # print ""
    # try:
    #     """Example test to make sure
    #     your minimax works, using the
    #     OpenMoveEvalFunction evaluation function.
    # 	This can be used for debugging your code
    # 	with different model Board states.
    # 	Especially important to check alphabeta
    # 	pruning"""
    #     # create dummy 5x5 board
    #     b = Board(RandomPlayer(), HumanPlayer(), 5, 5)
    #
    #     b.__board_state__ = [
    #         [" ", " ", " ", " ", " "],
    #         [" ", " ", " ", " ", " "],
    #         [" ", " ", " ", "Q1", " "],
    #         [" ", " ", " ", "Q2", " "],
    #         [" ", " ", " ", " ", " "]
    #     ]
    #     b.__last_queen_move__[b.__queen_1__] = (2, 3, False)
    #     b.__last_queen_move__[b.__queen_2__] = (3, 3, False)
    #     b.move_count = 2
    #
    #     output_b = b.copy()
    #     legal_moves = b.get_legal_moves()
    #     winner, move_history, termination = b.play_isolation(
    #         time_limit=100000, print_moves=True)
    #     print 'Minimax Test: Runs Successfully'
    #     # Uncomment to see example game
    # # insert in reverse order
    # # initial_turn = [(2, 3, False), (3, 3, False)]
    # # move_history.insert(0, initial_turn)
    # # print game_as_text(winner, move_history, termination, output_b)
    # except NotImplementedError:
    #     print 'Minimax Test: Not Implemented'
    # except:
    #     print 'Minimax Test: ERROR OCCURRED'
    #     print traceback.format_exc()
    """Example test you can run
    to make sure your AI does better
    than random."""
    cnt = 0
    for i in range(0, 1):

        try:
            r = RandomPlayer()
            h = CustomPlayer(2)
            game = Board(r, h, 7, 7)

            output_b = game.copy()
            winner, move_history, termination = game.play_isolation(
                time_limit=1000, print_moves=True)
            if winner == 'CustomPlayer - Q2':
                cnt += 1

            print "\n", winner, " has won. Reason: ", termination
            # Uncomment to see game
            # print game_as_text(winner, move_history, termination, output_b)
        except NotImplementedError:
            print 'CustomPlayer Test: Not Implemented'
        except:
            print 'CustomPlayer Test: ERROR OCCURRED'
            print traceback.format_exc()
    print "Win Rate ", float(cnt * 1.0 / 100.0)
def main():

    try:
        sample_board = Board(RandomPlayer(), RandomPlayer())
        # setting up the board as though we've been playing
        sample_board.move_count = 4
        sample_board.__board_state__ = [[11, 0, 0, 0, 21, 0, 0],
                                        [0, 0, 0, 0, 0, 0, 0],
                                        [0, 0, 22, 0, 0, 0, 0],
                                        [0, 0, 0, 0, 0, 0, 0],
                                        [0, 0, 0, 0, 0, 12, 0],
                                        [0, 0, 0, 0, 0, 0, 0],
                                        [0, 0, 0, 0, 0, 0, 0]]
        sample_board.__last_queen_move__ = {
            sample_board.queen_11: (0, 0),
            sample_board.queen_12: (4, 5),
            sample_board.queen_21: (0, 4),
            sample_board.queen_22: (2, 2)
        }
        test = sample_board.get_legal_moves()
        h = OpenMoveEvalFn()
        print 'OpenMoveEvalFn Test: This board has a score of %s.' % (
            h.score(sample_board))
    except NotImplementedError:
        print 'OpenMoveEvalFn Test: Not implemented'
    except:
        print 'OpenMoveEvalFn Test: ERROR OCCURRED'
        print traceback.format_exc()

    try:
        """Example test to make sure
        your minimax works, using the
        OpenMoveEvalFunction evaluation function.
	This can be used for debugging your code
	with different model Board states. 
	Especially important to check alphabeta 
	pruning"""
        # create dummy 5x5 board

        p1 = RandomPlayer()
        p2 = HumanPlayer()
        b = Board(p1, p2, 5, 5)

        b.__board_state__ = [[0, 0, 0, 0, 0], [0, 0, 0, 22, 0],
                             [0, 0, 0, 11, 0], [0, 0, 0, 21, 12],
                             [0, 0, 0, 0, 0]]
        b.__last_queen_move__["queen11"] = (2, 3)
        b.__last_queen_move__["queen12"] = (3, 4)
        b.__last_queen_move__["queen21"] = (3, 3)
        b.__last_queen_move__["queen22"] = (1, 3)
        b.move_count = 4

        output_b = b.copy()
        legal_moves = b.get_legal_moves()
        winner, move_history, termination = b.play_isolation()
        print 'Minimax Test: Runs Successfully'
        # Uncomment to see example game
        print game_as_text(winner, move_history, termination, output_b)
    except NotImplementedError:
        print 'Minimax Test: Not Implemented'
    except:
        print 'Minimax Test: ERROR OCCURRED'
        print traceback.format_exc()
    """Example test you can run
    to make sure your AI does better
    than random."""
    try:
        r = RandomPlayer()
        h = CustomPlayer()
        game = Board(r, h, 7, 7)
        output_b = game.copy()
        winner, move_history, termination = game.play_isolation()
        print game_as_text(winner, move_history, termination, output_b)
        if 'CustomPlayer' in str(winner):
            print 'CustomPlayer Test: CustomPlayer Won'
        else:
            print 'CustomPlayer Test: CustomPlayer Lost'
        # Uncomment to see game
        # print game_as_text(winner, move_history, termination, output_b)
    except NotImplementedError:
        print 'CustomPlayer Test: Not Implemented'
    except:
        print 'CustomPlayer Test: ERROR OCCURRED'
        print traceback.format_exc()
Example #15
0
"""Example test you can run
to make sure your basic evaluation
function works."""
from isolation import Board
from test_players import RandomPlayer
from player_submission import OpenMoveEvalFn

if __name__ == "__main__":
    sample_board = Board(RandomPlayer(),RandomPlayer())
    # setting up the board as though we've been playing
    sample_board.move_count = 3
    sample_board.__active_player__ = 0 # player 1 = 0, player 2 = 1
    # 1st board = 7 moves
    sample_board.__board_state__ = [
                [0,2,0,0,0,0,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,0,0,0,0],
                [0,0,0,0,0,0,0],
                [0,0,0,0,0,0,0]
    ]
    sample_board.__last_player_move__ = {0: (2,2), 1: (0,1)}

    # player 1 should have 7 moves available,
    # so board gets a score of 7
    h = OpenMoveEvalFn()
    print('This board has a score of %s.'%(h.score(sample_board)))
def main():
    """
    print ""
    try:
        sample_board = Board(RandomPlayer(), RandomPlayer())
        # setting up the board as though we've been playing
        sample_board.move_count = 2
        sample_board.__board_state__ = [
            ["Q1", " ", " ", " ", " ", " ", " "],
            [ " ", " ", " ", " ", " ", " ", " "],
            [ " ", " ", " ", " ", " ", " ", " "],
            [ " ", " ", " ","Q2", " ", " ", " "],
            [ " ", " ", " ", " ", " ", " ", " "],
            [ " ", " ", " ", " ", " ", " ", " "],
            [ " ", " ", " ", " ", " ", " ", " "]
        ]
        sample_board.__last_queen_move__ = {sample_board.__queen_1__: (0, 0, False), \
                                            sample_board.__queen_2__: (3, 3, False)}
        test = sample_board.get_legal_moves()
        h = OpenMoveEvalFn()
        print 'OpenMoveEvalFn Test: This board has a score of %s.' % (h.score(sample_board))
    except NotImplementedError:
        print 'OpenMoveEvalFn Test: Not implemented'
    except:
        print 'OpenMoveEvalFn Test: ERROR OCCURRED'
        print traceback.format_exc()

    """

    print ""
    try:
        """Example test to make sure
        your minimax works, using the
        OpenMoveEvalFunction evaluation function.
    	This can be used for debugging your code
    	with different model Board states. 
    	Especially important to check alphabeta 
    	pruning"""
        # create dummy 5x5 board
        b = Board(RandomPlayer(), CustomPlayer(4), 5, 5)

        b.__board_state__ = [[" ", " ", " ", " ", " "],
                             [" ", " ", " ", " ", " "],
                             [" ", " ", " ", "Q1", " "],
                             [" ", " ", " ", "Q2", " "],
                             [" ", " ", " ", " ", " "]]
        b.__last_queen_move__[b.__queen_1__] = (2, 3, False)
        b.__last_queen_move__[b.__queen_2__] = (3, 3, False)
        b.move_count = 2

        output_b = b.copy()
        legal_moves = b.get_legal_moves()
        winner, move_history, termination = b.play_isolation(time_limit=100000,
                                                             print_moves=True)
        print 'Minimax Test: Runs Successfully'
        # Uncomment to see example game

#insert in reverse order
#initial_turn = [(2, 3, False), (3, 3, False)]
#move_history.insert(0, initial_turn)
#print game_as_text(winner, move_history, termination, output_b)
    except NotImplementedError:
        print 'Minimax Test: Not Implemented'
    except:
        print 'Minimax Test: ERROR OCCURRED'
        print traceback.format_exc()
    """Example test you can run
    to make sure your AI does better
    than random."""
    print ""
    try:
        r = RandomPlayer()
        h = CustomPlayer()
        game = Board(r, h, 7, 7)
        output_b = game.copy()
        winner, move_history, termination = game.play_isolation(
            time_limit=1000, print_moves=True)
        print "\n", winner, " has won. Reason: ", termination
        # Uncomment to see game
        # print game_as_text(winner, move_history, termination, output_b)
    except NotImplementedError:
        print 'CustomPlayer Test: Not Implemented'
    except:
        print 'CustomPlayer Test: ERROR OCCURRED'
        print traceback.format_exc()
Example #17
0
"""Example test you can run
to make sure your AI does better
than random."""
from isolation import Board, game_as_text
from test_players import RandomPlayer
from player_submission import CustomPlayer

if __name__ == "__main__":
    r = RandomPlayer()
    h = CustomPlayer()
    game = Board(h, r)
    output = game.copy()
    winner, move_history, termination = game.play_isolation(time_limit=500)
    print game_as_text(winner, move_history, termination, output)
def alphabetaTest1(yourAgent, minimax_fn):
    """Example test to make sure
    your alphabeta works, using the
    OpenMoveEvalFunction evaluation function.
    This can be used for debugging your code
    with different model Board states.
    Especially important to check alphabeta
    pruning"""

    # create dummy 3x3 board
    print("Now running the AlphaBeta test.")
    print()
    try:

        def time_left():  # For these testing purposes, let's ignore timeouts
            return 10000

        player = yourAgent()  #using as a dummy player to create a board
        sample_board = Board(player, RandomPlayer(), width=3, height=3)
        # setting up the board as though we've been playing
        # board_state = [
        #     [" ",  " ",  "Q2"],
        #     [" ",  "Q1", "X"],
        #     ["X",  "X",  "X"]
        # ]
        # board_state = [
        #     ["X",  " ",  "Q2"],
        #     [" ",  " ",  " "],
        #     ["Q1",  "X", " "]
        # ]
        board_state = [[" ", " ", "X"], [" ", "Q1", " "], ["Q2", " ", "X"]]
        sample_board.set_state(board_state, True)

        test_pass = True

        expected_depth_scores = [(2, 0)]

        for depth, exp_score in expected_depth_scores:
            move, score = minimax_fn(player,
                                     sample_board,
                                     time_left,
                                     depth=depth,
                                     my_turn=True)
            # print(move, score)
            if exp_score != score:
                print(score, exp_score)
                print("AlphaBeta failed for depth: ", depth)
                test_pass = False

        # if test_pass:
        #     player = yourAgent()
        #     sample_board = Board(player, RandomPlayer())
        #     # setting up the board as though we've been playing
        #     board_state = [
        #         ["X",  " ",  "Q2"],
        #         [" ",  " ",  " "],
        #         ["Q1",  "X", " "]
        #     ]
        #     sample_board.set_state(board_state, True)
        #
        #     test_pass = True
        #
        #     expected_depth_scores = [(2,0)]
        #
        #     for depth, exp_score in expected_depth_scores:
        #         move, score = minimax_fn(player, sample_board, time_left, depth=depth, my_turn=True)
        #         if exp_score != score:
        #             # print("Minimax failed for depth: ", depth)
        #             print("Lev2: AlphaBeta failed for depth: ", depth, " Score, ", score)
        #             test_pass = False

        if test_pass:
            print("AlphaBeta Test: Runs Successfully!")

    except NotImplementedError:
        print('AlphaBeta Test: Not implemented')
    except:
        print('AlphaBeta Test: ERROR OCCURRED')
        print(traceback.format_exc())