Package model
Class GameModel
java.lang.Object
model.GameModel
The underlying state representation of a PacMann game, including the game graph and actors.
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic enum
During its lifetime, the game transitions from READY -> PLAYING -> either VICTORY/DEFEATstatic 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
FieldsModifier and TypeFieldDescriptionThe actors in this game, PacMann will be in index 0 and the ghosts will be in indices 1-4private final MazeGraph
The graph representation of the game's mazeprivate final int
The number of cells tall the maze isprivate final Map
<MazeGraph.MazeVertex, GameModel.Item> A map from the vertices in the game graph to items positioned on these vertices.private final GameMap
The map associated with this modelprivate int
The number of ghosts that were caught during the current FLEE cycleprivate int
The number of lives remainingprotected SwingPropertyChangeSupport
Helper object for managing property change notifications.private int
The current scoreprivate GameModel.GameState
The current state of this modelprivate double
The amount of time that has elapsedprivate final int
The number of cells across the maze is -
Constructor Summary
ConstructorsConstructorDescriptionGameModel
(GameMap map, Randomness randomness, boolean withAI) Construct a new game model using the given arrays of tile types and elevations -
Method Summary
Modifier and TypeMethodDescriptionactors()
Return the actors associated with this game instancevoid
addPropertyChangeListener
(String propertyName, PropertyChangeListener listener) 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.blinky()
Return a reference to this game instance's Blinky Actor objectclyde()
Return a reference to this game instance's Clyde Actor objectprivate void
Handle a PacMann-Ghost collision.private void
defeat()
Handle the end of the round (i.e., when PacMann has been caught).graph()
Return a reference to this game's graphint
height()
Return the height of the maze in this game instanceinky()
Return a reference to this game instance's Inky Actor objectReturn the item located at the given Vertex `v`, possibly NONE.map()
Return the GameMap that was used to construct this modelprivate 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 mazeprivate 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
numLives()
Return the number of remaining livespacMann()
Return a reference to this game instance's PacMann Actor objectpinky()
Return a reference to this game instance's Pinky Actor objectprivate void
Adds all dots and pellets to `items` during the construction of this game.private void
processCollision
(Actor a1, Actor a2) Handle a collision detected between two actors.void
Update the model to reflect PacMann's arrival at a vertex.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).void
Resets all ghosts to their WAIT state and PacMann to his starting position, then transitions to the READY state.int
score()
Return the current scoreprivate void
setState
(GameModel.GameState newState) 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.state()
Return the current state of this game.double
time()
Return the amount of elapsed timevoid
updateActors
(double totalDt) Propagate the game forward in time by `totalDt` ms.void
useLife()
Decrease the number of remaining lives by one, notifying "lives" observers.private void
victory()
Handle a victorious end of the game (i.e., when PacMann has eaten all of the dots and pellets).int
width()
Return the width of the maze in this game instance
-
Field Details
-
state
The current state of this model -
map
The map associated with this model -
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
The graph representation of the game's maze -
score
private int scoreThe current score -
time
private double timeThe amount of time that has elapsed -
width
private final int widthThe number of cells across the maze is -
height
private final int heightThe number of cells tall the maze is -
actors
The actors in this game, PacMann will be in index 0 and the ghosts will be in indices 1-4 -
numGhostsCaught
private int numGhostsCaughtThe number of ghosts that were caught during the current FLEE cycle -
numLives
private int numLivesThe number of lives remaining -
propSupport
Helper object for managing property change notifications.
-
-
Constructor Details
-
GameModel
Construct a new game model using the given arrays of tile types and elevations
-
-
Method Details
-
newGame
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
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
Return a reference to this game's graph -
actors
Return the actors associated with this game instance -
pacMann
Return a reference to this game instance's PacMann Actor object -
blinky
Return a reference to this game instance's Blinky Actor object -
pinky
Return a reference to this game instance's Pinky Actor object -
inky
Return a reference to this game instance's Inky Actor object -
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
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
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
Register `listener` to be notified whenever the property named `propertyName` of this model is changed. -
removePropertyChangeListener
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
Handle a collision detected between two actors.- Throws:
GameModel.PacMannCaught
-
collideWithGhost
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.
-