Class Level

java.lang.Object
edu.uoc.pacman.model.Level

public class Level extends Object
Represents a level in the game.
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    private String
    The name of the level configuration file.
    private List<Ghost>
    Ghosts that are part of the level.
    private int
    The height of the map/labyrinth.
    private int
    Number of lives in the level.
    private List<MapItem>
    Stores the items which are part of the map/labyrinth.
    private static final int
    The minimum height that the map must have.
    private static final int
    The minimum width that the map must have.
    private Pacman
    Reference to Pacman object, i.e. the player.
    private int
    Level's score.
    private int
    Number that allows us to manage when the actions happen.
    private static final int
    It is the refresh time.
    private int
    The width of the map/labyrinth.
  • Constructor Summary

    Constructors
    Constructor
    Description
    Level(String filename, int lives)
    Constructor with argument.
  • Method Summary

    Modifier and Type
    Method
    Description
    void
    Adds item to the mapItemList as long as item is not null.
    void
    addPoints(int points)
    Add the value of points to the attribute score as long as points is greater than 0.
    void
    Decreases 1 the number of lives as long as the number of lives is not zero.
    Gets the first Blinky object in the Ghost list of the level.
    Getter of the attribute fileName
    private String
    This is a helper method for parse() which returns the first non-empty and non-comment line from the reader.
    Getter of the attribute ghostList.
    int
    Getter of the attribute height.
    int
    Getter of the attribute lives.
    getMapItem(int x, int y)
    Returns the map/labyrinth item which is in the position (x,y).
    getMapItem(Position position)
    Returns the map/labyrinth item which is in position.
    private List<MapItem>
    Getter of the attribute mapItemList.
    Returns an Iterator of the attribute mapItemList.
    Getter ot the attribute pacman.
    int
    Getter of the attribute score.
    int
    Getter of the attribute width.
    boolean
    Checks if the level was finished.
    void
    Increases 1 the number of lives.
    boolean
    isPathable(Position position)
    Given a position, it indicates if such a position is pathable or not.
    private void
    Parses/Reads level's data from the given file.
    It also checks which the board's requirements are met.
    void
    Removes item from the mapItemList.
    private void
    setFileName(String fileName)
    Setter of the attribute fileName.
    void
    Sets the behaviour of all the ghosts in the level to FRIGHTENED.
    private void
    setHeight(int height)
    Setter of the attribute height.
    private void
    setLives(int lives)
    Setter of the attribute lives.
    private void
    setWidth(int width)
    Setter of the attribute width.
    void
    Refreshes/Updates the game when needed.

    Methods inherited from class java.lang.Object

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

    • MIN_WIDTH

      private static final int MIN_WIDTH
      The minimum width that the map must have.
      See Also:
    • MIN_HEIGHT

      private static final int MIN_HEIGHT
      The minimum height that the map must have.
      See Also:
    • fileName

      private String fileName
      The name of the level configuration file.
    • width

      private int width
      The width of the map/labyrinth.
    • height

      private int height
      The height of the map/labyrinth.
    • mapItemList

      private List<MapItem> mapItemList
      Stores the items which are part of the map/labyrinth.
    • pacman

      private Pacman pacman
      Reference to Pacman object, i.e. the player.
    • ghostList

      private List<Ghost> ghostList
      Ghosts that are part of the level.
    • score

      private int score
      Level's score.
    • lives

      private int lives
      Number of lives in the level.
    • tick

      private int tick
      Number that allows us to manage when the actions happen. An action will be performed, when tick == UPDATE_GAME.
    • UPDATE_GAME

      private static final int UPDATE_GAME
      It is the refresh time. In other words, it is the speed of the game.
      See Also:
  • Constructor Details

    • Level

      public Level(String filename, int lives) throws LevelException
      Constructor with argument.
      Parameters:
      filename - Value for the attribute fileName.
      lives - Initial number of lives in the level.
      Throws:
      LevelException - If there are any problems while parsing the level configuration file.
  • Method Details

    • setFileName

      private void setFileName(String fileName)
      Setter of the attribute fileName.
      Parameters:
      fileName - New value for the attribute fileName.
    • getFileName

      public String getFileName()
      Getter of the attribute fileName
      Returns:
      Current value of the attribute fileName
    • getLives

      public int getLives()
      Getter of the attribute lives.
      Returns:
      The value of the attribute lives.
    • setLives

      private void setLives(int lives)
      Setter of the attribute lives.
      Parameters:
      lives - Number of lives to assign to the attribute lives.
    • increaseLives

      public void increaseLives()
      Increases 1 the number of lives.
    • decreaseLives

      public void decreaseLives()
      Decreases 1 the number of lives as long as the number of lives is not zero.
    • parse

      private void parse() throws LevelException
      Parses/Reads level's data from the given file.
      It also checks which the board's requirements are met.
      Throws:
      LevelException - When there is any error while parsing the file or some board's requirement is not satisfied.
    • getFirstNonEmptyLine

      private String getFirstNonEmptyLine(BufferedReader br) throws IOException
      This is a helper method for parse() which returns the first non-empty and non-comment line from the reader.
      Parameters:
      br - BufferedReader object to read from.
      Returns:
      First line that is a parsable line, or null there are no lines to read.
      Throws:
      IOException - if the reader fails to read a line.
    • getWidth

      public int getWidth()
      Getter of the attribute width.
      Returns:
      The current value of the attribute width.
    • setWidth

      private void setWidth(int width) throws LevelException
      Setter of the attribute width.
      Parameters:
      width - New value for the attribute width.
      Throws:
      LevelException - When width is less than MIN_WIDTH.
    • getHeight

      public int getHeight()
      Getter of the attribute height.
      Returns:
      The current value of the attribute height.
    • setHeight

      private void setHeight(int height) throws LevelException
      Setter of the attribute height.
      Parameters:
      height - New value for the attribute height.
      Throws:
      LevelException - When height is less than MIN_HEIGHT.
    • getMapItemList

      private List<MapItem> getMapItemList()
      Getter of the attribute mapItemList.
      Returns:
      The current value of the attribute mapItemList.
    • getMapItemListIterator

      public Iterator<MapItem> getMapItemListIterator()
      Returns an Iterator of the attribute mapItemList.
      Returns:
      Iterator of the attribute mapItemList.
    • getMapItem

      public MapItem getMapItem(int x, int y)
      Returns the map/labyrinth item which is in the position (x,y).
      Parameters:
      x - Value of the position in the X axis.
      y - Value of the position in the Y axis.
      Returns:
      If any, the item which is the position (x,y). Otherwise, null.
    • getMapItem

      public MapItem getMapItem(Position position) throws NullPointerException
      Returns the map/labyrinth item which is in position.
      Parameters:
      position - The position where the item that we want to get is.
      Returns:
      If any, the item which is position. Otherwise, null.
      Throws:
      NullPointerException - When position is null.
    • addMapItem

      public void addMapItem(MapItem item)
      Adds item to the mapItemList as long as item is not null.
      Parameters:
      item - Item which we want to add.
    • removeMapItem

      public void removeMapItem(MapItem item)
      Removes item from the mapItemList.
      Parameters:
      item - Item which we want to remove.
    • getGhostList

      public List<Ghost> getGhostList()
      Getter of the attribute ghostList.
      Returns:
      The current value of the attribute ghostList.
    • getScore

      public int getScore()
      Getter of the attribute score.
      Returns:
      The current value of the attribute score.
    • addPoints

      public void addPoints(int points)
      Add the value of points to the attribute score as long as points is greater than 0.
      Parameters:
      points - Number to add to the attribute score.
    • getPacman

      public Pacman getPacman()
      Getter ot the attribute pacman.
      Returns:
      The current value of the attribute pacman.
    • getBlinky

      public Blinky getBlinky()
      Gets the first Blinky object in the Ghost list of the level.
      Returns:
      The first Blinky object in the level, or null if there are not Blinky ghosts in the level.
    • isPathable

      public boolean isPathable(Position position)
      Given a position, it indicates if such a position is pathable or not.
      Parameters:
      position - Position which must be checked.
      Returns:
      true if the position is pathable. Otherwise, false.
    • setGhostsFrightened

      public void setGhostsFrightened()
      Sets the behaviour of all the ghosts in the level to FRIGHTENED.
    • hasWon

      public boolean hasWon()
      Checks if the level was finished. This happens when all the objects that are Scorable and Pickable have been picked.
      Returns:
      true if the level was finished/won. Otherwise, false.
    • update

      public void update()
      Refreshes/Updates the game when needed.