-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGame.java
More file actions
415 lines (347 loc) · 14.6 KB
/
Game.java
File metadata and controls
415 lines (347 loc) · 14.6 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
import java.util.ArrayList;
import java.util.Map;
import java.util.Scanner;
public class Game
{
private Player player;
public void playGame()
{
Scanner scan = new Scanner(System.in);
Map<String, Room> roomMap = FileLoader.loadRooms("Room.txt");
ArrayList<Item> items = FileLoader.loadItems("item.txt");
ArrayList<Puzzle> puzzles = FileLoader.loadPuzzles("puzzles.txt");
ArrayList<Character> characters = FileLoader.loadChars("Character.txt");
player = new Player("R00");
player.setCurrentRoom(roomMap.get(player.getCurrentRoomID()));
Room currentRoom = player.getCurrentRoom();
Character eliteMerc = null;
for(Character monster: characters)
{
if(monster.getName().equalsIgnoreCase("Elite Mercenary"))
{
eliteMerc = monster;
break;
}
}
for(Room room : roomMap.values())
{
room.setEliteMercenary(eliteMerc);
}
System.out.println("Welcome to Master Jewel Thief!\n");
System.out.println("Type 'quit' to exit the game.");
System.out.println("Type 'help' to open the help menu.");
System.out.println("Type 'read map' to open the map.");
System.out.println("(Above commands only available after initial path chosen)\n");
while(true)
{
if(currentRoom.getRoomID().equals("R00"))
{
System.out.println("Where would you like to start?: 'window' or 'backdoor'");
System.out.println("Type 'Choose <window/backdoor>'");
String input = scan.nextLine().trim().toLowerCase();
if(input.startsWith("choose "))
{
String path = input.substring(7).trim();
String startRoomID = null;
switch (path) {
case "window":
startRoomID = "R02";
System.out.println("You climb through the garden window...\n");
break;
case "backdoor":
startRoomID = "R01";
System.out.println("You slip through the backdoor quietly...\n");
break;
default:
System.out.println("That path does not exist.\n");
continue;
}
// set current room and player
currentRoom = roomMap.get(startRoomID);
player.setCurrentRoomID(startRoomID);
player.setCurrentRoom(currentRoom);
// print room info
System.out.println(currentRoom.getRoomName());
System.out.println(currentRoom.getRoomDescr());
System.out.println("Exits: " + currentRoom.getExits().keySet());
System.out.println();
if (currentRoom.hasPuzzle()) {
currentRoom.getPuzzle().startPuzzle(player);
}
//player.startPuzzle();
continue;
}
}
System.out.println("Enter a command: ");
String input = scan.nextLine().trim().toLowerCase();
if(input.equals("quit"))
{
System.out.println("Thanks for playing. Bye!");
break;
}
if(input.equals("help"))
{
help();
continue;
}
if(input.startsWith("go "))
{
String direction = input.substring(3).trim();
player.move(direction, roomMap);
currentRoom = roomMap.get(player.getCurrentRoomID());
player.setCurrentRoom(currentRoom);
continue;
}
if(input.equals("explore"))
{
player.exploreRoom(roomMap);
continue;
}
if(input.equals("inventory"))
{
player.accessInventory();
continue;
}
if(input.startsWith("pickup "))
{
String item = input.substring(7).trim();
player.pickUpItem(item, roomMap);
continue;
}
if(input.startsWith("drop "))
{
String item = input.substring(5).trim();
player.dropItem(item, roomMap);
continue;
}
if(input.startsWith("use "))
{
String item = input.substring(4).trim();
player.useItem(item, roomMap);
continue;
}
if(input.startsWith("inspect "))
{
String item = input.substring(8).trim();
player.inspectItem(item);
continue;
}
if(input.startsWith("equip "))
{
String item = input.substring(6).trim();
player.equipItem(item);
continue;
}
if(input.startsWith("unequip "))
{
String item = input.substring(8).trim();
player.unequipItem(item);
continue;
}
if(input.startsWith("attack "))
{
String monsterName = input.substring(7).trim();
Character monster = null;
currentRoom = roomMap.get(player.getCurrentRoomID());
for(Character character : currentRoom.getCharacterList())
{
if(character.getName().equalsIgnoreCase(monsterName))
{
monster = character;
break;
}
}
if (monster == null)
{
System.out.println("No monster with that name.\n");
continue;
}
player.attack(monster);
if (monster.isMonster() && monster.isAlive())
{
monsterCounterAttack(monster);
}
else if (!monster.isAlive())
{
currentRoom.getCharacterList().remove(monster);
}
continue;
}
if (input.startsWith("flee"))
{
if (!hasLivingMonster(currentRoom))
{
System.out.println("You can only flee while in combat with a monster.\n");
continue;
}
String arg = input.length() > 4 ? input.substring(5).trim() : "";
if (arg.isEmpty())
{
System.out.println("Which direction do you want to flee? (N/S/E/W or north/east/south/west)");
continue;
}
String direction = arg.toLowerCase();
if (direction.equals("n")) direction = "north";
else if (direction.equals("s")) direction = "south";
else if (direction.equals("e")) direction = "east";
else if (direction.equals("w")) direction = "west";
player.move(direction, roomMap);
currentRoom = roomMap.get(player.getCurrentRoomID());
continue;
}
if(input.equals("check status"))
{
player.checkStatus();
continue;
}
if (input.startsWith("save")) {
String[] parts = input.split(" ", 2);
if (parts.length < 2) {
System.out.println("Save <filename>");
continue;
}
// ✅ Ensure currentRoom is up-to-date
Room loadedRoom = roomMap.get(player.getCurrentRoomID());
player.setCurrentRoom(loadedRoom);
currentRoom = loadedRoom;
// ✅ Reset puzzle mode if the room has no active puzzle
if (!loadedRoom.hasPuzzle() || loadedRoom.getPuzzle().isSolved()) {
player.setInPuzzleMode(false);
}
// ✅ Save file
String fileName = parts[1].replaceAll("[\\\\/:*?\"<>|]", "_") + ".txt";
player.saveGame(fileName, roomMap);
System.out.println("Game saved to " + fileName + "\n");
continue;
}
if (input.equals("load checkpoint"))
{
System.out.println("Loading checkpoint...");
player.loadGame("checkpoint.txt", characters, items, roomMap);
// Restore the current room
Room loadedRoom = roomMap.get(player.getCurrentRoomID());
player.setCurrentRoom(loadedRoom);
currentRoom = loadedRoom;
// ✅ Reset puzzle mode if player wasn't mid-puzzle in save
if (loadedRoom.hasPuzzle() && loadedRoom.getPuzzle().isSolved()) {
player.setInPuzzleMode(false); // puzzle already solved
} else if (!loadedRoom.hasPuzzle()) {
player.setInPuzzleMode(false); // no puzzle in room
}
System.out.println("Loaded into: " + currentRoom.getRoomName());
continue;
}
if(input.startsWith("load "))
{
String[] parts = input.split(" ", 2);
if (parts.length < 2)
{
System.out.println("Load <filename>");
continue;
}
String fileName = parts[1] + ".txt";
player.loadGame(fileName, characters, items, roomMap);
currentRoom = roomMap.get(player.getCurrentRoomID());
player.setCurrentRoom(currentRoom);
if (!currentRoom.hasPuzzle() || currentRoom.getPuzzle().isSolved()) {
player.setInPuzzleMode(false);
}
System.out.println("Loaded into: " + currentRoom.getRoomName());
continue;
}
if(input.equals("read map"))
{
player.readMap();
continue;
}
if(input.equals("check monster stats"))
{
Character monster = null;
for (Character c : currentRoom.getCharacterList())
{
if (c.isMonster() && c.isAlive())
{
monster = c;
break;
}
}
if (monster == null)
{
continue;
}
System.out.println("Monster: " + monster.getName());
System.out.println("HP: " + monster.getHealth());
System.out.println("ATK: " + monster.getDamage());
System.out.println();
continue;
}
// 🔒 NEW: if the player is in PuzzleMode, send input to the puzzle
if (player.isInPuzzleMode()) {
Room puzzleRoom = player.getCurrentRoom();
if (puzzleRoom != null && puzzleRoom.hasPuzzle()) {
Puzzle activePuzzle = puzzleRoom.getPuzzle();
String result = activePuzzle.tryAnswer(input);
System.out.println(result);
} else {
// safety fallback: no puzzle found
player.setInPuzzleMode(false);
System.out.println("There is no active puzzle in this room.\n");
}
// do NOT treat this as a normal command
continue;
}
}
}
private void monsterCounterAttack(Character monster)
{
int dmg = monster.getDamage();
System.out.println(monster.getName() + " attacks you for " + dmg + " damage!\n");
player.takeDamage(dmg, monster);
if (player.getPlayerHP() <= 0)
{
System.out.println(monster.getPlayerDies());
System.out.println("Game Over.\n");
System.exit(0);
}
}
private boolean hasLivingMonster(Room room)
{
if (room == null) return false;
for (Character c : room.getCharacterList())
{
if (c.isMonster() && c.isAlive())
{
return true;
}
}
return false;
}
public void help()
{
System.out.println("\n------------------------Help Menu---------------------------");
System.out.println(" quit | Exit game");
System.out.println(" help | Open help menu");
System.out.println(" read map | Open a pop-up map of game for each floor");
System.out.println(" go <direction> | Navigate rooms");
System.out.println(" explore | View the room details");
System.out.println(" check status | View player statistics");
System.out.println(" inventory | Open player inventory");
System.out.println(" pick up <item> | Pick up an item in the room");
System.out.println(" inspect <item> | Get item details from inventory items");
System.out.println(" use <item> | Use/consume item in inventory");
System.out.println(" drop <item> | Remove an item from player inventory into the room");
System.out.println(" equip <weapon> | Equip a weapon from player inventory");
System.out.println(" unequip <weapon> | Unequip a weapon from player inventory");
System.out.println(" attack <monster> | Enter combat mode and attack a monster");
System.out.println(" flee | Player flees to the previous room");
System.out.println(" flee <N|S|E|W> | Flee north, south, east, or west");
System.out.println(" save <filename> | Save the game progress");
System.out.println(" load <filename> | Load a saved file");
System.out.println(" load checkpoint | Load a saved checkpoint file\n");
}
public static void main(String[] args)
{
Game game = new Game();
game.playGame();
}
}