-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGame.java
More file actions
286 lines (274 loc) · 18.9 KB
/
Game.java
File metadata and controls
286 lines (274 loc) · 18.9 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
package org.uob.a1;
import java.util.Scanner;
public class Game {
public static void main(String args[])
{
//Asking the player their name
System.out.println("Please enter your name: ");
Scanner nameInput = new Scanner (System.in);
String name = nameInput.nextLine();
//Outputs the introductory method to player
System.out.println("Welcome to Santa's House,feel free to explore as many rooms and complete as many puzzles as possible. \nOnce a room has been visited, it will appear on the map(using a letter) \nThe map key: \nO=Outside\nH=Hallway\nE=Elf bedroom\nG=Santa's Grotto\nK=Kitchen\nL=Lounge\nD=Dining room\nR=Reindeer Stable\nP=Present storage\nT=Toy factory\nS=Sleigh garage \nRemember: You loose points for visiting rooms so try to avoid re-visiting rooms as much as possible. In addition, I must look at a thing before you try and do anything else with it. \nGood luck and merry christmas!");
//Declares the variable for the main game loop
boolean run = true;
//Initialises the player object (for this specific player)
Player player = new Player(name, "easy");
//I have assigned the arrays to these variables, as they are used lots in my program so this saved me having to access the player class every time they are used
Room[][] rooms = player.getRooms();
Item[] items = player.getItems();
//Places the room the player start in on their map, and outputs the description of the room to the terminal
player.map.placeRoom(player.position, 'O');
System.out.println((rooms[player.position.x][player.position.y]).getDescription());
//The main game loop
while (run)
{
//This line is used to seperate the different commands and outputs so the terminal is easier to read
System.out.println("\n>>>>>>>>>>>>>>>\n");
//Takes a new input from the user and sanitise it
Scanner nInput = new Scanner (System.in);
String rInput = (nInput.nextLine()).toLowerCase();
rInput.trim();
String action = "";
String object = "empty";
if (rInput.contains("\s"))
{
String[] command = rInput.split("\s");
action = command[0];
if (command.length > 1)
object = command[1];
}
else
{
action = rInput;
}
//This switch statement looks at the 'action' part of the input to decide the outcome
switch (action){
case "look":
if (object.equals("empty"))
{
//If no 'object' is entered the room description is outputted
System.out.println((rooms[player.position.x][player.position.y]).getDescription());
}
//If an 'object' is entered this section is run
else
{
//Check if the object entered is a feature in the room you currently are in
if (object.equals(rooms[player.position.x][player.position.y].getFeature()))
{
player.haveSeen(object);
System.out.println(rooms[player.position.x][player.position.y].getFdescription());
}
//Checks if the object entered is an item
else if (player.isItem(object))
{
//Uses a method in the player class to return the item object that is being mentioned
Item theItem = player.getItem(object);
//Checks if the player is in the correct room to look at that item (this stops the player from cheating at the game)
if ((theItem.position.x == player.position.x) && (theItem.position.y == player.position.y))
{
//This sets the boolean value for looking at the item to true (will be used when item is taken)
player.haveSeen(object);
//Outputs the decription of the item to the player
System.out.println(theItem.getDescription(player));
}
else
{
//If the position of the player is not the same as the object, but the object is an item. The only problem is that they are in rhe wrong room
System.out.println("Sorry you can't look at that at the moment, you are in the wrong room. ");
//The player has entered a command they are not ready for yet so they lose 5 points
player.score.fiveLost();
}
}
else
{
//If the player enters an object which is not a feature or an item this is outputted
System.out.println("Sorry you can't look at that. ");
//The player has entered a command they are not ready for yet so they lose 5 points
player.score.fiveLost();
}
}
break;
case "inventory":
//Prints out the contents of the player's inventory using the pScore object
System.out.println("Your inventory: \n" + player.inventory.displayInventory());
break;
case "score":
//Prints the player's score using the pScore object
System.out.println("Your score is: " + player.score.getScore());
break;
case "map":
//Prints the map (of the rooms the player has discovered so far)
System.out.println("Map: \n" + player.map.display());
break;
case "help":
//If the player is stuck, this reminds them of the available commands
System.out.println("The valid commands are: \n 'look', 'move *direction*', 'look *feature*', 'look *item*', 'help', 'inventory', 'map', 'score', 'progress' and 'quit'");
break;
case "quit":
//If the player wants to end the game this will stop the main game loop from running (after this iteration)
run = false;
break;
case "move":
//Uses the player method 'move' to update the player's position
player.move(object);
//Adds the symbol of the room the player is in to their map
player.map.placeRoom(player.position, rooms[player.position.x][player.position.y].getSymbol());
//The description of the room the player is in is displayed (this is done even if the move they chose is invalid so that they are reminded which room they are in
System.out.println(rooms[player.position.x][player.position.y].getDescription());
break;
case "take":
if (object.equals("empty"))
{
//If no 'object' is entered, this is outputted, and they lose five points for an 'invalid command'
System.out.println("You did not specify which item you want to take. ");
player.score.fiveLost();
break;
}
//If an 'object' is entered this section is run
Boolean validItem = false;
//First, I check if the 'object' is an item using a for loop to compare each item (using the items array) to the 'object' input from the player
for (int i = 0; i < items.length; i++)
{
if (items[i].getName().equals(object))
{
//If the 'object' has the same name as the item currently being checked using the for loop, and creates a boolean variable to store the outcome
validItem = true;
//This checks that the player and the item are in the same room
//I chose to do this so that the player cannot cheat and enter the necessary commands to complete the game one after another in the same room
if (player.position.x == items[i].position.x && player.position.y == items[i].position.y)
{
//This checks if the player has completed all of the the relevant prerequisites to be allowed to do the puzzle
if (items[i].readyForPuzzle((rooms[player.position.x][player.position.y]).getName(), player))
{
//Using the method in the items class we output the puzzle question
System.out.println((items[i]).getPuzzle());
//This accepts a new input (answer to the puzzle)
//I have done this here instead of breaking out of the game loop because this adds more saftey so that the player can only answer the puzzle if they have the correct prerequisites, and I could not import new modules into the other class files
nInput = new Scanner (System.in);
rInput = (nInput.nextLine()).toLowerCase();
//This if statement checks if the plater's input is correct using a method in the item
if (items[i].checkAnswer(rInput))
{
//If the player got the correct answer, they gain points, get the item added to their inventory and the correct boolean attributes in the player class are updated
player.score.solvePuzzle();
player.inventory.addItem(object);
player.updateStatus(object);
System.out.println("You have just added " + object + " to your inventory. ");
break;
}
else
{
//The answer to the puzzle is incorrect
System.out.println("Sorry that is incorect.\nTo try again enter the same command again. ");
//The player has entered an incorrect answer they lose 5 points
player.score.fiveLost();
break;
}
}
else
{
//If the player has not done all of the prequisites then a message is outputted
System.out.println("You are not ready for the puzzle yet. ");
//The player has entered a command they are not ready for yet so they lose 5 points
player.score.fiveLost();
break;
}
}
else
{
//If the player is in the wrong room to take the item, a message is outputted
System.out.println("You are in the wrong room. ");
//The player has entered a command they are not ready for yet so they lose 5 points
player.score.fiveLost();
break;
}
}
}
//If the player entered an 'object' which cannot be taken, this section of code is run
if (!validItem)
{
if ((rooms[player.position.x][player.position.y]).getFeature().equals(object))
{
//If the player tries to take a feature, this message is outputted
System.out.println("You can't take " + object + " but you can look!\n");
//The player has entered an invalid command so they lose 5 points
player.score.fiveLost();
}
else
{
//If the player enters an unrecognisable 'object' or they are in the incorrect room, this message is outputted
System.out.println("There isn't a " + object + " in the " + (rooms[player.position.x][player.position.y]).getName() + ". Keep looking\n");
//The player has entered a command they are not ready for yet so they lose 5 points
player.score.fiveLost();
}
//The description of the room the player is in is outputted (since it includes the valid commands for the room)
System.out.println(rooms[player.position.x][player.position.y].getDescription());
}
break;
case "use":
//This is an additional feature, which allows the player to 'use' a key to access more of the game
//I have done this because the only item in the game that you can use is the 'key'
if ((object.equals("key")) && rooms[player.position.x][player.position.y].getName().equals("Wrapping room") && player.inventory.hasItem("key") > -1 && player.lookDoor)
{
//If the player has entered the 'object' key, they are in the correct room, and have the key in their inventory, the key is used and the door is unlocked
//This is a saftey feature, as it makes sure they cannot reuse the key
player.inventory.removeItem("key");
System.out.println("You have used your key and unlocked the door, this means that the key has been removed from your inventory. ");
//Assigning the relevant boolean player attribute
player.usedKey = true;
//Outputs the description for the new part of the room which has been revealed (since the door is unlocked)
Item currentItem = player.getItem("letters");
System.out.println(currentItem.getDescription(player));
}
else
{
//If the player does not meet all of the required prerequisites this message is outputted
System.out.println("Sorry you cannot use that at the moment. ");
//The player has entered a command they are not ready for yet so they lose 5 points
player.score.fiveLost();
}
break;
case "give":
//This is an additional feature, which allows the player to 'give' their inventory to santa to complete the game
//I have done this because the only item in the game that you can 'give' is the inventory
if (object.equals("inventory") && rooms[player.position.x][player.position.y].getName().equals("Sleigh garage") && player.hPuzzle == true && player.wPuzzle == true && player.kPuzzle == true && player.gPuzzle == true && player.lookSleigh == true)
{
//If the player has entered the 'object' inventory, they are in the correct room, done all of the puzzles in the game and looked at the sleigh, they are able to give the relevant items to santa and complete the game
System.out.println("Santa says, 'thank you this has reminded me of all the people who need me and even though I made one mistake, christmas must go on. \nI will wrap this present myself so that I can deliver all of the children in the world presents tonight!");
//Increases the players score by 100 because they have completed the game
player.score.foundSanta();
//Assigns the correct boolean value to the attribute of the player
player.completed = true;
//Changes the value of the variable which is used to control the main game loop so that it stops running
run = false;
}
else
{
//If the player has not completed the required prerequisites then this message is outputted
System.out.println("Sorry you are not ready to do this yet, try and explore some more. ");
//The player has entered a command they are not ready for yet so they lose 5 points
player.score.fiveLost();
}
break;
case "progress":
//This is an additional feature, which tells the player how many of the stages of the game they have completed, by calling a method in the player class
System.out.println("You have completed " + player.checkProgress() + " parts out of 11");
break;
default:
//If an unrecognised 'action' is entered the help message is outputted
System.out.println("The valid commands are: \n 'look', 'move *direction*', 'look *feature*', 'look *item*', 'help', 'inventory', 'map', 'score', 'progress' and 'quit'");
//The player has entered an invalid command so they lose 5 points
player.score.fiveLost();
break;
}
}
//When the main game loop stops running, this message is sent
System.out.println("End of game. ");
//There is a special message if the player managed to complete the game, which also tells them their score
if (player.completed == true)
{
System.out.println(player.winnerConfirmation());
System.out.println("Well done "+ name + "! you completed the challenges and FOUND STANTA. \n Your final score was "+ player.score.getScore());
}
}
}