-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathGame.java
More file actions
697 lines (639 loc) · 26.1 KB
/
Game.java
File metadata and controls
697 lines (639 loc) · 26.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
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
import java.util.ArrayList;
import ansi_terminal.*;
import java.util.Scanner;
import java.io.PrintWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Random;
/** Creates the Game object a d contains logic for running it
*
*/
public class Game {
// making game a singleton
private static Game theInstance;
// creating an int field that is used to call the appropriate room
private int roomNumber;
private Room room;
// new room objects below and Position used for warp
private Room2 room2;
private Room3 room3;
private Room4 room4;
private Position warpPosit;
private Player player;
private ArrayList<Box> boxes;
private ArrayList<Enemy> enemies;
// used to save the size of boxes
private int boxSize;
// used to save the size of enemies
private int enemySize;
// warps is instantiated in to Room class
private ArrayList<Warp> warps;
// used in showHelp() to set the color randomly only once
private int justOnceHelp = 0;
private int helpForeground = 0;
// The ints are used in redrawMapAndHelp() to set the color randomly only once
private int justOnce = 0;
private int Foreground = 0;
// used to initialize the room variables once per game
private boolean roomReady = false;
private boolean room2Ready = false;
private boolean room3Ready = false;
private boolean room4Ready = false;
/** Creates a singleton for creating an instance of the game
*
* @return the instance of the game
*/
public static synchronized Game instance() {
if (theInstance == null) {
theInstance = new Game();
}
return theInstance;
}
private Game() {
room = new Room();
room2 = new Room2();
room3 = new Room3();
room4 = new Room4();
warpPosit = new Position();
player = new Player(room.getPlayerStart());
}
private void playerWon() {
Terminal.clear();
Terminal.setForeground(Color.YELLOW);
for (int i = 0; i < 40; i++) {
Terminal.warpCursor(i, 82);
System.out.print(" ");
}
Terminal.warpCursor(12, 30);
System.out.print("");
Terminal.warpCursor(13, 30);
System.out.print("██╗ ██╗ ██████╗ ██╗ ██╗ ██╗ ██╗ ██████╗ ███╗ ██╗██╗");
Terminal.warpCursor(14, 30);
System.out.print("╚██╗ ██╔╝██╔═══██╗██║ ██║ ██║ ██║██╔═══██╗████╗ ██║██║");
Terminal.warpCursor(15, 30);
System.out.print(" ╚████╔╝ ██║ ██║██║ ██║ ██║ █╗ ██║██║ ██║██╔██╗ ██║██║");
Terminal.warpCursor(16, 30);
System.out.print(" ╚██╔╝ ██║ ██║██║ ██║ ██║███╗██║██║ ██║██║╚██╗██║╚═╝");
Terminal.warpCursor(17, 30);
System.out.print(" ██║ ╚██████╔╝╚██████╔╝ ╚███╔███╔╝╚██████╔╝██║ ╚████║██╗");
Terminal.warpCursor(18, 30);
System.out.print(" ╚═╝ ╚═════╝ ╚═════╝ ╚══╝╚══╝ ╚═════╝ ╚═╝ ╚═══╝╚═╝");
Terminal.warpCursor(40, 1);
Terminal.pause(5);
}
private void gameOver() {
Terminal.clear();
Terminal.setForeground(Color.RED);
for (int i = 0; i < 40; i++) {
Terminal.warpCursor(i, 82);
System.out.print(" ");
}
Terminal.warpCursor(12, 30);
System.out.print("");
Terminal.warpCursor(13, 30);
System.out.print("██╗ ██╗ ██████╗ ██╗ ██╗ ██████╗ ██╗███████╗██████╗ ");
Terminal.warpCursor(14, 30);
System.out.print("╚██╗ ██╔╝██╔═══██╗██║ ██║ ██╔══██╗██║██╔════╝██╔══██╗");
Terminal.warpCursor(15, 30);
System.out.print(" ╚████╔╝ ██║ ██║██║ ██║ ██║ ██║██║█████╗ ██║ ██║");
Terminal.warpCursor(16, 30);
System.out.print(" ╚██╔╝ ██║ ██║██║ ██║ ██║ ██║██║██╔══╝ ██║ ██║");
Terminal.warpCursor(17, 30);
System.out.print(" ██║ ╚██████╔╝╚██████╔╝ ██████╔╝██║███████╗██████╔╝");
Terminal.warpCursor(18, 30);
System.out.print(" ╚═╝ ╚═════╝ ╚═════╝ ╚═════╝ ╚═╝╚══════╝╚═════╝ ");
Terminal.warpCursor(40, 1);
Terminal.pause(5);
}
private void debugMenu() {
Terminal.clear();
//title prompt for information
Terminal.warpCursor(1, 1);
System.out.print("");
Terminal.warpCursor(2, 1);
System.out.print(" ██╗ ██╗ ██████╗ ███████╗██████╗ ██╗ ██╗ ██████╗ ███╗ ███╗███████╗███╗ ██╗██╗ ██╗ ██╗ ██╗ ");
Terminal.warpCursor(3, 1);
System.out.print(" ██╔╝██╔╝▄ ██╗▄ ██╔══██╗██╔════╝██╔══██╗██║ ██║██╔════╝ ████╗ ████║██╔════╝████╗ ██║██║ ██║ ▄ ██╗▄╚██╗╚██╗ ");
Terminal.warpCursor(4, 1);
System.out.print("██╔╝██╔╝ ████╗ ██║ ██║█████╗ ██████╔╝██║ ██║██║ ███╗ ██╔████╔██║█████╗ ██╔██╗ ██║██║ ██║ ████╗ ╚██╗╚██╗");
Terminal.warpCursor(5, 1);
System.out.print("╚██╗╚██╗ ▀╚██╔▀ ██║ ██║██╔══╝ ██╔══██╗██║ ██║██║ ██║ ██║╚██╔╝██║██╔══╝ ██║╚██╗██║██║ ██║ ▀╚██╔▀ ██╔╝██╔╝");
Terminal.warpCursor(6, 1);
System.out.print(" ╚██╗╚██╗ ╚═╝ ██████╔╝███████╗██████╔╝╚██████╔╝╚██████╔╝ ██║ ╚═╝ ██║███████╗██║ ╚████║╚██████╔╝ ╚═╝ ██╔╝██╔╝ ");
Terminal.warpCursor(7, 1);
System.out.print(" ╚═╝ ╚═╝ ╚═════╝ ╚══════╝╚═════╝ ╚═════╝ ╚═════╝ ╚═╝ ╚═╝╚══════╝╚═╝ ╚═══╝ ╚═════╝ ╚═╝ ╚═╝ ");
Terminal.warpCursor(8, 1);
Terminal.reset();
//put test statement below this line
System.out.print("\n\rThese are the current enemies "+enemies.size()+"\n\r");
//System.out.print("This is the players icon "+playerIcon);
System.out.printf("\n\rPress any key to return...\n\r");
Terminal.getKey();
}
private void showHelp() {
//setting the color for help once per game
if (justOnceHelp == 0) {
justOnceHelp++;
// setting the color randomly
Random rng = new Random();
helpForeground = rng.nextInt(6);
}
switch (helpForeground) {
case 0: Terminal.setForeground(Color.CYAN);
break;
case 1: Terminal.setForeground(Color.GREEN);
break;
case 2: Terminal.setForeground(Color.MAGENTA);
break;
case 3: Terminal.setForeground(Color.WHITE);
break;
case 4: Terminal.setForeground(Color.YELLOW);
break;
default: Terminal.setForeground(Color.RED);
}
String[] cmds = {"Commands:",
"---------",
"Move: Arrow Keys",
"Pickup an item: p",
"Drop an item: d",
"List items: l",
"Equip weapon: w",
"Equip armor: a",
"Equip other: o",
"Save: s",
"Restore: r",
"Quit: q"
};
for (int row = 0; row < cmds.length; row++) {
Terminal.warpCursor(row + 1, room.getCols() + 1);
System.out.print(cmds[row]);
}
// adding the player info below the commands
String[] info = {
"Name: " + player.getName(),
"HP: " + player.getHealth(),
"Strength: " + player.getDamage(),
"Defense: " + player.getProtection()
};
int line = 0; //the current line of the info array
for (int i = 12; i < 16; i++) {
Terminal.warpCursor(i + 1, room.getCols() + 1);
System.out.print(info[line]);
line++;
}
Terminal.reset();
}
private void setStatus(String mesg) {
// clear anything old first
Terminal.warpCursor(room.getRows(), 0);
for (int i = 0; i < 100; i++) {
System.out.print(" ");
}
// then print the message
Terminal.warpCursor(room.getRows(), 0);
System.out.print(mesg);
}
private void pickup() {
Box thing = checkForBox();
if (thing == null) {
setStatus("There is nothing here to pick up...");
Terminal.pause(1.25);
} else {
if (player.getInventory().add(thing.getItem())) {
setStatus("You added the " + thing.getItem().getName() + " to your inventory.");
boxes.remove(thing);
} else {
setStatus("This is too large for you to add!");
}
Terminal.pause(1.25);
}
}
private void drop() {
if (checkForBox() == null) {
Item dropped = player.getInventory().drop();
if (dropped != null) {
boxes.add(new Box(player.getRow(), player.getCol(), dropped));
}
redrawMapAndHelp();
} else {
setStatus("You cannot drop something on an existing item...");
Terminal.pause(1.25);
}
}
protected boolean handleKey(Key key) {
switch (key) {
case p:
pickup();
break;
case l:
player.getInventory().print();
redrawMapAndHelp();
break;
//debug menu used for printing
case b:
debugMenu();
redrawMapAndHelp();
break;
case d:
drop();
break;
case w:
player.getInventory().equipWeapon();
redrawMapAndHelp();
break;
case a:
player.getInventory().equipArmor();
redrawMapAndHelp();
break;
case o:
player.getInventory().equipOther();
redrawMapAndHelp();
break;
case s:
//saves the current game info to a file
save();
break;
case r:
//restore save data from file while the game is running
File file = new File("save.txt");
try {
Terminal.clear();
Scanner in = new Scanner(file);
roomNumber = in.nextInt();
enemySize = in.nextInt();
boxSize = in.nextInt();
// needed because calling nextInt() leaves the cursor on the same line
in.nextLine();
player = new Player(in);
//read in enemies on current floor
for (int i = 0; i < enemySize; i++) {
enemies.set(i, new Enemy(in));
}
//read in items on current floor
for (int i = 0; i < boxSize; i++) {
boxes.set(i, new Box(in));
}
//consuming a delimeter
in.nextLine();
// redrawing the correct room
if (roomNumber == 1) {
World.instance().setRoom(roomNumber);
room = new Room(in);
}
else if (roomNumber == 2) {
World.instance().setRoom(roomNumber);
room2 = new Room2(in);
}
else if (roomNumber == 3) {
World.instance().setRoom(roomNumber);
room3 = new Room3(in);
}
else if (roomNumber == 4) {
World.instance().setRoom(roomNumber);
room4 = new Room4(in);
}
redrawMapAndHelp();
} catch (FileNotFoundException e) {
Terminal.warpCursor(40,0);
System.out.print("Save data does not exist"); //needs to be formatted
Terminal.pause(2);
}
break;
case LEFT:
player.move(0, -1, room, room2, room3, room4);
break;
case RIGHT:
player.move(0, 1, room, room2, room3, room4);
break;
case UP:
player.move(-1, 0, room, room2, room3, room4);
break;
case DOWN:
player.move(1, 0, room, room2, room3, room4);
break;
// and finally the quit command
case q:
return false;
}
return true;
}
// this is called when we need to redraw the room and help menu
// this happens after going into a menu like for choosing items
private void redrawMapAndHelp() {
//setting the color for the game once
if (justOnce == 0) {
justOnce++;
// setting the color randomly
Random rng = new Random();
Foreground = rng.nextInt(6);
}
switch (Foreground) {
case 0: Terminal.setForeground(Color.CYAN);
break;
case 1: Terminal.setForeground(Color.GREEN);
break;
case 2: Terminal.setForeground(Color.MAGENTA);
break;
case 3: Terminal.setForeground(Color.WHITE);
break;
case 4: Terminal.setForeground(Color.YELLOW);
break;
default: Terminal.setForeground(Color.RED);
}
roomNumber = World.instance().getRoom();
if (roomNumber == 1) {
if (!roomReady) {
initializeRoom();
}
room.draw();
showHelp();
} else if (roomNumber == 2) {
if (!room2Ready) {
initializeRoom2();
}
room2.draw();
showHelp();
} else if (roomNumber == 3) {
if (!room3Ready) {
initializeRoom3();
}
room3.draw();
showHelp();
} else if (roomNumber == 4) {
if (!room4Ready) {
initializeRoom4();
}
room4.draw();
showHelp();
}
}
// This method is used to load the game from the Main menu
protected void loadGameFromMenu() {
//restore save data from file
File file = new File("save.txt");
try {
Terminal.clear();
Scanner in = new Scanner(file);
roomNumber = in.nextInt();
enemySize = in.nextInt();
boxSize = in.nextInt();
// intializing all of the rooms, because we are loading from the menu
if (roomNumber == 1) {
if (!roomReady) {
initializeRoom();
}
} else if (roomNumber == 2) {
if (!room2Ready) {
initializeRoom2();
}
} else if (roomNumber == 3) {
if (!room3Ready) {
initializeRoom3();
}
} else if (roomNumber == 4) {
if (!room4Ready) {
initializeRoom4();
}
}
// needed because calling nextInt() leaves the cursor on the same line
in.nextLine();
player = new Player(in);
//read in enemies on current floor
for (int i = 0; i < enemySize; i++) {
enemies.set(i, new Enemy(in));
}
//read in items on current floor
for (int i = 0; i < boxSize; i++) {
boxes.set(i, new Box(in));
}
//consuming a delimeter
in.nextLine();
// redrawing the correct room
if (roomNumber == 1) {
room = new Room(in);
room.draw();
}
else if (roomNumber == 2) {
room2 = new Room2(in);
room2.draw();
}
else if (roomNumber == 3) {
room3 = new Room3(in);
room3.draw();
}
else if (roomNumber == 4) {
room4 = new Room4(in);
room4.draw();
}
} catch (FileNotFoundException e) {
Terminal.warpCursor(40,0);
System.out.print("Save data does not exist"); //needs to be formatted
Terminal.pause(2);
}
}
// creating a method that will initialize the values for Room 1
private void initializeRoom() {
boxes = room.getBoxes();
enemies = room.getEnemies();
warps = room.getWarp();
warpPosit = room.getPlayerStart();
int row = warpPosit.getRow();
int col = warpPosit.getCol();
player.setPosition(row, col);
roomReady = true;
World.instance().setRoom(1);
roomNumber = World.instance().getRoom();
}
// creating a method that will initialize the values for Room 2
private void initializeRoom2() {
boxes = room2.getBoxes();
enemies = room2.getEnemies();
warps = room2.getWarp();
warpPosit = room2.getPlayerStart();
int row = warpPosit.getRow();
int col = warpPosit.getCol();
player.setPosition(row, col);
room2Ready = true;
World.instance().setRoom(2);
roomNumber = World.instance().getRoom();
}
// creating a method that will initialize the values for Room 3
private void initializeRoom3() {
boxes = room3.getBoxes();
enemies = room3.getEnemies();
warps = room3.getWarp();
warpPosit = room3.getPlayerStart();
int row = warpPosit.getRow();
int col = warpPosit.getCol();
player.setPosition(row, col);
room3Ready = true;
World.instance().setRoom(3);
roomNumber = World.instance().getRoom();
}
// creating a method that will initialize the values for Room 4
private void initializeRoom4() {
boxes = room4.getBoxes();
enemies = room4.getEnemies();
warps = room4.getWarp();
warpPosit = room4.getPlayerStart();
int row = warpPosit.getRow();
int col = warpPosit.getCol();
player.setPosition(row, col);
room4Ready = true;
World.instance().setRoom(4);
roomNumber = World.instance().getRoom();
}
// returns a Box if the player is on it -- otherwise null
private Box checkForBox() {
Position playerLocation = player.getPosition();
for (Box box : boxes) {
if (playerLocation.equals(box.getPosition())) {
return box;
}
}
return null;
}
// returns a Warp if the player is on it -- otherwise null
private Warp checkForWarp() {
Position playerLocation = player.getPosition();
for (Warp warp : warps) {
if (playerLocation.equals(warp.getPosition())) {
return warp;
}
}
return null;
}
// check for battles and return false if player has died
private boolean checkBattles() {
Position playerLocation = player.getPosition();
// look for an enemy that is close
Enemy opponent = null;
for (Enemy enemy : enemies) {
if (playerLocation.isAdjacent(enemy.getPosition())) {
opponent = enemy;
}
}
// now do the battle
if (opponent != null) {
opponent.setBattleActive();
return player.fight(opponent, room, enemies);
}
return true;
}
public void run() {
// draw these for the first time now
redrawMapAndHelp();
boolean playing = true;
while (playing) {
// draw the entities
for (Box box : boxes) {
box.draw();
}
for (Enemy enemy : enemies) {
enemy.draw();
}
for (Warp warp : warps) {
warp.draw();
}
player.draw();
// read a key from the user
Terminal.warpCursor(room.getRows() + 1, 0);
Key key = Terminal.getKey();
playing = handleKey(key);
// clear status by default
setStatus("");
// move the enemies
for (Enemy enemy : enemies) {
enemy.walk(room, room2, room3, room4);
}
// check for battles
if (!checkBattles()) {
setStatus("You have been killed :(\n\r");
gameOver();
playing = false;
}
if (playing) {
showHelp();
}
// check if we are on a box and print what's in it
Box thingHere = checkForBox();
if (thingHere != null) {
setStatus("Here you find: " + thingHere.getItem().getName() + " Weight: " + thingHere.getItem().getWeight() + " Value: " + thingHere.getItem().getValue() + " Strength: " + thingHere.getItem().getStrength());
}
// copied the same idea as above however it may make sense to make this bit below its own method
// check if we are on a warp, print question, and store return response from user
Warp aWarp = checkForWarp();
if (aWarp != null) {
if (enemies.size() == 0) {
setStatus("The door unlocked");
Terminal.pause(2);
setStatus("Would you like to go to the next room? Y or N: ");
// asking for the response
Scanner response = new Scanner(System.in);
String answer = response.next();
if (answer.equalsIgnoreCase("Y")) {
if (roomNumber < 4) {
roomNumber = World.instance().roomUpdate();
//player.resetHP();
redrawMapAndHelp();
save();
}
else {
playerWon();
playing = false;
}
}
} else if (enemies.size() > 0 ) { //if not all of the enemies in the room are dead yet
setStatus("Door is locked! Rip and Tear!");
}
}
}
}
/** Creates a text file that data from the game is written into with a printwriter. This file stores the number
* of the room that the player last saved in, along with the number of enemies and boxes in that room. The method
* also saves data about the player, the enemies and boxes on the map, and the map room layout itself to the file
*
*/
public void save() {
try {
PrintWriter pw = new PrintWriter("save.txt");
pw.println(roomNumber);
enemySize = enemies.size(); //helps with loading in save data
boxSize = boxes.size();
pw.println(enemySize);
pw.println(boxSize);
// Saves the player as an Entity
player.save(pw);
//info about enemies on the floor
for (Enemy enemy : enemies) {
enemy.save(pw);
}
//info about items on the floor
for (Box box : boxes) {
box.save(pw);
}
pw.println(".");
if (roomNumber == 1) {
room.save(pw);
}
else if (roomNumber == 2) {
room2.save(pw);
}
else if (roomNumber == 3) {
room3.save(pw);
}
else {
room4.save(pw);
}
pw.close(); //closes the printwriter
//Prints message to display
Terminal.warpCursor(40,0);
setStatus("Your game was saved");
Terminal.pause(2);
} catch (FileNotFoundException e) {
Terminal.warpCursor(40,0);
System.out.print("Could not save data");
Terminal.pause(2);
}
}
}