Esempio n. 1
0
def main():
    # Parse command-line options into a namespace for use throughout this
    # program
    options = get_options()

    # Create a star-log for controlling the format of output from within this
    # program
    out = StarLog(level=options.verbosity, ansi=options.use_colour)
    out.comment("all messages printed by the client after this begin with a *")
    out.comment("(any other lines of output must be from your Player class).")
    out.comment()

    try:
        # Import player classes
        player = PlayerWrapper("your player",
                               options.player_loc,
                               logfn=out.comment)

        # Even though we're not limiting space, the display
        # may still be useful for some users
        set_space_line()

        # Play the game, catching any errors and displaying them to the
        # user:
        result = connect_and_play(player,
                                  options.name,
                                  options.channel,
                                  options.host,
                                  options.port,
                                  logfilename=options.logfile,
                                  out_function=out.comment,
                                  print_state=(options.verbosity > 1),
                                  use_debugboard=(options.verbosity > 2),
                                  use_colour=options.use_colour,
                                  use_unicode=options.use_unicode)

        out.comment("game over!", depth=-1)
        out.print(result)

    except KeyboardInterrupt:
        print()  # (end the line)
        out.comment("bye!")
    except ConnectingException as e:
        out.print("error connecting to server")
        out.comment(e)
    except DisconnectException as e:
        out.print("connection lost", depth=-1)
        out.comment(e)
    except ProtocolException as e:
        out.print("protocol error!", depth=-1)
        out.comment(e)
    except ServerEncounteredError as e:
        out.print("server encountered error!", depth=-1)
        out.comment(e)
Esempio n. 2
0
def main():
    # Parse command-line options into a namespace for use throughout this
    # program
    options = get_options()

    # Create a star-log for controlling the format of output from within this
    # program
    out = StarLog(level=options.verbosity, star="*")
    out.comment(
        "all messages printed by the referee after this begin with a *")
    out.comment(
        "(any other lines of output must be from your Player classes).")
    out.comment()

    try:
        # Import player classes
        p_R = PlayerWrapper('red player', options.playerR_loc, options, out)
        p_G = PlayerWrapper('green player', options.playerG_loc, options, out)
        p_B = PlayerWrapper('blue player', options.playerB_loc, options, out)

        # We'll start measuring space usage from now, after all
        # library imports should be finished:
        set_space_line()

        # open('simulation.tsv', 'w').close()

        # with open('simulation.tsv', 'wt') as out_file:
        #     tsv_writer = csv.writer(out_file, delimiter='\t')

        #     for i in range(-3,4):
        #         for j in range(-3,4):
        #             if i+j <4 and i+j>-4:
        #                 tsv_writer.writerow([(i,j),0])

        # Play the game!
        open('result2.txt', 'w').close()
        n = 10
        for i in range(0, n):
            play([p_R, p_G, p_B], options, out)

    # In case the game ends in an abnormal way, print a clean error
    # message for the user (rather than a trace).
    except KeyboardInterrupt:
        print()  # (end the line)
        out.comment("bye!")
    except IllegalActionException as e:
        out.section("game error")
        out.print("error: invalid action!")
        out.comment(e)
    except ResourceLimitException as e:
        out.section("game error")
        out.print("error: resource limit exceeded!")
        out.comment(e)
def main():
    # Parse command-line options into a namespace for use throughout this
    # program
    options = get_options()

    # Create a star-log for controlling the format of output from within this
    # program
    out = StarLog(options.verbosity)
    out.comment("all messages printed by the client after this begin with a *")
    out.comment("(any other lines of output must be from your Player class).")
    out.comment()

    try:
        # Import player classes
        player = PlayerWrapper("your player", options.player_loc, options, out)

        # We'll start measuring space usage from now, after all
        # library imports should be finished:
        set_space_line()

        # Play the game, catching any errors and displaying them to the
        # user:
        connect_and_play(player, options, out)

    except KeyboardInterrupt:
        print()  # (end the line)
        out.comment("bye!")
    except ConnectingException as e:
        out.print("error connecting to server")
        out.comment(e)
    except DisconnectException:
        out.print("connection lost")
    except ProtocolException as e:
        out.print("protocol error!")
        out.comment(e)
Esempio n. 4
0
def main():
    # Parse command-line options into a namespace for use throughout this
    # program
    options = get_options()

    # Create a star-log for controlling the format of output from within this
    # program
    out = StarLog(level=options.verbosity, star="*")
    out.comment(
        "all messages printed by the referee after this begin with a *")
    out.comment(
        "(any other lines of output must be from your Player classes).")
    out.comment()

    try:
        # Import player classes
        p_R = PlayerWrapper('red player', options.playerR_loc, options, out)
        p_G = PlayerWrapper('green player', options.playerG_loc, options, out)
        p_B = PlayerWrapper('blue player', options.playerB_loc, options, out)

        # We'll start measuring space usage from now, after all
        # library imports should be finished:
        set_space_line()

        # Play the game!
        play([p_R, p_G, p_B], options, out)

    # In case the game ends in an abnormal way, print a clean error
    # message for the user (rather than a trace).
    except KeyboardInterrupt:
        print()  # (end the line)
        out.comment("bye!")
    except IllegalActionException as e:
        out.section("game error")
        out.print("error: invalid action!")
        out.comment(e)
    except ResourceLimitException as e:
        out.section("game error")
        out.print("error: resource limit exceeded!")
        out.comment(e)
Esempio n. 5
0
def main():
    # Parse command-line options into a namespace for use throughout this
    # program
    options = get_options()

    # Create a star-log for controlling the format of output from within this
    # program
    out = StarLog(level=options.verbosity, ansi=options.use_colour)
    out.comment(
        "all messages printed by the referee after this begin with a *")
    out.comment(
        "(any other lines of output must be from your Player classes).")
    out.comment()

    try:
        # Import player classes
        p1 = PlayerWrapper('player 1',
                           options.player1_loc,
                           time_limit=options.time,
                           space_limit=options.space,
                           logfn=out.comment)
        p2 = PlayerWrapper('player 2',
                           options.player2_loc,
                           time_limit=options.time,
                           space_limit=options.space,
                           logfn=out.comment)

        # We'll start measuring space usage from now, after all
        # library imports should be finished:
        set_space_line()

        # Play the game!
        result = play([p1, p2],
                      delay=options.delay,
                      logfilename=options.logfile,
                      out_function=out.comment,
                      print_state=(options.verbosity > 1),
                      use_debugboard=(options.verbosity > 2),
                      use_colour=options.use_colour,
                      use_unicode=options.use_unicode)
        # Display the final result of the game to the user.
        out.comment("game over!", depth=-1)
        out.print(result)

    # In case the game ends in an abnormal way, print a clean error
    # message for the user (rather than a trace).
    except KeyboardInterrupt:
        print()  # (end the line)
        out.comment("bye!")
    except IllegalActionException as e:
        out.comment("game error!", depth=-1)
        out.print("error: invalid action!")
        out.comment(e)
    except ResourceLimitException as e:
        out.comment("game error!", depth=-1)
        out.print("error: resource limit exceeded!")
        out.comment(e)
Esempio n. 6
0
def run_game(weights, random_board=False):
    # Code copied from __main__() in referee.py
    # Modified to allow for training
    algorithm = Algorithm()
    options = get_options()
    real_reward = {"r": 0, "g": 0, "b": 0}

    # Create a star-log for controlling the format of output from within this
    # program
    out = StarLog(level=options.verbosity, star="*")
    out.comment(
        "all messages printed by the referee after this begin with a *")
    out.comment(
        "(any other lines of output must be from your Player classes).")
    out.comment()

    try:
        # Import player classes
        p_R = PlayerWrapper('red player', options.playerR_loc, options, out)
        p_G = PlayerWrapper('green player', options.playerG_loc, options, out)
        p_B = PlayerWrapper('blue player', options.playerB_loc, options, out)

        # Play the game!
        players = [p_R, p_G, p_B]

        board = None
        if random_board:
            board = Board.get_random_board()
        play(players, options, out, training=True, random_board=board)

        exits = p_R.player.board.exits
        draw = all(exits.values()) < 4

        if not draw:
            items = exits.items()
            winner = max(items, key=lambda score: items[1])[0]
            print("# DEBUG WINNER", winner)
            for color in "rgb":
                real_reward[color] = -1
            real_reward[winner] = 1
        else:
            for color in "rgb":
                real_reward[color] = 0

        # game finished, now we update weights
        assert (len(players) > 0)
        for wrapper in players:
            features = wrapper.player.features
            rewards = wrapper.player.rewards
            weights = algorithm.weight_update(
                weights, features, rewards,
                real_reward[wrapper.player.color[0]])

        return weights

    # In case the game ends in an abnormal way, print a clean error
    # message for the user (rather than a trace).
    except KeyboardInterrupt:
        print()  # (end the line)
        out.comment("bye!")
    except IllegalActionException as e:
        out.section("game error")
        out.print("error: invalid action!")
        out.comment(e)