Package edu.uoc.pacman.controller
Class Game
java.lang.Object
edu.uoc.pacman.controller.Game
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.
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
FieldsModifier and TypeFieldDescriptionprivate int
Number of the current level.private String
Name of the folder in which level files areprivate 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 -
Method Summary
Modifier and TypeMethodDescriptionint
Getter of the attributecurrentLevel
.private String
Getter of the attributefileFolder
.Gets the list of Ghosts which are present in the level.Gets an Iterator of theitemMapList
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
getScore()
Getter of the attribute "score" (Game score).boolean
hasLost()
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 attributecurrentLevel
.boolean
Checks if there is a new level to play and loads it.
If the game is finished, it returnsfalse
.void
reload()
Reloads the current level, i.e. load the level again.void
reset()
Resets the game so that it starts again.private void
setFileFolder
(String fileFolder) Setter of the attributefileFolder
.void
setPacmanDirection
(Direction direction) 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()
Update the game, namely the level.
-
Field Details
-
NUM_LIVES
private static final int NUM_LIVESNumber of lives which each level has when it starts.- See Also:
-
fileFolder
Name of the folder in which level files are -
currentLevel
private int currentLevelNumber of the current level. -
maxLevels
private final int maxLevelsMaximum amount of levels that the game has. -
score
private int scoreTotal score of the game, i.e. the sum of the levels' scores. -
level
Level object that contains the information of the current level.
-
-
Constructor Details
-
Game
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
Setter of the attributefileFolder
.- Parameters:
fileFolder
- Folder name where the configuration/level files are.
-
getItemMapListIterator
Gets an Iterator of theitemMapList
- Returns:
- Iterator of the
itemMapList
-
getFileFolder
Getter of the attributefileFolder
.- 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
Gests the @link Pacman} object which is present in the current level.- Returns:
- Reference to the
Pacman
object in the current level.
-
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 attributemaxLevels
+ 1.- Returns:
true
if there are no more levels and therefore the game is finished. Otherwise,false
.
-
getCurrentLevel
public int getCurrentLevel()Getter of the attributecurrentLevel
.- Returns:
- Value of the attribute
currentLevel
that indicates which level the player is playing.
-
nextLevel
Checks if there is a new level to play and loads it.
If the game is finished, it returnsfalse
. Otherwise, it returnstrue
.
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 returnsfalse
.- Throws:
LevelException
- When there is a level exception/problem loading the new level.
-
loadLevel
Loads a new level by using the value of the attributecurrentLevel
.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, otherwisefalse
.
-
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, otherwisefalse
.
-
reload
Reloads the current level, i.e. load the level again.- Throws:
LevelException
- When there is a level exception/problem.
-
setPacmanDirection
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.
-