Class Game

java.lang.Object
edu.uoc.pacman.controller.Game

public class Game extends Object
Controller class of the game. It is the middleware (or bridge) between the model and view classes.
This class is called from the view classes in order to access/modify the model data. This class controls the logic of the game.
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    private int
    Number of the current level.
    private String
    Name of the folder in which level files are
    private Level
    Level object that contains the information of the current level.
    private final int
    Maximum amount of levels that the game has.
    private static final int
    Number of lives which each level has when it starts.
    private int
    Total score of the game, i.e. the sum of the levels' scores.
  • Constructor Summary

    Constructors
    Constructor
    Description
    Game(String fileFolder)
    Constructor with argument.
  • Method Summary

    Modifier and Type
    Method
    Description
    int
    Getter of the attribute currentLevel.
    private String
    Getter of the attribute fileFolder.
    Gets the list of Ghosts which are present in the level.
    Gets an Iterator of the itemMapList
    int
    Gets the current score of the level that the player is playing.
    int
    Returns the number of lives that the player has in the current level.
    Gests the @link Pacman} object which is present in the current level.
    int
    Getter of the attribute "score" (Game score).
    boolean
    Checks if the player has lost, i.e. the number of lives is zero.
    boolean
    Indicates if the game is finished (true) or not (false).
    boolean
    Checks if the level is completed, i.e. the player has collected all the dots and energizers of the map.
    private void
    Loads a new level by using the value of the attribute currentLevel.
    boolean
    Checks if there is a new level to play and loads it.
    If the game is finished, it returns false.
    void
    Reloads the current level, i.e. load the level again.
    void
    Resets the game so that it starts again.
    private void
    setFileFolder(String fileFolder)
    Setter of the attribute fileFolder.
    void
    This method allows us to change Pacman's direction according to the user input (i.e. the pressed key).
    private void
    setScore(int score)
    Setter of the attribute "score" (Game score).
    void
    Update the game, namely the level.

    Methods inherited from class java.lang.Object

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

    • NUM_LIVES

      private static final int NUM_LIVES
      Number of lives which each level has when it starts.
      See Also:
    • fileFolder

      private String fileFolder
      Name of the folder in which level files are
    • currentLevel

      private int currentLevel
      Number of the current level.
    • maxLevels

      private final int maxLevels
      Maximum amount of levels that the game has.
    • score

      private int score
      Total score of the game, i.e. the sum of the levels' scores.
    • level

      private Level level
      Level object that contains the information of the current level.
  • Constructor Details

    • Game

      public Game(String fileFolder) throws IOException
      Constructor with argument.
      Parameters:
      fileFolder - Folder name where the configuration/level files are.
      Throws:
      IOException - When there is a problem while retrieving number of levels
  • Method Details

    • setFileFolder

      private void setFileFolder(String fileFolder)
      Setter of the attribute fileFolder.
      Parameters:
      fileFolder - Folder name where the configuration/level files are.
    • getItemMapListIterator

      public Iterator<MapItem> getItemMapListIterator()
      Gets an Iterator of the itemMapList
      Returns:
      Iterator of the itemMapList
    • getFileFolder

      private String getFileFolder()
      Getter of the attribute fileFolder.
      Returns:
      Value of the attribute fileFolder.
    • getLevelScore

      public int getLevelScore()
      Gets the current score of the level that the player is playing.
      Returns:
      Current score of the current level.
    • getScore

      public int getScore()
      Getter of the attribute "score" (Game score).
      Returns:
      Value of the attribute "score"
    • setScore

      private void setScore(int score)
      Setter of the attribute "score" (Game score).
      Parameters:
      score - New value for the attribute "score"
    • getPacman

      public Pacman getPacman()
      Gests the @link Pacman} object which is present in the current level.
      Returns:
      Reference to the Pacman object in the current level.
    • getGhosts

      public List<Ghost> getGhosts()
      Gets the list of Ghosts which are present in the level.
      Returns:
      List which contains all the ghosts in the current live.
    • getNumLives

      public int getNumLives()
      Returns the number of lives that the player has in the current level.
      Returns:
      Number of lives in the current level.
    • isFinished

      public boolean isFinished()
      Indicates if the game is finished (true) or not (false).

      The game is finished when the attribute currentLevel is equal to attribute maxLevels + 1.

      Returns:
      true if there are no more levels and therefore the game is finished. Otherwise, false.
    • getCurrentLevel

      public int getCurrentLevel()
      Getter of the attribute currentLevel.
      Returns:
      Value of the attribute currentLevel that indicates which level the player is playing.
    • nextLevel

      public boolean nextLevel() throws LevelException
      Checks if there is a new level to play and loads it.
      If the game is finished, it returns false. Otherwise, it returns true.
      The game score must be updated when a level is finished. Thus, when the player is playing the first level, game's score is zero.
      Returns:
      true if there is a next level, and it has been loaded correctly. Otherwise, it returns false.
      Throws:
      LevelException - When there is a level exception/problem loading the new level.
    • loadLevel

      private void loadLevel() throws LevelException
      Loads a new level by using the value of the attribute currentLevel.

      The pattern of the filename is: fileFolder+"level" + numberLevel + ".txt".

      Throws:
      LevelException - When there is a level exception/problem.
    • isLevelCompleted

      public boolean isLevelCompleted()
      Checks if the level is completed, i.e. the player has collected all the dots and energizers of the map.
      Returns:
      true if this level is beaten, otherwise false.
    • hasLost

      public boolean hasLost()
      Checks if the player has lost, i.e. the number of lives is zero.
      Returns:
      true if this the player has lost, otherwise false.
    • reload

      public void reload() throws LevelException
      Reloads the current level, i.e. load the level again.
      Throws:
      LevelException - When there is a level exception/problem.
    • setPacmanDirection

      public void setPacmanDirection(Direction direction)
      This method allows us to change Pacman's direction according to the user input (i.e. the pressed key).
      Parameters:
      direction - New direction to set to Pacman.
    • reset

      public void reset()
      Resets the game so that it starts again.
    • update

      public void update()
      Update the game, namely the level.