Example #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)
Example #2
0
def rungame(args):
    from gamectrl import BrowserRemoteControl, Fast2048Control, Keyboard2048Control, Hybrid2048Control

    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 = Hybrid2048Control(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))
Example #3
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)