Esempio n. 1
0
def main(argv):
    args = parse_args(argv)

    if args.browser == 'firefox':
        from ffctrl import FirefoxDebuggerControl
        if args.port is None:
            args.port = 32000
        ctrl = FirefoxDebuggerControl(args.port)
    elif args.browser == 'firefox-rc':
        from ffctrl import FirefoxRemoteControl
        if args.port is None:
            args.port = 32000
        ctrl = FirefoxRemoteControl(args.port)
    elif args.browser == 'chrome':
        from chromectrl import ChromeDebuggerControl
        if args.port is None:
            args.port = 9222
        ctrl = ChromeDebuggerControl(args.port)

    if args.ctrlmode == 'keyboard':
        from gamectrl import Keyboard2048Control
        gamectrl = Keyboard2048Control(ctrl)
    elif args.ctrlmode == 'fast':
        from gamectrl import Fast2048Control
        gamectrl = Fast2048Control(ctrl)
    elif args.ctrlmode == 'hybrid':
        from gamectrl import Hybrid2048Control
        gamectrl = Hybrid2048Control(ctrl)

    if gamectrl.get_status() == 'ended':
        gamectrl.restart_game()

    play_game(gamectrl)
Esempio n. 2
0
def main(argv):
    args = parse_args(argv)

    if args.browser == 'firefox':
        from ffctrl import FirefoxRemoteControl
        if args.port is None:
            args.port = 32000
        ctrl = FirefoxRemoteControl(args.port)
    elif args.browser == 'chrome':
        from chromectrl import ChromeDebuggerControl
        if args.port is None:
            args.port = 9222
        ctrl = ChromeDebuggerControl(args.port)

    # if args.ctrlmode == 'keyboard':
    #     from gamectrl import Keyboard2048Control
    #     gamectrl = Keyboard2048Control(ctrl)
    # elif args.ctrlmode == 'fast':
    #     from gamectrl import Fast2048Control
    #     gamectrl = Fast2048Control(ctrl)
    # elif args.ctrlmode == 'hybrid':
    #     from gamectrl import Hybrid2048Control
    #     gamectrl = Hybrid2048Control(ctrl)
    from gamectrl import Fast2048Control
    gamectrl = Fast2048Control(ctrl)

    if gamectrl.get_status() == 'ended':
        gamectrl.restart_game()

    score_list = []
    high_score = 0
    sum_tile = 0
    sum_score = 0
    iterations = 10
    for i in range(iterations):
        board, game = play_game(gamectrl)
        score_list.append(game)
        with open("score.txt", "a") as myfile:
            myfile.write("Score %d \n" % game.final_score)
            myfile.write("Tile %d \n" % game.maxval)
            myfile.write("Board:\n")
            myfile.write(str(board))
            myfile.write("\n --------------\n")
            myfile.close()
        if high_score < game.final_score:
            high_score = game.final_score
        time.sleep(0.3)
        gamectrl.restart_game()
    for score in score_list:
        sum_tile = sum_tile + score.maxval
        sum_score = sum_score + score.final_score
        print("Score %d; highest tile %d." % (score.final_score, score.maxval))
    print("Final score %d" % high_score)
    print("Avg score %d" % (sum_score / iterations))
    print("Avg Tile %d" % (sum_tile / iterations))
Esempio n. 3
0
def rungame(args):
    from gamectrl import BrowserRemoteControl, Fast2048Control, Keyboard2048Control

    if len(args) == 1:
        port = int(args[0])
    else:
        port = 32000

    ctrl = BrowserRemoteControl(port)
    # Use Keyboard2048Control if Fast2048Control doesn't seem to be working.
    gamectrl = Fast2048Control(ctrl)
    # gamectrl = Keyboard2048Control(ctrl)

    if gamectrl.get_status() == 'ended':
        gamectrl.restart_game()

    moveno = 0
    start = time.time()
    while 1:
        state = gamectrl.get_status()
        if state == 'ended':
            break
        elif state == 'won':
            time.sleep(0.75)
            gamectrl.continue_game()

        moveno += 1
        board = gamectrl.get_board()
        move = find_best_move(board)
        if move < 0:
            break
        print("%010.6f: Score %d, Move %d: %s" % (time.time() - start, gamectrl.get_score(), moveno, movename(move)))
        gamectrl.execute_move(move)

    score = gamectrl.get_score()
    board = gamectrl.get_board()
    maxval = max(max(row) for row in to_val(board))
    print("Game over. Final score %d; highest tile %d." % (score, maxval))
Esempio n. 4
0
RANK_NUMBER = { 0:0, 1:1, 2:2, 3:3, 6:4, 12: 5, 24: 6, 48: 7,
                96: 8, 192:9, 384: 10, 768:11, 1536:12, 3072:13, 6144:14, 12288:15}
rank_converter = np.vectorize(lambda x: RANK_NUMBER[x])

def play_with_chrome(game_ctrl):
    import time
    while True:
        lost = game_ctrl.get_status()
        if lost:
            break
        else:
            # Wait for animation
            time.sleep(0.1)
            board, deck, tileset = game_ctrl.get_board()
            rank_board = rank_converter(board)
            best_move = find_best_move(rank_board, deck, tileset)
            if best_move < 0:
                break
            else:
                game_ctrl.execute_move(best_move)

    # print out the result

if __name__ == '__main__':
    # play_with_search()
    from chromectrl import ChromeDebuggerControl
    from gamectrl import Fast2048Control
    chrome_ctrl = ChromeDebuggerControl(9222)
    game_ctrl = Fast2048Control(chrome_ctrl)
    play_with_chrome(game_ctrl)
Esempio n. 5
0
def main(argv):
    args = parse_args(argv)

    if args.browser == 'firefox':
        from ffctrl import FirefoxRemoteControl
        if args.port is None:
            args.port = 32000
        ctrl = FirefoxRemoteControl(args.port)
    elif args.browser == 'chrome':
        from chromectrl import ChromeDebuggerControl
        if args.port is None:
            args.port = 9222
        ctrl = ChromeDebuggerControl(args.port)

    if args.ctrlmode == 'keyboard':
        from gamectrl import Keyboard2048Control
        gamectrl = Keyboard2048Control(ctrl)
    elif args.ctrlmode == 'fast':
        from gamectrl import Fast2048Control
        gamectrl = Fast2048Control(ctrl)
    elif args.ctrlmode == 'hybrid':
        from gamectrl import Hybrid2048Control
        gamectrl = Hybrid2048Control(ctrl)

    if gamectrl.get_status() == 'ended':
        gamectrl.restart_game()

    '''========================================================================================
        Start playing 2048
    ========================================================================================'''

    for i in range(games_to_be_played):
        if log_each_game_as_csv:
            '''Log Each Game'''
            file_name = 'log/' + timestamp() + '_' + current_ai.__name__ +'.csv'
            with open(file_name, 'w', newline='') as csv_file:
                file_writer = csv.writer(csv_file)
                current_ai.print_csv_header(file_writer)

                '''Initialize global variables for current_ai'''
                initialize_current_ai_globals(file_writer)

                '''Play full game and reset'''
                play_game(gamectrl)
                gamectrl.restart_game()

        else:
            '''Initialize global variables for current_ai'''
            initialize_current_ai_globals(None)

            '''Play full game and reset'''
            play_game(gamectrl)
            gamectrl.restart_game()

    '''Log Totals'''
    if log_totals_as_csv:
        file_name = 'log/' + timestamp() + '_' + current_ai.__name__ + '_totals.csv'
        with open(file_name, 'w', newline='') as csv_file2:
            csv_writer2 = csv.writer(csv_file2)
            for line in games_played:
                csv_writer2.writerow(line)