Repository navigation
Expand file tree
/
Copy pathUserInterface.java
More file actions
414 lines (376 loc) · 11.3 KB
/
Copy pathUserInterface.java
File metadata and controls
414 lines (376 loc) · 11.3 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
/**
* 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.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
*/
String level;
/**
* Default contructor that sets the game in motion.
* @param game GameEngine object
*/
public UserInterface(GameEngine game) {
this.game = game;
scan = new Scanner(System.in);
}
public void printTitle() {
System.out.println("\tGet 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.
*/
public void mainMenu() {
printTitle();
System.out.println( "Main Menu: \n"+
"------------------\n"
+ "\t1) How to Play\n" +
"\t2) Start New Game\n" +
"\t3) Load Game\n" +
"\t4) Exit Game\n");
String input = scan.next();
switch(input) {
case "1":
howToPlay();
mainMenu();
break;
case "2":
showLine();
System.out.println("You chose 2.");
showLine();
startGame(chooseDifficulty());
break;
case "3":
showLine();
System.out.println("You chose 3.");
showLine();
if(loadGame())
playGame(getLevel());
else
mainMenu();
break;
case "4":
showLine();
System.out.println("You chose 4.\n" + "Game will close.\n" + "GOODBYE" ); ///closes/exits game
showLine();
System.exit(0);
break;
default: //when anything besides 1,2,3,4 is pressed in menu
showLine();
System.out.println("Invalid Input");
mainMenu();
break;
}
}
/**
* This method will create the current difficulty of the game.
* @return difficulty level
*/
public String chooseDifficulty()
{
System.out.println("Select Difficulty. \n"
+ "\t(1) lol\n"
+ "\t(2) Yo. Dis hard. \n"
+ "\t(3) DAFUQ?!\n");
boolean isCorrectInput = false;
do{
Scanner input = new Scanner(System.in);
level = input.next();
if (level.equals("1") || level.equals("2") || level.equals("3")) {
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;
}
}
else
System.out.println("Nah. Enter 1, 2, or 3.");
} while(isCorrectInput == false);
return level;
}
// Will probably need to catch exception when a String is entered
/**
* This method will create the board at the start of a new game.
*/
public void startGame(String level) {
game.createBoard();
playGame(level);
}
/**
* 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(String lvl){
String direction;
gameStatus = status.CONTINUE;
while(gameStatus == status.CONTINUE) {
direction = look();
if(game.look(direction))
{
showLine();
System.out.println("All Clear!");
}
else {
showLine();
System.out.println("Ninja Ahead!");
}
boolean valid = false;
while(!valid) { //repeat until user input a valid key -- take action(move or shoot)
showLine();
printStatus();
displayBoard();
System.out.print("\tMake a wise choice: \nMove: \n\t W(UP)\n A(LEFT) S(DOWN) D(RIGHT) \nShoot: F \nDebug: R \n");
direction = scan.next().toLowerCase();
if(direction.equals("r"))
game.debugMode();
else if(direction.equals("f")) {
showLine();
System.out.print("Choose direction to fire: \n\t W(UP)\n A(LEFT) S(DOWN) D(RIGHT) \n");
direction = scan.next();
showLine();
System.out.print(game.shoot(direction));
valid = true;
}
else if(direction.equals("w")|| direction.equals("a") || direction.equals("s") || direction.equals("d")){
valid = game.move(direction);
}
else {
showLine();
System.out.println("Invalid Input!");
}
}
if (game.checkPlayerIsBriefcase() == true){
displayBoard();
showLine();
System.out.println("YOU FOUND THE BRIEFCASE!");
showLine();
System.out.println("\n\n\n\n");
gameStatus = status.WON;
mainMenu(); //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;
}
}
//add check item method here
if(game.checkSpy()) { //ninja check method
showLine();
System.out.println("A Ninja destroyed you!");
}
game.ninjaDecision();
game.decInvincibility();
if(game.playerAlive() == false) {
gameStatus = status.LOST;
showLine();
System.out.println("YOU LOST THE GAME! LOSER!\n");
mainMenu();
}
}
}
private String look() {
boolean valid = false;
String direction = "";
while(!valid) {
showLine();
printStatus();
displayBoard();
System.out.print("\nChoose direction to look: \n\t W(UP)\n A(LEFT) S(DOWN) D(RIGHT) \nDebug: R \nType \"save\" to save, \"quit\" to exit game.\n");
direction = scan.next().toLowerCase();
if(direction.equals("r"))
game.debugMode();
if(direction.toLowerCase().equals("w")||direction.toLowerCase().equals("a")||direction.toLowerCase().equals("s")||direction.toLowerCase().equals("d"))
valid = true;
else if(direction.toLowerCase().equals("save"))
saveGame();
else if(direction.toLowerCase().equals("quit"))
quitGame();
}
return direction;
}
/**
* This method displays the current status of the player and the number of ninjas left
*/
public void printStatus() {
System.out.println("Invincibility: "+ game.invCount());
System.out.println("Ammo: "+ game.getAmmoCount());
System.out.println("Lives: " + game.getPlayer().getLives());
System.out.println("Ninjas 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;
mainMenu();
}
else if(userInput.toLowerCase().equals("n")) {
showLine();
System.out.println("The game will now return to main menu.\n");
valid = true;
mainMenu();
}
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 String getLevel(){
return level;
}
public void showLine() {
System.out.println("\n----------------------------------\n");
}
}