Ejemplo n.º 1
0
    def autoplay(self):
        """ Creates a new ClientActor, and connects it with
        the Server.
        This method only returns when the ClientActor finishes.
        """
        client_actor = ClientActor(self.team_name)
        client_actor.register_team(self.team)

        if self.port is None:
            address = "%s" % self.main_actor
            connect = lambda: client_actor.connect_local(self.main_actor)
        else:
            address = "%s on %s:%s" % (self.main_actor, self.host, self.port)
            connect = lambda: client_actor.connect(self.main_actor, self.host, self.port)

        # Try 3 times to connect
        for i in range(3):
            if connect():
                break
            else:
                print "%s: No connection to %s." % (self.team_name, address),
                if i < 2:
                    print " Waiting 3 seconds. (%d/3)" % (i + 1)
                    time.sleep(3)
        else:
            print "Giving up."
            return

        try:
            while client_actor.actor_ref.is_alive:
                client_actor.actor_ref.join(1)
        except KeyboardInterrupt:
            print "%s: Client received CTRL+C. Exiting." % self.team_name
        finally:
            client_actor.actor_ref.stop()
# Notify the ServerActor that we’d like to run a game
# with our preferred layout, 4 bots and 200 rounds.
server.notify("initialize_game", [layout, 4, 200])

# Initialise a TkViewer and register it with the ServerActor.
viewer = TkViewer()
server.notify("register_viewer", [viewer])

# Our two PlayerTeams must be defined in the same Python
# process (because this is local game).
# Create their ClientActors, register the Teams, and connect
# to the ServerActor.
clientActor = ClientActor("the good ones")
clientActor.register_team(SimpleTeam(BFSPlayer(), BFSPlayer()))
clientActor.connect_local("pelita-main")

clientActor2 = ClientActor("the bad ones")
clientActor2.register_team(SimpleTeam(RandomPlayer(), RandomPlayer()))
clientActor2.connect_local("pelita-main")

# Now follows the boilerplate which is needed to run the game.
# As this uses a TkViewer, we need to give Tk the control
# over our main thread.
# Since everything else runs in threaded actors, this is not
# much of a problem. The queue needed to exchange data between
# different threads is handled by our TkViewer.
try:
    viewer.app.mainloop()
except KeyboardInterrupt:
    print "Received CTRL+C. Exiting."