-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathGame.java
More file actions
515 lines (433 loc) · 17.1 KB
/
Game.java
File metadata and controls
515 lines (433 loc) · 17.1 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
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
/**
* Game.java
*
* This class provides methods with which to run the game. The map is predetermined.
*
* Known bugs:
* The fight goes an extra round after the enemies are defeated
* Clicking too fast can break the game (seems to be a problem with the mouse action listener
*
* @author Emma Shumadine, Lily Orth-Smith, Rachel Zhang
* */
import java.util.*;
import javax.swing.*;
import java.awt.*;
public class Game {
private Player player;
private Map myMap;
/**
* Creates a game with the default map
*/
public Game() {
player = new Player();
myMap = makeMap1();
}
/**
* Creates a game with the specified map
* @param userMap the map to use for the game
*/
public Game(Map userMap) {
player = new Player();
myMap = userMap;
}
/**
* Returns the player to the start of the map
*/
public void goToBeginning() {
myMap.goToBeginning();
}
/**
* Moves the player to an adjacent situation given by the index in the adjacency array
* @param adjIndex the index in the adjacency array
*/
public void movePlayer(int adjIndex) {
Situation[] adjacentSituations = myMap.getAdjArray();
movePlayer(adjacentSituations[adjIndex]);
}
/**
* Moves the player to an adjacent situation
* @param adjSit the situation
*/
public void movePlayer(Situation adjSit) {
//check if the player has the required items to enter the situation
if(!adjSit.getRequiredItems().isEmpty()) {
int count = 0;
for (Item i: adjSit.getRequiredItems()) {
if (player.hasItem(i)) {
count++;
}
}
if (count == adjSit.getRequiredItems().size()) {
myMap.changeSituation(adjSit);
} else {
throw new IllegalMoveException();
}
} else {
myMap.changeSituation(adjSit);
}
}
/**
* Picks up an item from the current situation and puts it in the player's inventory
* @param item the item to pick up
*/
public void pickUpItem(Item item) throws FullInventoryException{
if(!player.inventoryIsFull()){
myMap.getCurrentSituation().removeItem(item);
double hitRate = item.getHitRate();
if (hitRate == 0) {
player.pickUpItem(item);
}
else { //item is a weapon
player.pickUpItem(item);
Item drop = player.pickUpWeapon(item);
dropItem(drop);
}
}
}
/**
* Removes the item from the player's inventory and drops it into the situation
* @param item the item to drop
*/
public void dropItem(Item item) {
if (!item.equals(player.getWeapon()))
player.dropItem(item);
myMap.getCurrentSituation().addItem(item);
}
/**
* Uses an item on the player (if it is a health item) and returns a string describing the result
* @param item the item to use
* @return a string describing the result
*/
public String useItem(Item item) {
int healed = player.useHealthItem(item);
int HP = item.getHp();
if (HP != 0)
return "Used health item. Healed " + Integer.toString(healed) + " HP.";
else
return "Can't use this item.";
}
/**
* Returns true if the boss has been defeated, and false otherwise
* @return true if the boss has been defeated
*/
public boolean isOver() {
return myMap.isCleared();
}
/**
* Returns the description of the current situation
* @return the description
*/
public String getCurrentDescription() {
return myMap.getCurrentSituation().getDescription();
}
public String[] getAdjNames() {
String[] names = myMap.getAdjNames();
String[] options = new String[names.length + 1];
options[0] = "...";
for (int i = 1; i < options.length; i++) {
options[i] = names[i-1];
}
return options;
}
/**
* Returns the current health of the player as a string status message
* @return the current health of the player as a string status message
*/
public String getPlayerHealth() {
String s = "Current Meme Points: ";
s+= Integer.toString(player.getCurrentHP());
s+= "/";
s+= Integer.toString(player.getMaxHP());
return s;
}
/**
* Determines and returns the color to be associated with the player's current hp, based on the current health
* @return the color to be associated with the player's hp
*/
public Color getHealthColor() {
int b = 66;
int r = 244 - (player.getCurrentHP());
int g = 74 + (player.getCurrentHP());
System.out.println(r + " " + g + " " + b);
return new Color(r, g, b);
}
/**
* Returns a Vector containing the items in the current situation
* @return a vector containing the items in the current situation
*/
public Vector<Item> getRoomItems() {
Vector<Item> temp = myMap.getCurrentSituation().getItems();
return temp;
}
/**
* Returns the player's inventory as a vector
* @return the player's inventory as a vector
*/
public Vector<Item> getInventory() {
Vector<Item> inventory = new Vector<Item>();
Item[] temp = player.getInventory();
for (int i = 0; i < player.getLastItemIndex() +1; i++)
inventory.add(temp[i]);
return inventory;
}
/**
* Returns the player's current weapon as a string
* @return the player's current weapon as a string
*/
public String getWeapon() {
return "Current Weapon: " + player.getWeapon().toString();
}
/**
* Returns true if the current situation contains a fight
* @return true if the current situation contains a fight
*/
public boolean hasFight() {
return myMap.getCurrentSituation().hasFight();
}
/**
* Returns a vector containing the names of the enemies in the current situation's fight
* @return a vector containing the names of the enemies
*/
public Vector<String> getEnemyNames() {
Vector<String> enemyNames = new Vector<String>();
Vector<GameCharacter> enemies = myMap.getCurrentSituation().getFight().getEnemies();
for (int i = 0; i < enemies.size(); i++)
enemyNames.add(enemies.get(i).getName());
return enemyNames;
}
/**
* Returns a vector containing the enemies in the current situation's fight
* @return a vector containing the enemies
*/
public Vector<GameCharacter> getEnemies() {
return myMap.getCurrentSituation().getFight().getEnemies();
}
/**
* Returns a string describing the current state of the current situation's fight
* @return a string describing the current state of the fight
*/
public String currentFightState() {
String p = "";
p += "Player HP: " + Integer.toString(player.getCurrentHP());
Vector<GameCharacter> enemies = myMap.getCurrentSituation().getFight().getEnemies();
String e = "Enemies: [";
for(int i = 0; i < enemies.size(); i++) {
GameCharacter en = enemies.get(i);
if(enemies.indexOf(en) != enemies.size() - 1) {
e += en.getName() + ": " + en.getCurrentHP() + ", ";
} else {
e += en.getName() + ": " + en.getCurrentHP() + "]";
}
}
return p + "\n" +e;
}
/**
* Runs one round of the current situation's fight
* @param action the action to be taken ("heal" or "attack")
* @param targetIndex the index of the target in its respective data structure (-1 if it's the player)
* @return A string describing the results of the round
*/
public String fightOneRound(String action, int targetIndex){
while (!myMap.getCurrentSituation().getFight().isPlayersTurn()){
myMap.getCurrentSituation().getFight().playOneRound(action, targetIndex);
}
int result = myMap.getCurrentSituation().getFight().playOneRound(action, targetIndex);
if (action.equals("attack")) {
if (result == -1)
return "Miss!";
else
return Integer.toString(result) + " damage!";
}
else
return Integer.toString(result) + " healed!";
}
/**
* Runs the current situation's fight using user input provided by a GUI
* @param message the message to be displayed to the player
* @return a string describing the results of one round of the fight
*/
public String fight(String message) {
Object[] options = {"Attack","Heal"};
String title = "Fight!! ";
for (int i = 0; i < getEnemyNames().size()-1; i++)
title += getEnemyNames().get(i) + " and ";
title+= getEnemyNames().get(getEnemyNames().size()-1);
int choice = JOptionPane.showOptionDialog(null, message,
title, JOptionPane.YES_NO_CANCEL_OPTION, JOptionPane.QUESTION_MESSAGE, null, options, options[1]);
if (choice == 0) {
JComboBox enemyCombo = new JComboBox(getEnemyNames());
JOptionPane.showMessageDialog(null, enemyCombo, "Select the enemy to attack", JOptionPane.QUESTION_MESSAGE);
return fightOneRound("attack", enemyCombo.getSelectedIndex()) + "\n" + currentFightState();
}
else if (choice == 1) {
JComboBox healCombo = new JComboBox(getInventory());
JOptionPane.showMessageDialog(null, healCombo, "Choose an item to heal with", JOptionPane.QUESTION_MESSAGE);
return fightOneRound("heal", healCombo.getSelectedIndex()) + "\n" + currentFightState();
}
else
return "";
}
/**
* Runs the entirety of the fight using user input provided by a GUI
*/
public void startFight() {
String fightString = currentFightState();
while (!myMap.getCurrentSituation().getFight().fightEnded()){
fightString = fight(fightString);
}
if (player.getCurrentHP() == 0) {
JOptionPane.showMessageDialog (null, "Game Over.");
myMap.goToBeginning();
}
else {
JOptionPane.showMessageDialog (null, "You won the fight! Congrats.");
}
myMap.getCurrentSituation().getFight().respawn();
}
/**
* Makes the map
* @return the map
*/
public Map makeMap1() {
// beginning
Situation beginning = new Situation("First Room", "You are in a small, dark room.");
Weapon smashBall = new Weapon("Smash Ball", "Final smaaaaaaash! Hit Rate: 0.1%, 10d100 damage", 0.001, "10d100");
beginning.addItem(smashBall);
// nyan cat room
HealItem rainbowPopTart = new HealItem("A rainbow poptart", "It crosses your mind that this could be nyan cat poop, but as the rainbowy scent wafts "
+ "towards you, you put the thought out of your mind. Heals 5 meme points.", 5);
Vector<Item> dropNyan = new Vector<>();
dropNyan.add(rainbowPopTart);
dropNyan.add(rainbowPopTart.clone());
Situation nyanCats = new Situation("Nyan Cat Attack!", "Some Nyan Cats seem to have made their home here.", dropNyan);
Weapon rainbow = new Weapon("Rainbow Power", "Nyanyanyanyanyanyanya!", 0.7, "1d8");
GameCharacter nyanCat = new GameCharacter(10, rainbow, "A Nyan Cat");
Vector<GameCharacter> manyNyanCats = new Vector<GameCharacter>();
manyNyanCats.add(nyanCat);
GameCharacter nyanCat2 = nyanCat.clone();
nyanCat2.setName("Another Nyan Cat");
manyNyanCats.add(nyanCat2);
Fight nyanCatFight = new Fight(player, manyNyanCats);
nyanCats.addFight(nyanCatFight);
// apple room
Vector<Item> apples = new Vector<>();
Item apple = new HealItem("Apple", "A crunchy apple. Can be eaten. Heals 10 meme points.", 10);
apples.add(apple);
apples.add(apple.clone());
Situation forest = new Situation("A wild wood", "You find yourself in what appears to be an abandoned apple orchard. Apples are strewn on the "
+ "ground.", apples);
// sword room
Situation swordRoom = new Situation("A clearing", "You find yourself in a circular clearing. Someone seems to have left a sword here.");
Weapon sword = new Weapon("Sword", "A sharp sword. Hit Rate: 95%, 4d6 damage", 0.95, "4d6");
swordRoom.addItem(sword);
// dat boi room
Situation datBoiRoom = new Situation("The dankest of memes", "You can feel the meme energy in the air.");
Item datBoi = new Item("Dat Boi", "Here comes dat boi!");
datBoiRoom.addItem(datBoi);
// sickle room
Vector<Item> grains = new Vector<>();
Item wheat = new HealItem("Wheat", "A grain of wheat. Heals 5 meme points", 5);
grains.add(wheat);
grains.add(wheat.clone());
Situation sickleRoom = new Situation("A wheat field", "Outside of the woods lies a large wheat field. It looks like someone was in the "
+ "middle of harvesting the wheat, but only their sickle was left behind.", grains);
Weapon sickle = new Weapon("Sickle", "A sickle used for harvesting wheat. Hit Rate: 85%, 3d10 damage", 0.85, "3d10");
sickleRoom.addItem(sickle);
// the second apple room
Vector<Item> apples2 = new Vector<>();
Item apple2 = new HealItem("Apple", "A crunchy apple. Can be eaten.", 10);
apples.add(apple2);
apples.add(apple2.clone());
Situation appleStorage = new Situation("A storehouse", "The storehouse appears not to have been used for some time, but a few apples remain.",
apples2);
// doge room
Situation dogeRoom = new Situation("Shibe", "A doge lives here.");
Weapon suchDamage = new Weapon("Such Damage", "Much wow", 0.8, "3d8");
GameCharacter doge = new GameCharacter(50, suchDamage, "Doge");
Vector<GameCharacter> dogeVector = new Vector<>();
dogeVector.add(doge);
Fight dogeFight = new Fight(player, dogeVector);
dogeRoom.addFight(dogeFight);
Item dogeCoin = new Item("Dogecoin", "Much money");
dogeRoom.addItem(dogeCoin);
// mace room
Situation maceRoom = new Situation("A cottage", "A run-down cottage. Whoever lived here left behind a mace.");
Weapon mace = new Weapon("Mace", "A heavy mace. Hit Rate: 80%, 3d12 damage", 0.8, "3d12");
maceRoom.addItem(mace);
// boss room
Situation cena = new Situation("The Finale", "A wild John Cena appears!");
Weapon sass = new Weapon("Sass", "You call that a weapon?", 0.9, "4d10");
GameCharacter johnCena = new GameCharacter(150, sass, "John Cena");
Vector<GameCharacter> cenaFight = new Vector<>();
cenaFight.add(johnCena);
Fight bossFight = new Fight(player, cenaFight);
cena.addFight(bossFight);
Vector<Item> required = new Vector<>();
required.add(datBoi);
required.add(dogeCoin);
cena.setRequiredItems(required);
// create the map
Map map = new Map(beginning, cena);
map.addVertex(nyanCats);
map.addVertex(forest);
map.addVertex(swordRoom);
map.addVertex(appleStorage);
map.addVertex(dogeRoom);
map.addVertex(sickleRoom);
map.addVertex(datBoiRoom);
map.addVertex(maceRoom);
map.addEdge(beginning, nyanCats);
map.addEdge(beginning, cena);
map.addEdge(nyanCats, forest);
map.addEdge(nyanCats, swordRoom);
map.addEdge(forest, swordRoom);
map.addEdge(forest, datBoiRoom);
map.addEdge(swordRoom, sickleRoom);
map.addEdge(sickleRoom, appleStorage);
map.addEdge(swordRoom, dogeRoom);
map.addEdge(dogeRoom, maceRoom);
return map;
}
/**
* Returns a string representation of the player's current situation, health, and options
* @return a string representation of the player's current situation, health, and options
*/
public String toString() {
LinkedList<Situation> adj = myMap.getAdjacent();
String s = myMap.getCurrentSituation().toString() + "\nAdjacent rooms: ";
for(Situation sit: adj) {
if(adj.indexOf(sit) != adj.size() - 1) {
s += sit.getName() + ", ";
} else {
s += sit.getName();
}
}
s += "\nCurrent Meme Points: " + player.getCurrentHP() + "/" + player.getMaxHP() + "\nInventory: [";
for(int i = 0; i < player.getInventory().length; i++) {
if(i != player.getInventory().length - 1) {
s += player.getInventory()[i] + ", ";
} else {
s += player.getInventory()[i] + "]";
}
}
s += "\nWeapon: " + player.getWeapon();
return s;
}
public static void main(String[] args) {
Game myGame = new Game();
System.out.println(myGame);
Weapon smashBall = new Weapon("Smash Ball", "Final smaaaaaaash! Hit Rate: 1%, 10d100 damage", 0.01, "10d100");
myGame.pickUpItem(smashBall);
//Item Apple = new HealItem("Apple", "A crunchy apple. Can be eaten. Heals 10 HP.", 10);
//System.out.println(myGame.useItem(Apple));
myGame.movePlayer(1);
myGame.pickUpItem(new HealItem("A rainbow poptart", "It crosses your mind that this could be nyan cat poop, but as the rainbowy scent wafts "
+ "towards you, you put the thought out of your mind. Heals 5 meme points.", 5));
System.out.println("\n" + myGame);
myGame.movePlayer(2);
System.out.println("\n" + myGame);
Weapon sword = new Weapon("Sword", "A sharp sword. Hit Rate: 95%, 4d6 damage", 0.95, "4d6");
myGame.pickUpItem(sword);
System.out.println("\n" + myGame);
}
}