-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGameState.java
More file actions
60 lines (51 loc) · 1.61 KB
/
GameState.java
File metadata and controls
60 lines (51 loc) · 1.61 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
/**
* I implement a singleton pattern to manage the game's state, It includes:
* A private static instance of GameState to ensure only one instance exists.
* A private constructor to prevent external instantiation.
* Static method getInstance to get the instance of the class.
* Methods to manipulate game state, like increaseScore and decreaseLives.
* Getters (and potentially setters) for state variables like score and lives.
* This class is focused on maintaining and providing access to the state of the
* game.
* It acts as a central point for other parts of the game to query and modify
* game-related data.
*
*/
public final class GameState {
private static GameState instance = null;
// Game state attributes
private int score;
private int lives;
// Private constructor to prevent instantiation
private GameState() {
score = 0;
lives = 3;
}
public static final GameState getInstance() {
if (instance == null) {
instance = new GameState();
}
return instance;
}
/**
* Increases the player's score by a specified amount.
*
* @param points The number of points to add to the current score.
*/
public final void increaseScore(int points) {
score += points;
}
/**
* Decreases the number of lives the player has by one.
* This method should be called when the player loses a life.
*/
public final void decreaseLives() {
lives--;
}
public final int getScore() {
return score;
}
public final int getLives() {
return lives;
}
}