实现一款基于命令行的迷宫 Maze 类游戏。
![Maze](https://upload.wikimedia.org/wikipedia/commons/thumb/f/fb/Maze_2008a.JPG/220px-
Maze_2008a.JPG)
Project Overview
The Maze Game will first display a title screen that welcomes the player.
When the player presses the Enter key, the game will start.
The goal of the game is to move the + symbol from the starting point (denoted
on the maze by S ) to the maze’s exit (denoted on the maze by E ). The player
can move using the directional arrow keys (or using WASD for any PC gamers out
there). If the player successfully reaches the exit, the Maze Game will
display a victory screen.
At any point, the user can quit the game by pressing either the X or the Q
key. If the player quits the game, the Maze Game will display an exit screen.
Your Task
You will be filling in the missing parts of the file Maze.java. Specifically,
various parts of the file have the following comment:
// YOUR CODE HERE
—|—
You will be filling in all such parts of the file by replacing the comment
with your code. Many methods that you need to complete will have the following
thrown exception:
throw new UnsupportedOperationException();
—|—
Be sure to remove these thrown exceptions as you fill in the corresponding
method bodies.
Instance Variables
- private final boolean[][] mazeMatrix : Backing 2D boolean array to represent the maze internally ( true = walkable space, false = wall)
- private final int START_X : The starting point’s x-coordinate (i.e., column), where 0 denotes the left-most column
- private final int START_Y : The starting point’s y-coordinate (i.e., row), where 0 denotes the top row
- private final int END_X : The ending point’s x-coordinate (i.e., column), where 0 denotes the left-most column
- private final int END_Y : The ending point’s y-coordinate (i.e., row), where 0 denotes the top row
- private int currX : The player’s current x-coordinate (i.e., column), where 0 denotes the left-most column
- private int currY : The player’s current y-coordinate (i.e., row), where 0 denotes the top row
Constructors
- public Maze(boolean[][] mazeMatrix, int startX, int startY, int endX, int endY)
- This constructor has 5 parameters, each to be used to initialize the corresponding instance variable
- Before assigning the instance variables, it should check if the maze is valid
- What exactly does “valid” mean here? It’s up to you to figure that out!
- Think about the relationship between mazeMatrix and the start and end coordinates
- If the maze is invalid, this constructor should throw an IllegalArgumentException
- public Maze(String mazeMatrixFilename) throws FileNotFoundException
- This constructor has a single parameter, which is the filename of a maze file
- You can find example maze files in the mazes directory
- (space character) denotes a walkable space
* denotes a wall
- S denotes the starting point (which is a walkable space)
- E denotes the ending point (which is a walkable space)
- If the file does not exist, it should throw a FileNotFoundException
- Hint: Do you actually need to do anything yourself to achieve this? Or will it happen on its own?
- As it’s parsing the input maze file, it should check if the maze is valid
- Just like with the previous constructor, it’s up to you to determine how to do so
- If the maze is invalid, this constructor should throw an IllegalArgumentException
- This constructor has a single parameter, which is the filename of a maze file
- public Maze(int width, int height)
- This constructor has two parameters, a width and a height, and it must randomly generate a maze with the given dimensions
- You are free to do this in any way you please!
- The only requirement is that it must be random, so two subsequent calls to this constructor should yield two different mazes
- These resources may be helpful if you want to generate some cool random mazes
Getter Methods
- Each of the following getter methods should return the corresponding instance variable
- public boolean[][] getMazeMatrix()
- public int getStartX()
- public int getStartY()
- public int getEndX()
- public int getEndY()
- public int getCurrX()
- public int getCurrY()
Setter Methods
- Each of the following setter methods should set the corresponding instance variable
- public void setCurrX(int x)
- public void setCurrY(int y)
Other Methods
- public boolean isBlank(int x, int y)
- This method should return true if the given coordinate is a blank space in the maze, otherwise false
- Any helper methods you may want to implement!
Running the Game
Once you’ve filled in the relevant code, you can play your game using one of
the “play scripts” we have included. If you’re developing in Windows, you can
use play.bat . If you’re developing in Linux/Mac, you can use play.sh .
Specifying a Maze File
You can run the Maze Game by specifying a file, in which case the program will
use the Maze constructor that takes as input the filename of a maze file. You
would do so by calling the following from the command prompt, assuming you’ve
navigated to the Maze Game directory (replace .sh with .bat if you’re on
Windows):
./play.sh my_maze_file.txt # replace my_maze_file.txt with whatever maze file you want
Randomly Generating a Maze File
You can also run the Maze Game without any arguments, in which case the
program will use the Maze constructor that randomly generates a maze. You
would do so by calling the following from the command prompt, assuming you’ve
navigated to the Maze Game directory (replace .sh with .bat if you’re on
Windows):
./play.sh
Common Error: Permission Denied
In Linux/Mac, if you get an error like the following when you call ./play.sh:
-bash: ./test.sh: Permission denied
You may need to make the play.sh script executable via the following command:
chmod a+x test.sh
If you can’t get it running, be sure to seek help from a tutor in the labs!
Submitting Your Code
The only file you will need to submit is Maze.java . To do so, log into
codePost. You should see an assignment called “Midterm Project: MazeGame” (it
should be the top item). In the “Upload” column, you should see a green
“Upload files” button.