The language used is Java This lab is due in 2 days, and I am lost, please help.
ID: 3827770 • Letter: T
Question
The language used is Java
This lab is due in 2 days, and I am lost, please help.
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!). 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.*;
import java.util.Arrays;
public class Maze
{
private int mazeSize=10,blocks=55;
public int playerX,playerY,exitX,exitY;
public char movement;
public int hintProbability=33; //imagine its a % out of 100
public int[][] mazeArray = new int[10][10];
Random generator = new Random();
public Maze(int blocks)
{
blocks=this.blocks;
do{
for(int i=0;i<mazeSize-1;i++)
{
for(int j=0;j<mazeSize-1;j++)
{
mazeArray[i][j]=0;
}
}
for(int i=0;i<blocks;i++)
{
int randX = generator.nextInt(mazeSize);
int randY = generator.nextInt(mazeSize);
mazeArray[randX][randY]=1;
}
int playerX = generator.nextInt(mazeSize);
int playerY = generator.nextInt(mazeSize);
mazeArray[playerX][playerY]=2;
do{
int exitX = generator.nextInt(mazeSize);
int exitY = generator.nextInt(mazeSize);
mazeArray[exitX][exitY]=3;
}
while(exitX==playerX && exitY==playerY);
}
while(!canItBeDone());
}
public String toString()
{
String printer="";
for (int i=0;i<mazeSize;i++)
{
for (int j=0;j<mazeSize;j++)
{
printer+=(" " + mazeArray[i][j]);
}
printer+=(" ");
}
return printer;
}
public String move(char direction)
{
movement=Character.toUpperCase(direction);
switch(movement)
{
case 'N':if(!validMove(playerX,playerY+1))
return "Blocked";
else
{ playerY++;
return "OK";
}
case 'S': if(!validMove(playerX,playerY-1))
return "Blocked";
else
{ playerY--;
return "OK";
}
case 'E': if(!validMove(playerX+1,playerY))
return "Blocked";
else
{ playerX++;
return "OK";
}
case 'W': if(!validMove(playerX-1,playerY))
return "Blocked";
else
{ playerY--;
return "OK";
}
default:
return "Not a valid input";
}
}
public String hint()
{
double shouldi = generator.nextDouble();
if((shouldi*100)<=hintProbability)
{
int spacesX=(playerX-exitX);
int spacesY=(playerY-exitY);
return ("You are ["+spacesX+","+spacesY+"] away from the exit");
}
else
{
return "";
}
}
boolean canItBeDone(){
return solve(playerX,playerY);
}
public boolean finished()
{
return(solve(playerX,playerY));
}
public boolean atEnd(int curx,int cury)
{
if (mazeArray[curx][cury]==3)
return true;
else
return false;
}
public boolean validMove(int x,int y)
{
if(x<0 || y<0 || y>=mazeSize || x>=mazeSize || mazeArray[x][y] == 1 || mazeArray[x][y] == 8)
return false;
return true;
}
public boolean solve(int x,int y)
{
if(validMove(x,y)==true){
if(atEnd(x,y)==true){
mazeArray[x][y]=5;
return true;
}
else{
mazeArray[x][y]=8;
if(validMove(x+1,y)){
mazeArray[x+1][y]=8;
solve(x+1,y);}
if(validMove(x-1,y)){
mazeArray[x-1][y]=8;
solve(x-1,y);}
if(validMove(x,y+1)){
mazeArray[x][y+1]=8;
solve(x,y+1);}
if(validMove(x,y-1)){
mazeArray[x][y-1]=8;
solve(x,y-1);}
}
}
return false;
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.