Exemple #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()
Exemple #2
0
 def __init__(self, search_depth=25, eval_fn=OpenMoveEvalFn()):
     """Initializes your player.
     
     if you find yourself with a superior eval function, update the default 
     value of `eval_fn` to `CustomEvalFn()`
     
     Args:
         search_depth (int): The depth to which your agent will search
         eval_fn (function): Utility function used by your agent
     """
     self.eval_fn = eval_fn
     self.search_depth = search_depth
     self.variable_search_depth = 0
     self.time_to_return = 100
     self.shortter_time_to_return = 70
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()
Exemple #4
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(), 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."""
    win = 0
    loss = 0
    count = 0
    for i in range(1, 51):
        print ""
        try:
            r = RandomPlayer()
            #r = CustomPlayer(search_depth=10)
            h = CustomPlayer(search_depth=12)
            game = Board(h, r, 7, 7)
            output_b = game.copy()
            winner, move_history, termination = game.play_isolation(time_limit=1000, print_moves=True)
            if winner == game.__queen_1__:
                win += 1.
            else:
                loss += 1.
            count += 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("total " + str(count) + " games", "Q1 win ratio: " + str(win/(win + loss)))
Exemple #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()
        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()
        h = CustomPlayer()
        #h = CustomPlayer()
	"""
        game = Board(r, h, 3, 3)
	game.__board_state__= [
		['-', '-', '-'],
		['-', 0, '-'],
		[0, '-', 0],
	]
        game = Board(r, h, 5, 5)
	game.__board_state__= [
		['-', '-', '-', 0, 0],
		['-', '-', '-', 0, 0],
		['-', '-', 0, '-', 0],
		['-', '-', '-', 0, 0],
		['-', '-', '-', '-', '-']
	]
        game.__last_queen_move__ = (1, 0)
        b.move_count = 1
	"""
        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()