This is java. Creating a 2D array Maze In this lab, you are going to create a gr
ID: 3840493 • Letter: T
Question
This is java.
Creating a 2D array Maze In this lab, you are going to create a graphical maze based on a file that is read in and stored in a 2D array. First, you must create at least two files that are at least 10x5 of ones and zeros. When your program first starts, it should randomly choose between the two files and then display the maze Based on the ones and zeros, they should show a block or space. (You should use an image to show a block (e.g. trees, bricks, stones.. your choice!) It might look something like this Then, you should create a character that you can move through the maze. If they run into a block, they should not be able to move. If it is empty, they should be able to move freely. Specifics 1. Create a GUI program that has a background of your choice 2. You should create a 10x5 maze on the background (by using a 2-dimensional array) 3. Generate the maze from a randomly chosen file (You need at least two files). 4. The blocks should be an image 5. You are to create player image that can be moved based on the keyboard 6. Once they get to the end, they should see a message that says they won Extra Credit: Create items that can be collected and increment the score if the player gathers the item (i.e. collides with them). Do not forget to display their score.Explanation / Answer
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Stack;
public class Maze {
public static void main(String[] args) {
int[][] maze = new int[6][6];
maze[0] = new int[] { 1, 1, 0, 0, 0, 0 };
maze[1] = new int[] { 0, 1, 0, 0, 0, 0 };
maze[2] = new int[] { 0, 1, 0, 1, 1, 1 };
maze[3] = new int[] { 1, 1, 1, 1, 0, 1 };
maze[4] = new int[] { 0, 1, 0, 0, 0, 1 };
maze[5] = new int[] { 0, 1, 0, 0, 0, 1 };
Point start = new Point(0,0);
Point exit = new Point(5,5);
Stack<Point> s = new Stack<Point>();
s.push(start);
Map<Point,Status> visited = new HashMap<Point,Status>();
visited.put(start, Status.VISITING);
while(!s.isEmpty()){
Point currentP = s.peek();
boolean isAllVisited = true;
for (Point p : getAdjacent(currentP.x, currentP.y)){
if (visited.get(p)==null && maze[p.y][p.x]==1){
visited.put(p, Status.VISITING);
isAllVisited = false ;
s.push(p);
if ( p.equals(exit)){
printPath(s);
}
break;
}
}
if (isAllVisited){
visited.put(currentP, Status.VISITED);
s.pop();
}
}
}
private static void printPath(Stack<Point> s){
while (!s.isEmpty()){
System.out.print("->");
Point p = s.remove(0);
System.out.format("[%d,%d]",p.x,p.y);
}
}
private static List<Point> getAdjacent(int x, int y){
List<Point> result = new ArrayList<Point>();
if (y-1>=0) {
result.add(new Point(x, y-1));
};
if (x+1<=5) {
result.add(new Point(x+1, y));
};
if (y+1<=5) {
result.add(new Point(x, y+1));
};
if (x-1>=0) {
result.add(new Point(x-1, y));
};
return result;
}
private static class Point {
int x, y;
public Point(int x,int y) {
this.x = x;
this.y = y;
}
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + x;
result = prime * result + y;
return result;
}
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Point other = (Point) obj;
if (x != other.x)
return false;
if (y != other.y)
return false;
return true;
]
private static enum Status {
UNVISITED, VISITING, VISITED
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.