-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGame.java
More file actions
127 lines (102 loc) · 3.83 KB
/
Copy pathGame.java
File metadata and controls
127 lines (102 loc) · 3.83 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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
/*
Description:
Abstract class representing a generic game. This class stores the teams participating in the game,
the winner, the game name, and a history of previously played games. It provides basic methods for
accessing and modifying these fields, as well as abstract methods that must be implemented by
subclasses to define game-specific logic.
Fields:
- teams: An array of teams participating in the game.
- winner: The team that won the game.
- name: The name of the game.
- gameHistory: A list of past game history records.
- gameID: the ID of the current game
Constructors:
- Game(): Initializes a game with default teams (2 teams, each with 1 player).
- Game(String name): Initializes a game with the specified name and default teams.
- Game(String name, String gameID): initializes a game with the specified name and game ID (used in Super)
Abstract Methods:
- playGame(): Must be implemented to define the logic for playing the game.
- isWinner(): Must be implemented to determine if a winner has been decided.
- exitGame(): Must be implemented to handle the actions required when the game is exited.
Other Important Methods:
- getTeamByName(String name): Returns the team with the specified name.
- setGameHistory(List<GameHistory> history): Sets the game history.
- addGameHistory(GameHistory g): Adds a game history record.
*/
import java.util.ArrayList;
import java.util.List;
public abstract class Game {
// every game has teams, a winner, a name, and a list of previously played games
private Team[] teams;
private Team winner;
private String name;
private List<GameHistory> gameHistory;
private String gameID;
// CONSTRUCTORS
public Game() {
this.teams = new Team[] { new Team(new Player(), 0), new Team(new Player(), 1) }; // default of 2 teams with 1 player each
this.winner = null;
this.name = null;
this.gameHistory = new ArrayList<>();
this.gameID = null;
}
public Game(String name) {
this.teams = new Team[] { new Team(new Player(), 0), new Team(new Player(), 1) }; // default of 2 teams with 1 player each
this.winner = null;
this.name = name;
this.gameHistory = new ArrayList<>();
this.gameID = null;
}
public Game(String name, String gameID) {
this.teams = new Team[] { new Team(new Player(), 0), new Team(new Player(), 1) }; // default of 2 teams with 1 player each
this.winner = null;
this.name = name;
this.gameHistory = new ArrayList<>();
this.gameID = gameID;
}
// GETTER METHODS
public Team[] getTeams() {
return this.teams;
}
public Team getWinner() {
return this.winner;
}
public String getName() {
return this.name;
}
public List<GameHistory> getGameHistory() {
return this.gameHistory;
}
public String getGameID() {
return this.gameID;
}
public Team getTeamByName(String name) {
for (Team team : this.teams) {
if (team.getName().equals(name)) return team;
}
return null; // team with inputted name was not found
}
// SETTER METHODS
public void setTeams(Team[] teams) {
this.teams = teams;
}
public void setWinner(Team winner) {
this.winner = winner;
}
public void setName(String name) {
this.name = name;
}
public void setGameHistory(List<GameHistory> history) {
this.gameHistory = history;
}
public void setGameID(String id) {
this.gameID = id;
}
public void addGameHistory(GameHistory g) {
this.gameHistory.add(g);
}
// ABSTRACT METHODS
abstract void playGame();
abstract boolean isWinner();
abstract void exitGame();
}