예제 #1
0
class Game:
    """
    The core of the application.
    """
    def __init__(self, layout, agents, display_class, turn=AgentRole.Mr_X):
        self.gameState = GameState(GameStateData(layout, agents))
        self.turn = turn
        # create a new <type>+Display object
        self.display = display_class(self.gameState.data)
        self.MOD = len(agents)  # number of players

    def run(self):
        """
        The main loop of the application.
        """
        self.display.start()
        counter = 0

        while self.gameState.isEndState(self.turn) == (False, None, ""):
            # ask for an action and create the new game state
            self.display.wait()

            new_action = self.gameState.data.getAgent(counter).getAction(
                self.gameState, self.display)

            if not new_action is None:  # if there is a possible action, do it
                self.gameState = self.gameState.generateSuccessor(
                    counter, new_action)
            else:
                self.display.wait()

            # update the display
            self.display.update(self.gameState.data)

            self.display.wait()
            # update the player turn
            counter += 1
            counter = counter % self.MOD
            self.turn = AgentRole.Mr_X if counter == 0 else AgentRole.COP

        # end of the main while loop
        self.display.showMessage("GAME OVER\n\n" +
                                 self.gameState.isEndState(self.turn)[2])
        self.display.waitQuit()
        self.display.finish()