Repository navigation
Expand file tree
/
Copy pathUserInterface.java
More file actions
538 lines (501 loc) · 13.8 KB
/
Copy pathUserInterface.java
File metadata and controls
538 lines (501 loc) · 13.8 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
/**
* CS 141: Intro to Programming and Problem Solving
* Professor: Edwin Rodríguez
*
* Programming Assignment #N
*
* <description-of-assignment>
* This is a team project in which we implement a 2D game containing
* various objects: rooms, power ups, player, and ninjas.
* The goal of this game is to have the user interact with different
* entities in the program and to complete the objective of finding
* the briefcase.
*
* Team Blank
*
* Justen Minamitani
* Saroj Poudel
* Steve Marrero
* Aaron Lim
* Koshi Huynh
* Jon Camarillo
*/
package edu.cpp.cs.cs141.final_prog_assignment;
import java.util.InputMismatchException;
import java.util.Scanner;
/**
* This class handles all the interactions between the game and the user.
*/
public class UserInterface {
private enum status{WON, LOST, CONTINUE};
private GameEngine game;
private Scanner scan = null;
/**
* The game status is set as CONTINUE until player wins or loses.
*/
private status gameStatus = status.CONTINUE;
/**
* This field represents difficulty of the game
*/
private int level;
/**
* Default constructor that sets the game in motion.
* @param game GameEngine object
*/
public UserInterface(GameEngine game) {
this.game = game;
scan = new Scanner(System.in);
}
/**
* Displays the title of the game at the start.
*/
public void printTitle() {
System.out.println("**********************\n"
+ "* Get the Cheese *\n"
+ "**********************");
}
/**
* This method will display the main menu at the start of the game or when the
* player quits the game. Game will end when the player exits from this menu.
* @return userInput an integer from 1 to 4
*/
public int mainMenu() {
int userInput = 0;
boolean valid = false;
System.out.println( "Main Menu: \n"
+ "------------------\n"
+ "\t[1] How to Play\n"
+ "\t[2] Start New Game\n"
+ "\t[3] Load Game\n"
+ "\t[4] Exit Game\n");
while(!valid) {
try {
userInput = scan.nextInt();
if(userInput > 0 && userInput < 5) {
valid = true;
}
else
System.out.println("Error: Enter a number from 1 to 4"
+ "\nTry again: ");
}
catch(InputMismatchException e) {
System.out.println("Error: You must enter an integer "
+ "\nTry again: ");
scan.nextLine();
}
}
return userInput;
}
/**
* This method represents an extentions of the main menu. It will
* direct the user depending on what they chose in main menu.
*/
public void startGame() {
boolean valid;
do {
valid = false;
int option = mainMenu();
switch(option) {
case 1:
howToPlay();
break;
case 2:
showLine();
System.out.println("New Game");
showLine();
chooseDifficulty();
loop(level);
break;
case 3:
showLine();
if(loadGame())
playGame(getLevel());
else
startGame();
break;
case 4:
showLine();
System.out.println("Game will close.\n" + "GOODBYE" ); ///closes/exits game
showLine();
System.exit(0);
valid = true;
break;
}
}while(!valid);
}
/**
* This method will create the current difficulty of the game.
*/
public void chooseDifficulty() {
System.out.println("Select Difficulty. \n"
+ "\t(1) lol\n"
+ "\t(2) Yo. Dis hard. \n"
+ "\t(3) DAFUQ?!\n");
boolean isCorrectInput = false;
while(!isCorrectInput) {
try {
level = scan.nextInt();
switch(level) {
case 1:
game.setDifficulty(1);
isCorrectInput = true;
break;
case 2:
game.setDifficulty(2);
isCorrectInput = true;
break;
case 3:
game.setDifficulty(3);
isCorrectInput = true;
break;
default:
System.out.println("Error: Must enter a number from 1 to 3."
+ "\nTry again: ");
}
}catch(InputMismatchException e) {
System.out.println("Error: Must input an integer"
+ "\nTry again:");
scan.nextLine();
}
}
}
/**
* This method represents the game loop. It will start by creating
* the board and directing the user to the game.
* @param level integer that represents the difficulty level
*/
public void loop(int level) {
game.createBoard();
playGame(level);
}
/**
* This method will check to see if the spy and the ninja are adjacent to
* each other and display and kill the player if they are.
*/
public void checkSpy() {
if(game.checkSpy()) { //ninja check method
showLine();
System.out.println("A Ninja destroyed you!");
}
}
/**
* This method is an extension of the look funciion. The user input in
* the look function will direct the game to either look in a direction,
* activate debug, save, or load a game.
*/
public void lookCommand() {
boolean valid = false;
while(!valid) {
String direction = look();
switch(direction) {
case "w":
case "a":
case "s":
case "d":
if(game.look(direction)) {
showLine();
System.out.println("All Clear!");
}
else {
showLine();
System.out.println("Ninja Ahead!");
}
valid = true;
break;
case "r":
game.debugMode();
showLine();
displayBoard();
break;
case "1":
saveGame();
displayBoard();
break;
case "2":
showLine();
if(loadGame())
playGame(getLevel());
else
System.out.println("Error: Unable to load");
break;
case "3":
showLine();
quitGame();
break;
}
}
displayBoard();
}
/**
* This method is called upon when the player wants to shoot their gun.
* The player will shoot at a direction the user chooses.
*/
public void shootCommand() {
String userInput = "";
System.out.println("Choose direction to shoot");
commandMenu();
boolean valid = false;
while(!valid) {
userInput = scan.next().toLowerCase();
switch(userInput) {
case "w":
case "a":
case "s":
case "d":
showLine();
System.out.println(game.shoot(userInput));
showLine();
valid = true;
break;
default:
System.out.println("Error: Invalid input. Try Again.");
break;
}
}
}
/**
* This method directs the action of the player after the look phase.
* The user input commands the player to move 1 space, shoot, or activate
* debug mode.
*/
public void moveCommand() {
String direction = "";
boolean valid = false;
showLine();
System.out.print("Make a wise choice: \nMove: ");
commandMenu();
System.out.println("\nF(Shoot) \nR(Debug)");
while(!valid) {
direction = scan.next().toLowerCase();
switch(direction) {
case "w":
case "a":
case "s":
case "d":
valid = game.move(direction);
break;
case "r":
game.debugMode();
displayBoard();
break;
case "f":
shootCommand();
valid = true;
break;
}
}
}
/**
* This method will allow player to play the game on the desired difficulty. It will
* get its action from {@link GameEngine} and display messages with each command.
* This method will control the flow of the game.
* @param lvl String input from user that indicates the difficulty of the game.
*/
public void playGame(int lvl) {
String direction;
gameStatus = status.CONTINUE;
while(gameStatus == status.CONTINUE) {
displayBoard();
printStatus();
lookCommand();
moveCommand();
if (game.checkPlayerIsBriefcase() == true){
displayBoard();
showLine();
System.out.println("YOU FOUND THE BRIEFCASE!"
+"\nCONGRATS YOU WIN!");
showLine();
System.out.println("\n\n\n\n");
gameStatus = status.WON;
startGame(); //added
}
if(game.checkItem()) {
int itemPickUp = game.applyItem();
printStatus();
displayBoard();
showLine();
switch(itemPickUp) {
case 1:
System.out.println("You have picked up a Radar!");
break;
case 2:
System.out.println("You have picked up an Invincibility!");
break;
case 3:
System.out.println("You have picked up an Ammo!");
break;
case 0:
System.out.println("Cannot pick up Ammo!");
break;
default:
break;
}
}
checkSpy();
game.ninjaDecision();
game.decInvincibility();
if(game.playerAlive() == false) {
gameStatus = status.LOST;
showLine();
System.out.println("YOU LOST THE GAME! LOSER!\n");
startGame();
}
}
}
/**
* This method will only display the directions. No input is necessary.
*/
public void commandMenu() {
System.out.println("\t W(UP)\n A(LEFT) S(DOWN) D(RIGHT)");
}
/**
* This method will get an input from the user for which direction the user wants to look,
* if they want to save, if they want to load, or if they want to reload.
* @return direction a String for instructions.
*/
private String look() {
boolean valid = false;
String direction = "";
showLine();
System.out.println("Choose a direction to look");
commandMenu();
System.out.print("R(Debug)\t 1(Save)\t 2(Load) \t3(Quit)");
while(!valid) {
direction = scan.next().toLowerCase();
if(direction.equals("w") || direction.equals("a") || direction.equals("s") || direction.equals("d")
|| direction.equals("r")|| direction.equals("1")|| direction.equals("2") || direction.equals("3"))
valid = true;
else
System.out.println("Invalid Entry. Try Again");
}
return direction;
}
/**
* This method displays the current status of the player and the number of ninjas left
*/
public void printStatus() {
System.out.println("STATUS: \n"
+ "-----------"
+ "\nInvincibility: "+ game.invCount()
+ "\nAmmo: "+ game.getAmmoCount()
+ "\nLives: " + game.getPlayer().getLives()
+ "\nNinjas Left: " + game.getNumNinja() + "\n");
}
/**
* This method will display the board onto the screen
*/
private void displayBoard() {
int length = 9;
for(int x = 0;x < length; x++) {
System.out.println("");
for(int y = 0;y < length;y++) {
System.out.print(game.displayBoard(x, y));
}
}
System.out.println("\n");
}
/**
* This method displays the background information of the game, and instructions on how to play.
* It will describe the controls, and what each item does.
*/
public void howToPlay() {
System.out.println("Background:\n"
+ "--------------\n"
+ "You are a SPY that infiltrated the enemy's base of operation in order to steal a briefcase\n"
+ "containing classified information. The building you have infiltrated has a 9x9 layout, and\n"
+ "is pitch black. Luckily, you brought along your nightvision goggle that allows your to see\n"
+ "two spaces ahead of you. The briefcase is hidden in one of nine rooms, and for added security\n"
+ "there are six ninjas patrolling the building. Your objective is to safely navigate the building\n"
+ "while doing your best to avoid the ninjas. If the ninjas is next to you, the ninja will stab you\n"
+ "sending you back to the beginning of the game. When a ninja kills you three times you have failed\n"
+ "your objective. You do have a gun with a single bullet, so use it with caution. While navigating\n"
+ "the building, you may find items laying on the floor: radar, invincible potion, or one more ammo.\n"
+ "The items will grant you a power up that will help you on your quest to steal the briefcase.\n"
+ "GOOD LUCK!\n");
System.out.println("Items:\n"
+ "(R) - RADAR - When picked up it will show your which room contains the briefcase\n"
+ "(A) - AMMO - An additional bullet, can only be picked up if you have no ammo\n"
+ "(I) - INVINCIBILITY - For 5 turns you are impervious to death\n");
System.out.println("Controls: \n"
+ "\t W(UP)\n A(LEFT) S(DOWN) D(RIGHT) \n"
+ "- You can only move one space per turn. If you run into a wall or room, your turn does not expire\n"
+ "- Rooms can only be accessed from the NORTH\n"
+ "- Look ahead(once per turn) 2 spaces in the direction of your choice\n"
+ "[r] - debug mode\n"
+ "[f] - shoot, use wasd for which direction. The bullet will travel in a straight trajectory until\n"
+ "\tit hits a Ninja, Wall, or a Room\n"
+ "-At any time type [save] to save your progress or type [quit] to return to the main menu\n");
}
/**
* This method will ask the user to create a new save file or to overwrite an
* existing one.
*/
public void saveGame() {
String userInput;
showLine();
System.out.println("Save: Name your save file:");
userInput = scan.next();
scan.nextLine();
game.save(userInput);
}
/**
* This method will ask the user to load an existing file that they have
* previously created
* @return true if the file exist, false if it doesn't
*/
public boolean loadGame() {
String userInput;
System.out.println("Load: Enter file name");
userInput = scan.next();
scan.nextLine();
if(game.load(userInput))
return true;
else {
System.out.println("Unable to Load\n");
return false;
}
}
/**
* This method will be called when the player quits the game. It will check
* to see if the player wanted to save their progress before quitting and
* send them back to the main menu.
*/
public void quitGame() {
String userInput;
boolean valid = false;
do {
showLine();
System.out.println("Would you like to save? [Y]es / [N]o");
userInput = scan.next();
if(userInput.toLowerCase().equals("y")) {
saveGame();
valid = true;
startGame();
}
else if(userInput.toLowerCase().equals("n")) {
showLine();
System.out.println("The game will now return to main menu.\n");
valid = true;
startGame();
}
else {
showLine();
System.out.println("Invalid Input!");
valid = false;
}
}while(!valid);
}
/**
* This method will return the current difficulty of the game
* @return String value of the level difficulty.
*/
public int getLevel(){
return level;
}
/**
* Used as a styling tool for the UI
*/
public void showLine() {
System.out.println("\n----------------------------------\n");
}
}