-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGameMaster.java
More file actions
334 lines (315 loc) · 12 KB
/
GameMaster.java
File metadata and controls
334 lines (315 loc) · 12 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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
/**
@author Ethan Owen Taruc (236196)
@version 12/8/2023
**/
/*
I have not discussed the Java language code in my program
with anyone other than my instructor or the teaching assistants
assigned to this course.
I have not used Java language code obtained from another student,
or any other unauthorized source, either modified or unmodified.
If any Java language code or documentation used in my program
was obtained from another source, such as a textbook or website,
that has been clearly noted with a proper citation in the comments
of my program.
*/
import java.util.*;
/**
* The GameMaster class represents the game master who manages the game.
* It keeps track of players, deck, turn counter, and game progress.
*/
public class GameMaster {
private Player p1;
private Player p2;
private boolean didWin;
private int turnCounter;
private List<Card> deck;
/**
* Constructs a GameMaster object with two players.
*
* @param a The first player.
* @param b The second player.
*/
public GameMaster(Player a, Player b) {
p1 = a;
p2 = b;
didWin = false;
turnCounter = 1;
deck = new ArrayList<Card>();
assembleDeck();
}
/**
* The assembleDeck() method is a private method.
* It is given entirely to the student.
* It must NOT be modified.
*/
private void assembleDeck() {
deck.add(new Card("Dragon", "Aquira", 174, 26));
deck.add(new Card("Ghost", "Brawn", 130, 48));
deck.add(new Card("Fairy", "Cerulea", 162, 29));
deck.add(new Card("Dragon", "Demi", 147, 28));
deck.add(new Card("Ghost", "Elba", 155, 37));
deck.add(new Card("Fairy", "Fye", 159, 42));
deck.add(new Card("Dragon", "Glyede", 129, 26));
deck.add(new Card("Ghost", "Hydran", 163, 35));
deck.add(new Card("Fairy", "Ivy", 146, 45));
deck.add(new Card("Dragon", "Jet", 170, 24));
deck.add(new Card("Ghost", "Kineti", 139, 21));
deck.add(new Card("Fairy", "Levi", 160, 43));
deck.add(new Card("Dragon", "Meadow", 134, 29));
deck.add(new Card("Ghost", "Naidem", 165, 26));
deck.add(new Card("Fairy", "Omi", 145, 21));
deck.add(new Card("Dragon", "Puddles", 170, 34));
deck.add(new Card("Ghost", "Quarrel", 151, 29));
deck.add(new Card("Fairy", "Raven", 168, 32));
deck.add(new Card("Dragon", "Surge", 128, 27));
deck.add(new Card("Ghost", "Takiru", 140, 26));
deck.add(new Card("Fairy", "Ustelia", 163, 47));
deck.add(new Card("Dragon", "Verwyn", 145, 25));
deck.add(new Card("Ghost", "Wyverin", 158, 32));
deck.add(new Card("Fairy", "Xios", 155, 27));
deck.add(new Card("Dragon", "Yora", 159, 44));
deck.add(new Card("Ghost", "Zulu", 125, 46));
}
/**
* Gets the name of the current player.
*
* @return The name of the current player.
*/
public String getPlayerNames() {
if (turnCounter % 2 != 0) {
return p1.getName();
}
return p2.getName();
}
/**
* Main method that dictates the game flow
*
* @param action either swap or attack.
* @return The result of the action.
*/
public String play(String action) {
String r = "";
Card a = deck.get(0);
Card b = deck.get(1);
if (action.equalsIgnoreCase("SWAP")) {
if (turnCounter % 2 != 0) {
r += " " + p1.getName() + " swaps out " + p1.getActiveCard().getName() + "...\n";
p1.swap();
r += " " + p1.getActiveCard().getName() + " is now active with " + p1.getActiveCard().getHealth()
+ " health.\n";
} else {
r += " " + p2.getName() + " swaps out " + p2.getActiveCard().getName() + "...\n";
p2.swap();
r += " " + p2.getActiveCard().getName() + " is now active with " + p2.getActiveCard().getHealth()
+ " health.\n";
}
} else if (action.equalsIgnoreCase("ATTACK")) {
if (turnCounter % 2 != 0) {
r += " " + p1.getName() + " attacks with " + p1.getActiveCard().getName() + ".\n";
r += dealDamage(p2.getActiveCard(), p1.getActiveCard()) + "\n";
if (p2.getActiveCard().getHealth() <= 0) {
r += " " + p2.getName() + " discards " + p2.getActiveCard().getName() + ".\n";
p2.discard();
if (deck.size() > 1) {
if ((a.getHealth() * a.getPower()) > b.getHealth() * b.getPower()) {
p2.drawCard(a);
r += " " + p2.getName() + " draws " + a.getName() + ".\n";
deck.add(b);
deck.remove(a);
deck.remove(b);
} else {
p2.drawCard(b);
r += " " + p2.getName() + " draws " + b.getName() + ".\n";
deck.add(a);
deck.remove(b);
deck.remove(a);
}
topShifter();
} else if (deck.size() == 1) {
p2.drawCard(a);
r += " " + p2.getName() + " draws " + a.getName() + ".\n";
deck.remove(a);
} else {
r += " The deck is empty.\n";
}
p1.claimToken();
r += " " + p1.getName() + " gets a token!\n";
}
} else {
r += " " + p2.getName() + " attacks with " + p2.getActiveCard().getName() + ".\n";
r += dealDamage(p1.getActiveCard(), p2.getActiveCard()) + "\n";
if (p1.getActiveCard().getHealth() <= 0) {
r += " " + p1.getName() + " discards " + p1.getActiveCard().getName() + ".\n";
p1.discard();
if (deck.size() > 1) {
if ((a.getHealth() * a.getPower()) > b.getHealth() * b.getPower()) {
p1.drawCard(a);
r += " " + p1.getName() + " draws " + a.getName() + ".\n";
deck.add(b);
deck.remove(a);
deck.remove(b);
} else {
p1.drawCard(b);
r += " " + p1.getName() + " draws " + b.getName() + ".\n";
deck.add(a);
deck.remove(b);
deck.remove(a);
}
topShifter();
} else if (deck.size() == 1) {
p1.drawCard(a);
r += " " + p1.getName() + " draws " + a.getName() + ".\n";
deck.remove(a);
} else {
r += " The deck is empty.\n";
}
p2.claimToken();
r += " " + p2.getName() + " gets a token!\n";
}
}
}
turnCounter++;
return r;
}
/**
* Gets the current player's turn.
*
* @return The current player to act.
*/
public String pTurn() {
String r = "";
if (turnCounter % 2 != 0) {
r += "It's " + p1.getName() + "'s turn.";
} else {
r += "It's " + p2.getName() + "'s turn.";
}
return r;
}
/**
* Checks if the target card has resistance against the in-play card.
*
* @param type1 The type of the target card.
* @param type2 The type of the in-play card.
* @return true if the target card has resistance, false otherwise.
*/
public boolean checkResistance(String type1, String type2) {
if ((type1 == "Dragon") && (type2 == "Ghost") ||
((type1 == "Ghost") && (type2 == "Fairy")) ||
((type1 == "Fairy") && (type2 == "Dragon"))) {
return true;
} else {
return false;
}
}
/**
* Checks if the target card has weakness against the in-play card.
*
* @param type1 The type of the target card.
* @param type2 The type of the in-play card.
* @return true if the target card has weakness, false otherwise.
*/
public boolean checkWeakness(String type1, String type2) {
if (((type1 == "Dragon") && (type2 == "Fairy")) ||
((type1 == "Fairy") && (type2 == "Ghost")) ||
((type1 == "Ghost") && (type2 == "Dragon"))) {
return true;
} else {
return false;
}
}
/**
* Shifts the top card of the deck to the bottom until a non-null card is found.
*/
private void topShifter() {
while (deck.get(0) == (null)) {
for (int i = 1; i < deck.size(); i++) {
deck.set(i - 1, deck.get(i));
}
deck.remove(deck.size() - 1);
}
}
/**
* Deals a card from the deck to the players.
*
* @return The result of dealing the card.
*/
public String dealCard() {
String result = "";
int a = 0;
while (!p1.handIsFull() || !p2.handIsFull()) {
if (turnCounter % 2 != 0) {
p1.drawCard(deck.get(a));
result += p1.getName() + " draws " + deck.get(a).getName() + ".\n";
deck.remove(a);
} else {
p2.drawCard(deck.get(a));
result += p2.getName() + " draws " + deck.get(a).getName() + ".\n";
deck.remove(a);
}
topShifter();
turnCounter++;
}
result += "\n" + p1.getName() + "'s hand is full.\n";
result += "\n" + p2.getName() + "'s hand is full.\n";
return result;
}
/**
* Deals damage to the target card based on the active card.
*
* @param target The target card to receive damage.
* @param inPlay The active card that deals damage.
* @return The report on the damage dealt
*/
public String dealDamage(Card target, Card inPlay) {
int damage = inPlay.getPower();
int finDam;
String s = "";
if (checkWeakness(target.getType(), inPlay.getType())) {
s += " " + target.getType() + " is weak to " + inPlay.getType() + ".\n";
finDam = damage * 2;
target.takeDamage(finDam);
} else if (checkResistance(target.getType(), inPlay.getType())) {
s += " " + target.getType() + " is resistant to " + inPlay.getType() + ".\n";
finDam = damage / 2;
target.takeDamage(finDam);
} else {
finDam = damage;
target.takeDamage(finDam);
}
s += " " + inPlay.getName() + " deals " + finDam + " damage on " + target.getName() + ".\n";
s += " " + target.getName() + " has " + target.getHealth() + " health left.";
return s;
}
/**
* Checks if there is a winner in the game.
*
* @return true if there is a winner, false otherwise.
*/
public boolean hasWinner() {
if (p1.getTokens() == 3 || p2.getTokens() == 3) {
didWin = true;
} else {
didWin = false;
}
return didWin;
}
/**
* Generates a game report with the winner and game summary.
*
* @return The game report.
*/
public String gameReport() {
String a = "";
if (p1.getTokens() == 3) {
a = p1.getName() + " wins!!!";
} else if (p2.getTokens() == 3) {
a = p2.getName() + " wins!!!";
}
String b = "---=== GAME SUMMARY ===---\n" +
"This game lasted " + (turnCounter - 1) + " turns.\n" +
p1.statusReport() + "\n" + p2.statusReport();
String finalReport = a + "\n\n" + b;
return finalReport;
}
}