Ejemplo n.º 1
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()
Ejemplo n.º 2
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()
Ejemplo n.º 3
0
def test_alphabeta():
    # For alpha beta pruning test
    b = Board(CustomPlayer(3), HumanPlayer(), 5, 5)
    b.__board_state__ = [["X", "X", "X", "X", "X"], ["X", " ", "X", " ", "X"],
                         ["X", " ", " ", "Q1", "X"],
                         ["X", " ", " ", "Q2", "X"], ["X", "X", "X", "X", "X"]]
    b.__last_queen_move__[b.__queen_1__] = (2, 3, False)
    b.__last_queen_move__[b.__queen_2__] = (3, 3, False)
    b.move_count = 2
    winner, move_history, termination = b.play_isolation(time_limit=1000000,
                                                         print_moves=True)
    print winner
    print move_history
    print termination
    print("End alphabeta test")
Ejemplo n.º 4
0
def test_no_best_move():
    ### TODO: There is no best move available for the CustomPlayer. Maybe try to implement a next best move
    # Below is the setup that causes the AI to be unable to select a function.
    # Using the normal eval function and minimax algorithm
    b = Board(CustomPlayer(6), HumanPlayer(), 3, 3)
    b.__board_state__ = [["Q1", " ", " "], [" ", " ", "Q2"], [" ", " ", " "]]
    b.__last_queen_move__[b.__queen_1__] = (0, 0, False)
    b.__last_queen_move__[b.__queen_2__] = (1, 2, False)
    b.move_count = 2
    winner, move_history, termination = b.play_isolation(time_limit=1000,
                                                         print_moves=True)
    print winner
    print move_history
    print termination
    print("End no_best_move test")
Ejemplo n.º 5
0
def compare_minimax_alphabeta():
	c = CustomPlayer(4)
	c.search_fn = c.minimax
	h = HumanPlayer()
	b = Board(c, h, 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
	winner, move_history, termination = b.play_isolation(time_limit=100000, print_moves=True)
Ejemplo n.º 6
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()
    game = Board(h,r)
    game.play_isolation()


# In[ ]:

"""Example test you can run
to make sure your basic evaluation
function works."""
from isolation import Board

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 = 16 moves
    sample_board.__board_state__ = [
                [0,2,0,0,0],
                [0,0,0,0,0],
                [0,0,1,0,0],
                [0,0,0,0,0],
                [0,0,0,0,0]]
    sample_board.__last_player_move__ = [(2,2),(0,1)]

    # player 1 should have 16 moves available,
    # so board gets a score of 16
    h = OpenMoveEvalFn()
    print('This board has a score of %s.'%(h.score(sample_board)))

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()
Ejemplo n.º 9
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()
Ejemplo n.º 11
0
"""Example test to make sure
your minimax works, using the
#my_moves evaluation function."""
from isolation import Board, game_as_text
from player_submission import CustomPlayer

if __name__ == "__main__":
    # create dummy 3x3 board
    p1 = CustomPlayer(search_depth=3)
    p2 = CustomPlayer()
    b = Board(p1, p2, 3, 3)
    b.__board_state__ = [[0, 2, 0], [0, 0, 1], [0, 0, 0]]
    b.__last_player_move__[p1] = (1, 2)
    b.__last_player_move__[p2] = (0, 1)
    b.move_count = 2

    output_b = b.copy()
    # use minimax to determine optimal move
    # sequence for player 1
    winner, move_history, termination = b.play_isolation()

    print game_as_text(winner, move_history, termination, output_b)
    # your output should look like this:
    """
    ####################
      | 2 |   |
      |   | 1 |
      |   |   |

    ####################
    ####################