-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGameRunner.java
More file actions
254 lines (232 loc) · 10.7 KB
/
GameRunner.java
File metadata and controls
254 lines (232 loc) · 10.7 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
package APCSA.APCSA_Code_Your_Own;
import java.util.*;
public class GameRunner {
public static void runGame() {
System.out.println(
"\n\n\n~.~.~. Welcome to Adventure Game by Will Barber! To quit the game, simply type \"quit\" from the main command screen. Follow the instructions on screen to play the game. ~.~.~.\n\n");
System.out.print("files loading...");
Player restoredPlayer = Player.restore();
Map restoredMap = Map.restore();
System.out.println("loaded");
boolean doRestore = false;
if (restoredPlayer != null && restoredMap != null) {
String restoreFile = Utils.inputString("Previous save file detected. Restore file? y/n: ",
new String[] { "y", "n" }, "Please enter \"y\" or \"n\": ", false);
if (restoreFile.toLowerCase().equals("y")) {
doRestore = true;
} else {
restoreFile = Utils.inputString("Are you sure? The previous file will be overriden. y/n: ",
new String[] { "y", "n" }, "Please enter \"y\" or \"n\": ", false);
if (restoreFile.toLowerCase().equals("n")) {
doRestore = true;
}
}
}
System.out.println();
if (doRestore) {
// make rooms match
for (Room room : restoredPlayer.getVisited()) {
int row = room.getRow();
int column = room.getColumn();
restoredMap.getRooms()[row][column] = room;
}
// Make rooms in map match
restoredPlayer.removeItem("map");
restoredPlayer.addItem("map", new MapItem(restoredMap));
runGameLoop(restoredPlayer, restoredMap);
} else {
int mapSize = Utils.inputInt("Enter a map size from 4 to 8: ", 4, 8);
System.out.println();
Map map = new Map(mapSize);
HashMap<String, Item> initialInventory = new HashMap<String, Item>();
initialInventory.put("map", new MapItem(map));
Player player = new Player("north", initialInventory);
player.setRow(0);
player.setColumn(0);
player.addVisited(map.getRoom(player.getRow(), player.getColumn()));
runGameLoop(player, map);
}
}
private static void runGameLoop(Player player, Map map) {
String userOption = "none";
while (!userOption.equals("quit")) {
// if (!map.getRoom(player.getRow(), player.getColumn()).hasNpcs()) {
userOption = Utils
.inputString("Enter your next command. To view a list of commands, type \"options\": ");
System.out.println();
switch (userOption) {
case "quit":
// TODO: save game files
System.out.println("Thanks for playing!");
break;
case "options":
printCommandOptions();
break;
case "room description":
System.out.println(map.getRoom(player.getRow(), player.getColumn()));
break;
case "action":
playerAction(player, map);
break;
case "inventory":
System.out.println(player.getInventoryString());
break;
case "player information":
printPlayerInfo(player);
break;
case "debugMap":
System.out.println(map.toString());
default:
System.out.println("Invalid command. To view a list of options, type \"options\".\n");
}
// } else {
// npcEncounter(map, player);
// }
}
player.save();
map.save();
}
private static void printCommandOptions() {
String[] availableOptions = new String[] { "quit", "inventory", "player information", "options",
"room description", "action" };
for (String option : availableOptions) {
System.out.println("\t" + option);
}
System.out.println();
}
private static void movePlayer(Player player, Map map) {
String userDirection = Utils.inputString("Enter a direction: North, East, South, or West: ",
new String[] { "north", "east", "south", "west" },
"\nInvalid option. Please enter one of the options listed.\n", false);
switch (userDirection) {
case "north":
if (map.checkSingleLink(player.getRow(), player.getColumn(), player.getRow() - 1, player.getColumn(),
map.getRooms())) {
player.setRow(player.getRow() - 1);
} else {
System.out.println(
"\nInvalid choice: Cannot move in the desired direction. This may be due to a wall or the edge of the map. Try checking your map.");
}
break;
case "east":
if (map.checkSingleLink(player.getRow(), player.getColumn(), player.getRow(), player.getColumn() + 1,
map.getRooms())) {
player.setColumn(player.getColumn() + 1);
} else {
System.out.println(
"\nInvalid choice: Cannot move in the desired direction. This may be due to a wall or the edge of the map. Try checking your map.");
}
break;
case "south":
if (map.checkSingleLink(player.getRow(), player.getColumn(), player.getRow() + 1, player.getColumn(),
map.getRooms())) {
player.setRow(player.getRow() + 1);
} else {
System.out.println(
"\nInvalid choice: Cannot move in the desired direction. This may be due to a wall or the edge of the map. Try checking your map.");
}
break;
case "west":
if (map.checkSingleLink(player.getRow(), player.getColumn(), player.getRow(), player.getColumn() - 1,
map.getRooms())) {
player.setColumn(player.getColumn() - 1);
} else {
System.out.println(
"\nInvalid choice: Cannot move in the desired direction. This may be due to a wall or the edge of the map. Try checking your map.");
}
break;
default:
System.out.println("\nAn unknown error has occured. Please try again.");
}
player.addVisited(map.getRoom(player.getRow(), player.getColumn()));
System.out.println();
}
private static void playerAction(Player player, Map map) {
String userAction = "none";
while (!userAction.equals("cancel")) {
userAction = Utils.inputString(
"Enter your action. For a list of actions, type \"options\". To cancel, type \"cancel\": ");
System.out.println();
switch (userAction) {
case "cancel":
System.out.println();
break;
case "options":
printActionOptions();
break;
case "use item":
useItem(player);
userAction = "cancel";
break;
case "move":
movePlayer(player, map);
userAction = "cancel";
break;
default:
System.out.println("Invalid command. To view a list of options, type \"options\".\n");
}
}
}
private static void useItem(Player player) {
System.out.println(player.getInventoryString());
Item selectedItem = null;
while (selectedItem == null) {
String userItemSelect = Utils.inputString("What item do you want to use? ");
if (player.inventoryContains(userItemSelect)) {
selectedItem = player.getInventory().get(userItemSelect);
} else {
System.out.println("\nInvalid item. Please try again.\n");
}
}
System.out.println("\nWhat ability do you want to use with your item? Your options are as follows:\n");
for (String ability : selectedItem.getAbilities()) {
System.out.println("\t" + ability);
}
System.out.println();
String selectedAbility = Utils.inputString("Enter your desired ability. Type \"cancel\" to cancel. ");
while (!(selectedItem.useAbility(selectedAbility, player) || selectedAbility.equals("cancel"))) {
selectedAbility = Utils.inputString("\nInvalid ability. Please try again. ");
}
System.out.println();
}
private static void printActionOptions() {
String[] availableOptions = new String[] { "cancel", "options", "use item", "move" };
for (String option : availableOptions) {
System.out.println("\t" + option);
}
System.out.println();
}
private static void printPlayerInfo(Player player) {
System.out.println("\nCurrent Health:");
System.out.println("\t" + player.getCurrentHealth() + "/" + player.getMaxHealth() + "\n");
}
private static void npcEncounter(Map map, Player player) {
System.out.println("\nAn enemy is in this room! You can engage in combat or attempt to flee.");
System.out.println("\nThe enemies are as follows: ");
for (NonPlayerCharacter npc : map.getRoom(player.getRow(), player.getColumn()).getNpcs()){
System.out.println("\n\tnpc");
}
System.out.println();
String comabtResponse = Utils.inputString("\nWhat is your action? combat/flee: ", new String[] {"combat", "flee"}, "\nInvalid option. Please enter either \"combat\" or \"flee\": ", false);
if (comabtResponse.equals("flee")){
if (Utils.randInt(0, 3) != 0){
System.out.print("\nFlee attempt successful. ");
movePlayer(player, map);
}
else{
player.takeDamage(5);
System.out.println("\nFlee attempt failed. You have lost 5 health points.");
}
}
else{
engageCombat(map, player);
}
}
private static void engageCombat(Map map, Player player) {
System.out.println("Pick a weapon: ");
// TODO: pick weapon item
while (map.getRoom(player.getRow(), player.getColumn()).hasNpcs()) {
// TODO: combat loop
}
}
}