Exemplo n.º 1
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)
Exemplo n.º 2
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")
Exemplo n.º 3
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")
Exemplo n.º 4
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)
Exemplo n.º 5
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))
Exemplo n.º 6
0
        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")

    # Adding winner_text
    winner_text = "Player 1" if winner == board.__player_1__ else "Player 2"
    #ans.write("Winner: " + str(winner) + "\n")
    ans.write("Winner: " + str(winner_text) + "\n")

    return ans.getvalue()


if __name__ == '__main__':

    print("Starting game:")

    from player_submission import CustomPlayer
    from test_players import HumanPlayer

    board = Board(CustomPlayer(), HumanPlayer())
    winner, move_history, termination = board.play_isolation()

    print game_as_text(winner, move_history, termination)
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()
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."""
    # 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()

    print ""
    ai = CustomPlayer()
    h = HumanPlayer()
    game = Board(h, ai, 7, 7)
    winner, move_history, termination = game.play_isolation(time_limit=1000,
                                                            print_moves=True)
    print "\n", winner, " has won. Reason: ", termination