Package model

Class GameModel

java.lang.Object
model.GameModel

public class GameModel extends Object
The underlying state representation of a PacMann game, including the game graph and actors.
  • Nested Class Summary

    Nested Classes
    Modifier and Type
    Class
    Description
    static enum 
    During its lifetime, the game transitions from READY -> PLAYING -> either VICTORY/DEFEAT
    static enum 
    Each path tile contains a dot, contains a pellet, or is empty.
    private static class 
    Indicates that a collision between PacMann and a CHASING ghost was detected, meaning that the current round should end.
    private static final record 
    The position and velocity of an actor on an associated _undirected_ edge.
    private static final class 
    An unordered pair of values of type `T`.
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    private final List<Actor>
    The actors in this game, PacMann will be in index 0 and the ghosts will be in indices 1-4
    private final MazeGraph
    The graph representation of the game's maze
    private final int
    The number of cells tall the maze is
    A map from the vertices in the game graph to items positioned on these vertices.
    private final GameMap
    The map associated with this model
    private int
    The number of ghosts that were caught during the current FLEE cycle
    private int
    The number of lives remaining
    Helper object for managing property change notifications.
    private int
    The current score
    The current state of this model
    private double
    The amount of time that has elapsed
    private final int
    The number of cells across the maze is
  • Constructor Summary

    Constructors
    Constructor
    Description
    GameModel(GameMap map, Randomness randomness, boolean withAI)
    Construct a new game model using the given arrays of tile types and elevations
  • Method Summary

    Modifier and Type
    Method
    Description
    Return the actors associated with this game instance
    void
    Register `listener` to be notified whenever the property named `propertyName` of this model is changed.
    private void
    addToScore(int points)
    Increment the current score by `points` points and notify "score" observers.
    Return a reference to this game instance's Blinky Actor object
    Return a reference to this game instance's Clyde Actor object
    private void
    Handle a PacMann-Ghost collision.
    private void
    Handle the end of the round (i.e., when PacMann has been caught).
    Return a reference to this game's graph
    int
    Return the height of the maze in this game instance
    Return a reference to this game instance's Inky Actor object
    Return the item located at the given Vertex `v`, possibly NONE.
    map()
    Return the GameMap that was used to construct this model
    private void
    Give any actor currently standing on a vertex an opportunity to decide which edge to traverse next.
    static GameModel
    newGame(int width, int height, boolean withAI, Randomness randomness)
    Static method to construct a GameModel object associated with a new random maze
    private double
    Return the earliest timestep at which two actors will collide, given their current trajectories.
    private double
    nextDt(double maxDt)
    Return the largest timestep that the engine can propagate the actors by, up to `maxDt`.
    int
    Return the number of remaining lives
    Return a reference to this game instance's PacMann Actor object
    Return a reference to this game instance's Pinky Actor object
    private void
    Adds all dots and pellets to `items` during the construction of this game.
    private void
    Handle a collision detected between two actors.
    void
    Update the model to reflect PacMann's arrival at a vertex.
    void
    Stop notifying `listener` of changes to the property named `propertyName` for this model (assuming it was added no more than once).
    void
    Resets all ghosts to their WAIT state and PacMann to his starting position, then transitions to the READY state.
    int
    Return the current score
    private void
    Transition to `newState` and notify observers.
    private void
    Initiate the FLEE sequence by resetting the number of caught ghosts and transitioning all non-WAITING ghosts to their FLEE state.
    Return the current state of this game.
    double
    Return the amount of elapsed time
    void
    updateActors(double totalDt)
    Propagate the game forward in time by `totalDt` ms.
    void
    Decrease the number of remaining lives by one, notifying "lives" observers.
    private void
    Handle a victorious end of the game (i.e., when PacMann has eaten all of the dots and pellets).
    int
    Return the width of the maze in this game instance

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Field Details

    • state

      private GameModel.GameState state
      The current state of this model
    • map

      private final GameMap map
      The map associated with this model
    • items

      private final Map<MazeGraph.MazeVertex,GameModel.Item> items
      A map from the vertices in the game graph to items positioned on these vertices. `item` only includes the vertices containing DOTs and PELLETs, not NONE.
    • graph

      private final MazeGraph graph
      The graph representation of the game's maze
    • score

      private int score
      The current score
    • time

      private double time
      The amount of time that has elapsed
    • width

      private final int width
      The number of cells across the maze is
    • height

      private final int height
      The number of cells tall the maze is
    • actors

      private final List<Actor> actors
      The actors in this game, PacMann will be in index 0 and the ghosts will be in indices 1-4
    • numGhostsCaught

      private int numGhostsCaught
      The number of ghosts that were caught during the current FLEE cycle
    • numLives

      private int numLives
      The number of lives remaining
    • propSupport

      protected SwingPropertyChangeSupport propSupport
      Helper object for managing property change notifications.
  • Constructor Details

    • GameModel

      public GameModel(GameMap map, Randomness randomness, boolean withAI)
      Construct a new game model using the given arrays of tile types and elevations
  • Method Details

    • newGame

      public static GameModel newGame(int width, int height, boolean withAI, Randomness randomness)
      Static method to construct a GameModel object associated with a new random maze
    • placeDotsAndPellets

      private void placeDotsAndPellets()
      Adds all dots and pellets to `items` during the construction of this game.
    • map

      public GameMap map()
      Return the GameMap that was used to construct this model
    • width

      public int width()
      Return the width of the maze in this game instance
    • height

      public int height()
      Return the height of the maze in this game instance
    • graph

      public MazeGraph graph()
      Return a reference to this game's graph
    • actors

      public Iterable<Actor> actors()
      Return the actors associated with this game instance
    • pacMann

      public PacMann pacMann()
      Return a reference to this game instance's PacMann Actor object
    • blinky

      public Ghost blinky()
      Return a reference to this game instance's Blinky Actor object
    • pinky

      public Ghost pinky()
      Return a reference to this game instance's Pinky Actor object
    • inky

      public Ghost inky()
      Return a reference to this game instance's Inky Actor object
    • clyde

      public Ghost clyde()
      Return a reference to this game instance's Clyde Actor object
    • score

      public int score()
      Return the current score
    • time

      public double time()
      Return the amount of elapsed time
    • itemAt

      Return the item located at the given Vertex `v`, possibly NONE. This method will never return null.
    • state

      public GameModel.GameState state()
      Return the current state of this game.
    • numLives

      public int numLives()
      Return the number of remaining lives
    • addToScore

      private void addToScore(int points)
      Increment the current score by `points` points and notify "score" observers.
    • startFlee

      private void startFlee()
      Initiate the FLEE sequence by resetting the number of caught ghosts and transitioning all non-WAITING ghosts to their FLEE state.
    • processPacMannArrival

      public void processPacMannArrival()
      Update the model to reflect PacMann's arrival at a vertex.
    • setState

      private void setState(GameModel.GameState newState)
      Transition to `newState` and notify observers.
    • resetActors

      public void resetActors()
      Resets all ghosts to their WAIT state and PacMann to his starting position, then transitions to the READY state.
    • useLife

      public void useLife()
      Decrease the number of remaining lives by one, notifying "lives" observers.
    • defeat

      private void defeat()
      Handle the end of the round (i.e., when PacMann has been caught). Consume a life and reset if at least one life still remains. Otherwise, transition to the DEFEAT state and notify "game_result" observers.
    • victory

      private void victory()
      Handle a victorious end of the game (i.e., when PacMann has eaten all of the dots and pellets). Transition to the VICTORY state and notify "game_result" observers.
    • addPropertyChangeListener

      public void addPropertyChangeListener(String propertyName, PropertyChangeListener listener)
      Register `listener` to be notified whenever the property named `propertyName` of this model is changed.
    • removePropertyChangeListener

      public void removePropertyChangeListener(String propertyName, PropertyChangeListener listener)
      Stop notifying `listener` of changes to the property named `propertyName` for this model (assuming it was added no more than once). Does not affect listeners who were not registered with `propertyName`.
    • processCollision

      private void processCollision(Actor a1, Actor a2) throws GameModel.PacMannCaught
      Handle a collision detected between two actors.
      Throws:
      GameModel.PacMannCaught
    • collideWithGhost

      private void collideWithGhost(Ghost g) throws GameModel.PacMannCaught
      Handle a PacMann-Ghost collision. Score and respawn ghosts if they are fleeing; otherwise, throw `PacMannCaught`. Fleeing ghosts earn points exponential in the number of ghosts caught since the last pellet was consumed.
      Throws:
      GameModel.PacMannCaught
    • updateActors

      public void updateActors(double totalDt)
      Propagate the game forward in time by `totalDt` ms. Process all actor collisions and vertex visitations. Update actors' traversed edges upon reaching a vertex. Handle round-end and game-end conditions, notifying observers. Notify "board_state" observers after propagation has concluded.
    • nextDt

      private double nextDt(double maxDt)
      Return the largest timestep that the engine can propagate the actors by, up to `maxDt`. Timestep is constrained by actor vertex arrivals, actor state changes, and actor collisions. A minimum timestep is imposed to ensure forward progress.
    • nextCollisionTime

      private double nextCollisionTime()
      Return the earliest timestep at which two actors will collide, given their current trajectories. Actors may cross each other along an edge or may meet at a vertex. Returns POSITIVE_INFINITY if no actors will collide along their current edge trajectories.