-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMazeGame.java
476 lines (444 loc) · 15.3 KB
/
MazeGame.java
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
import java.io.IOException;
import java.util.Scanner;
import java.util.ArrayList;
import java.io.File;
import java.util.Arrays;
import java.io.FileNotFoundException;
import java.io.PrintWriter;
/**
* Maze Game
*
* INFO1113 Assignment 1
* 2018 Semester 2
*
* The Maze Game.
* In this assignment you will be designing a maze game.
* You will have a maze board and a player moving around the board.
* The player can step left, right, up or down.
* However, you need to complete the maze within a given number of steps.
*
* As in any maze, there are walls that you cannot move through. If you try to
* move through a wall, you lose a life. You have a limited number of lives.
* There is also gold on the board that you can collect if you move ontop of it.
*
* Please implement the methods provided, as some of the marks are allocated to
* testing these methods directly.
*
* @author YOU :)
* @date 23 August 2018
*
*/
public class MazeGame {
/* You can put variables that you need throughout the class up here.
* You MUST INITIALISE ALL of these class variables in your initialiseGame
* method.
*/
// A sample variable to show you can put variables here.
// You would initialise it in initialiseGame method.
// e.g. Have the following line in the initialiseGame method.
// sampleVariable = 1;
static int lives;
static int steps;
static int gold;
static int rows;
static ArrayList<String[]> maze;
static int destination_x;
static int destination_y;
static String save_file;
/**
* Initialises the game from the given configuration file.
* This includes the number of lives, the number of steps, the starting gold
* and the board.
*
* If the configuration file name is "DEFAULT", load the default
* game configuration.
*
* NOTE: Please also initialise all of your class variables.
*
* @args configFileName The name of the game configuration file to read from.
* @throws IOException If there was an error reading in the game.
* For example, if the input file could not be found.
*/
public static void initialiseGame(String configFileName) throws IOException {
save_file = "";
File f = new File(configFileName);
try {
Scanner scan = new Scanner(f);
String[] title = scan.nextLine().split(" ");
lives = Integer.parseInt(title[0]);
steps = Integer.parseInt(title[1]);
gold = Integer.parseInt(title[2]);
rows = Integer.parseInt(title[3]);
maze = new ArrayList<>();
while (scan.hasNextLine()) {
maze.add(scan.nextLine().split(""));
}
for (int i = 0; i < maze.size() ; i++) {
for (int j = 0; j < maze.get(i).length; j++) {
if (maze.get(i)[j].equals("@")) {
destination_y = i;
destination_x = j;
}
}
}
if (maze.size() != rows) {
throw new IOException();
}
}
catch (FileNotFoundException e) {
throw new IOException();
}
}
/**
* Save the current board to the given file name.
* Note: save it in the same format as you read it in.
* That is:
*
* <number of lives> <number of steps> <amount of gold> <number of rows on the board>
* <BOARD>
*
* @args toFileName The name of the file to save the game configuration to.
* @throws IOException If there was an error writing the game to the file.
*/
public static void saveGame(String toFileName) throws IOException {
save_file = toFileName;
File f = new File(toFileName);
try {
PrintWriter writer = new PrintWriter(f);
writer.println(lives + " " + steps + " " + gold + " " +rows);
for (String[] x : maze) {
for (String s : x) {
writer.print(s);
}
writer.println();
}
writer.close();
System.out.printf("Successfully saved the current game configuration to '%s'.\n", toFileName);
}
catch (FileNotFoundException e) {
throw new IOException();
}
}
/**
* Gets the current x position of the player.
*
* @return The players current x position.
*/
public static int getCurrentXPosition() {
int y = getCurrentYPosition();
for (int i = 0; i < maze.get(y).length ; i++) {
if (maze.get(y)[i].equals("&")) {
return i;
}
}
//System.out.println("bugs in x func");
return -1;
}
/**
* Gets the current y position of the player.
*
* @return The players current y position.
*/
public static int getCurrentYPosition() {
for (int i = 0 ; i < maze.size() ; i++) {
ArrayList<String> row = new ArrayList<>(Arrays.asList(maze.get(i)));
if (row.contains("&")) {
return i;
}
}
//System.out.println("bugs in y func");
return -1;
}
/**
* Gets the number of lives the player currently has.
*
* @return The number of lives the player currently has.
*/
public static int numberOfLives() {
return lives;
}
/**
* Gets the number of remaining steps that the player can use.
*
* @return The number of steps remaining in the game.
*/
public static int numberOfStepsRemaining() {
return steps;
}
/**
* Gets the amount of gold that the player has collected so far.
*
* @return The amount of gold the player has collected so far.
*/
public static int amountOfGold() {
return gold;
}
/**
* Checks to see if the player has completed the maze.
* The player has completed the maze if they have reached the destination.
*
* @return True if the player has completed the maze.
*/
public static boolean isMazeCompleted() {
if (getCurrentYPosition() == destination_y && getCurrentXPosition() == destination_x) {
return true;
}
return false;
}
/**
* Checks to see if it is the end of the game.
* It is the end of the game if one of the following conditions is true:
* - There are no remaining steps.
* - The player has no lives.
* - The player has completed the maze.
*
* @return True if any one of the conditions that end the game is true.
*/
public static boolean isGameEnd() {
if (lives <= 0 || steps <= 0) {
return true;
}
return false;
}
//This function check whether it's a wall
public static boolean isWall(int x, int y) {
if (maze.get(y)[x].equals("#")) {
return true;
}
return false;
}
/**
* Checks if the coordinates (x, y) are valid.
* That is, if they are on the board.
*
* @args x The x coordinate.
* @args y The y coordinate.
* @return True if the given coordinates are valid (on the board),
* otherwise, false (the coordinates are out or range).
*/
public static boolean isValidCoordinates(int x, int y) {
if (y >= maze.size() || y < 0) {
return false;
}
if (x >= maze.get(0).length || x < 0) {
return false;
}
return true;
}
/**
* Checks if a move to the given coordinates is valid.
* A move is invalid if:
* - It is move to a coordinate off the board.
* - There is a wall at that coordinate.
* - The game is ended.
*
* @args x The x coordinate to move to.
* @args y The y coordinate to move to.
* @return True if the move is valid, otherwise false.
*/
public static boolean canMoveTo(int x, int y) {
if (!isGameEnd() && isValidCoordinates(x, y) && !isWall(x, y)) {
return true;
}
return false;
}
/**
* Move the player to the given coordinates on the board.
* After a successful move, it prints "Moved to (x, y)."
* where (x, y) were the coordinates given.
*
* If there was gold at the position the player moved to,
* the gold should be collected and the message "Plus n gold."
* should also be printed, where n is the amount of gold collected.
*
* If it is an invalid move, a life is lost.
* The method prints: "Invalid move. One life lost."
*
* @args x The x coordinate to move to.
* @args y The y coordinate to move to.
*/
public static void moveTo(int x, int y) {
if ( canMoveTo(x, y)) {
System.out.printf("Moved to (%d, %d).\n", x, y);
maze.get(getCurrentYPosition())[getCurrentXPosition()] = ".";
int raise_gold = 0;
try {
raise_gold += Integer.parseInt(maze.get(y)[x]);
System.out.printf("Plus %s gold.\n", raise_gold);
gold += raise_gold;
}
catch(NumberFormatException nef) {
raise_gold += 0;
}
maze.get(y)[x] = "&";
}
else {
System.out.println("Invalid move. One life lost.");
lives -= 1;
}
steps -= 1;
}
/**
* Prints out the help message.
*/
public static void printHelp() {
System.out.println("Usage: You can type one of the following commands.");
System.out.println("help Print this help message.");
System.out.println("board Print the current board.");
System.out.println("status Print the current status.");
System.out.println("left Move the player 1 square to the left.");
System.out.println("right Move the player 1 square to the right.");
System.out.println("up Move the player 1 square up.");
System.out.println("down Move the player 1 square down.");
System.out.println("save <file> Save the current game configuration to the given file.");
}
/**
* Prints out the status message.
*/
public static void printStatus() {
System.out.println("Number of live(s): " + lives);
System.out.println("Number of step(s) remaining: " + steps);
System.out.println("Amount of gold: " + gold);
}
/**
* Prints out the board.
*/
public static void printBoard() {
for (String[] x : maze) {
for (String s : x) {
System.out.print(s);
}
System.out.println();
}
}
/**
* Performs the given action by calling the appropriate helper methods.
* [For example, calling the printHelp() method if the action is "help".]
*
* The valid actions are "help", "board", "status", "left", "right",
* "up", "down", and "save".
* [Note: The actions are case insensitive.]
* If it is not a valid action, an IllegalArgumentException should be thrown.
*
* @args action The action we are performing.
* @throws IllegalArgumentException If the action given isn't one of the
* allowed actions.
*/
public static void performAction(String action) throws IllegalArgumentException {
String[] command = action.split(" ");
if (command.length == 1) {
if (command[0].equalsIgnoreCase("help")) {
printHelp();
}
else if (command[0].equalsIgnoreCase("board")) {
printBoard();
}
else if (command[0].equalsIgnoreCase("status")) {
printStatus();
}
else if (command[0].equalsIgnoreCase("left")) {
moveTo(getCurrentXPosition() - 1, getCurrentYPosition());
}
else if (command[0].equalsIgnoreCase("right")) {
moveTo(getCurrentXPosition() + 1, getCurrentYPosition());
}
else if (command[0].equalsIgnoreCase("up")) {
moveTo(getCurrentXPosition(), getCurrentYPosition() - 1);
}
else if (command[0].equalsIgnoreCase("down")) {
moveTo(getCurrentXPosition(), getCurrentYPosition() + 1);
}
else if (command[0].equalsIgnoreCase("down")) {
moveTo(getCurrentXPosition(), getCurrentYPosition() + 1);
}
else if (command[0].equals("")) {
//Do nothing.
}
else {
throw new IllegalArgumentException();
}
}
else if (command.length == 2) {
if (command[0].equalsIgnoreCase("save")) {
try {
saveGame(command[1]);
}
catch (IOException e) {
System.out.printf("Error: Could not save the current game configuration to '%s'.\n", command[1]);
}
}
else {
throw new IllegalArgumentException();
}
}
else {
throw new IllegalArgumentException();
}
}
public static void gameEndsType() {
if (isMazeCompleted()) {
// Complete the maze.
System.out.println("Congratulations! You completed the maze!");
System.out.println("Your final status is:");
printStatus();
}
else { // Do not complete the maze.
if (isGameEnd()) { // End because no lives or steps.
if (lives <= 0 && steps <= 0) {
System.out.println("Oh no! You have no lives and no steps left.");
System.out.println("Better luck next time!");
}
else if (lives <= 0) {
System.out.println("Oh no! You have no lives left.");
System.out.println("Better luck next time!");
}
else if (steps <= 0) {
System.out.println("Oh no! You have no steps left.");
System.out.println("Better luck next time!");
}
}
else { // End because EOF.
if ((destination_x != getCurrentXPosition() || destination_y != getCurrentYPosition())) {
System.out.println("You did not complete the game.");
}
}
}
}
/**
* The main method of your program.
*
* @args args[0] The game configuration file from which to initialise the
* maze game. If it is DEFAULT, load the default configuration.
*/
public static void main(String[] args) {
// Run your program (reading in from args etc) from here.
if (args.length == 1) {
try {
Scanner scan = new Scanner(System.in);
initialiseGame(args[0]);
while (!isMazeCompleted() &&!isGameEnd() && scan.hasNextLine()) {
String input = scan.nextLine();
try {
performAction(input);
}
catch (IllegalArgumentException e) {
System.out.printf("Error: Could not find command '%s'.\n", input);
System.out.println("To find the list of valid commands, please type 'help'.");
}
}
gameEndsType();
}
catch (IOException e) {
System.out.printf("Error: Could not load the game configuration from '%s'.\n", args[0]);
}
}
else if (args.length < 1){
System.out.printf("Error: Too few arguments given. Expected 1 argument, found %d.\n", args.length);
System.out.println("Usage: MazeGame [<game configuration file>|DEFAULT]");
}
else {
System.out.printf("Error: Too many arguments given. Expected 1 argument, found %d.\n", args.length);
System.out.println("Usage: MazeGame [<game configuration file>|DEFAULT]");
}
}
}