Maze game in Java (Eclipse) - Please follow all directions - Use JavaFX Describt
ID: 3863139 • Letter: M
Question
Maze game in Java (Eclipse)
- Please follow all directions
- Use JavaFX
Describtion:
You should create a maze game. The aim of the game is to get out of the maze. The game will read inn maze from a text file with the format specified below. I'm going to lay out some such as mazes, but you can feel free to make your own as well. Here is an example of a file that specifies a maze:
7
6
##### - #
# * # # #
# # #
### # #
# #
Files that specifies a maze should start with two numbers on each line. The numbers indicate the width and height of the maze. Example file indicates that the maze will be 7 squares wide and 6 squares high. Then comes the maze. Each line of the text file is a row in the maze and each character in the text file marks what should be included in a particular cell in the maze. "#" Character marks a wall, a blank character marks an opening (hallway), "*" indicates where the player starts and "-" marks the end. An example of how the game might look like based on sample file stated above are given in Figure 1. The left is so game starts and the right is such a game is right before the player wins.
Tasks:
Create an abstract class "MazeRoute" representing a route in the maze. All maze routes should store the position on the board (x-and y-coordinates). The class should declare two abstract methods. One method to move the player to the route, and one method that returns a reference to the "type" of route.
Then create concrete subclasses for the three types of routes (wall, hallway and finishline). The hallway moves the player, the wall class makes the player stand in the previous route(cant move into a wall), and if the player moves to the finishline, the player wins and program should stop. You choose if the look of the routes are easy forms like javafx.scene.shape.Rectangle, or more advanced selfdrawn forms with Canvas, or pictures that you show off with ImageView objects.
Create a new class "Player" which stores the position to the player on the board. Then create a method that reads a text file and create a two-dimensional array of maze routes based on the scanned file.
Create a Java FX main class that displays the maze. Then make a Event Handler responsive to arrow keys by trying to move the player in direction of the arrow. Then finish by register keylistener in Scene object to let it be applicable for the entire game.
MExplanation / Answer
I have taken this as sample input
2 4
R.XX.
.X...
...X.
XXT..
..X..
Program for the maze game
----------------------------------------
//Importing packages
import java.util.*;
import java.io.File;
public class MazeGame {
public static void main(String[] args) throws Exception {
Scanner scan = new Scanner(new File("maze.txt"));
Scanner user = new Scanner(System.in);
int rows = scan.nextInt();
int columns = scan.nextInt();
int px = 0;
int py = 0;
String [][] maze = new String[rows][columns];
String junk = scan.nextLine();
for (int i = 0; i < rows; i++){
String temp = scan.nextLine();
String[] arrayPasser = temp.split("");
for (int j = 0; j < columns; j++){
maze[i][j] = arrayPasser[i];
}
}
boolean gotTreasure = false;
while (gotTreasure == false){
for (int i = 0; i < rows; i++){
for (int j = 0; j < columns; j++){
System.out.print(maze[i][j]);
System.out.print(" ");
}
System.out.print(" ");
}
System.out.printf(" ");
System.out.println("You may:");
System.out.println("Move up");
System.out.println("Move down");
System.out.println("Move left");
System.out.println("Move right");
System.out.println("Quit");
int choice = user.nextInt();
int i = 0;
if (choice == 1 && i >= 0 && i < columns){
for (int k = 0; k < rows; k++){
for (int l = 0; l < columns; l++){
if (maze[k][l].equals(maze[px][py]) && maze[px][py-1].equals("X") == false){
maze[px][py] = ".";
maze[k][l-1] = "P";
maze[px][py] = maze[k][l-1];
}else if (maze[px][py-1] == "X"){
System.out.println("Cannot move into a cave-in! Try something else.");
}else {
continue;}
}
}
}
else if (choice == 2 && i >= 0 && i < columns){
for (int k = 0; k < rows; k++){
for (int l = 0; l < columns; l++){
if (maze[k][l].equals(maze[px][py]) && maze[px][py+1].equals("X") == false){
maze[px][py] = ".";
maze[k][l+1] = "P";
maze[px][py] = maze[k][l+1];
}else if (maze[px][py+1] == "X"){
System.out.println("Cannot move into a cave-in! Try something else.");
}else {
continue;}
}
}
}
else if (choice == 3 && i >= 0 && i < columns){
for (int k = 0; k < rows; k++){
for (int l = 0; l < columns; l++){
if (maze[k][l].equals(maze[px][py]) && maze[px-1][py].equals("X") == false){
maze[px][py] = ".";
maze[k-1][l] = "P";
maze[px][py] = maze[k-1][l];
}else if (maze[px-1][py] == "X"){
System.out.println("Cannot move ! Try something else.");
}else {
continue;}
}
}
}
else if (choice == 4 && i >= 0 && i < columns){
for (int k = 0; k < rows; k++){
for (int l = 0; l < columns; l++){
if (maze[k][l].equals(maze[px][py]) && maze[px+1][py].equals("X") == false){
maze[px][py] = ".";
maze[k+1][l] = "P";
maze[px][py] = maze[k+1][l];
}else if (maze[px+1][py] == "X"){
System.out.println("Cannot move ! Try something else.");
}else {
continue;}
}
}
}
else if (choice == 0){
System.exit(0);
}
}
System.out.println("Congratulations You Found the treasure!");
scan.close();
user.close();
}
}