Need Help with this JAVA MAZE RUNNER PROJECT.. Can you please help me with the c
ID: 3599018 • Letter: N
Question
Need Help with this JAVA MAZE RUNNER PROJECT.. Can you please help me with the code and also add comments so I understand the situation.. A sample output is also attached so that you can check your code. **** Please see the sample output, I need to have the same output****
You are working for a software company to develop a new game that involves characters exploring a world. The world is represented as a two dimensional array. In each cell of the array, a monster capable of a certain amount of damage (represented as an integer value) is present. A player starts out in some cell of the two-dimensional array and progresses to the next level by reaching any cell on the edge of the array. On each turn, a player can move north, south, east or west (but not diagonally).
Your task for this project is to write a basic AI character for this game, as follows:
Your program should begin by prompting for the number of rows and columns in the world, and the danger level of each cell in the world.
The program should then prompt for the starting x and y coordinate (i.e. row and column) of the AI character.
On each turn, your program should display the state of the world (i.e. the two dimensional array) with the AI character’s location marked with an *.
The AI should decide which way to move simply by looking at the cells to the north, south, east and west and moving towards the one with the smallest amount of danger.
The AI character should not return to a cell that it has already visited. Hint: This can be done using a String variable. Every time the character is in a new position, add this position to the string variable. That way, you can use the String contains method to check if a position has already been visited.
When the character reaches the edge of the world, the program should stop, and display the place the character exited the world and the total amount of danger the character faced on its way through the world. Don’t forget to include the danger faced in the character’s starting location.
Your project will be graded based on the above requirements (each requirement is worth one point).
Output- Sample (to check if the code is correct or not
How many rows are in the maze? 5
How many columns are in the maze? 7
Enter the danger in row 0, separated by spaces: 0 1 4 2 6 0 1
Enter the danger in row 1, separated by spaces: 1 7 4 2 2 6 0
Enter the danger in row 2, separated by spaces: 0 0 0 8 1 4 1
Enter the danger in row 3, separated by spaces: 1 1 2 7 3 4 2
Enter the danger in row 4, separated by spaces: 5 1 6 4 2 2 1
Enter the starting x coordinate: 2
Enter the starting y coordinate: 4
0142601
1742260
0008*41
1127342
5164221
Moving to 1,4 (danger level 2)
0142601
1742*60
0008141
1127342
5164221
Moving to 1,3 (danger level 2)
0142601
174*260
0008141
1127342
5164221
Moving to 0,3 (danger level 2)
014*601
1742260
0008141
1127342
5164221
Exited the world at 0,3 Total danger faced: 7
Explanation / Answer
//source code
package chegg;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.util.Scanner;
public class game_danger_world {
public static void displayBoard(int arr[][], int row, int col, int cur_row, int cur_col) //display the matrix
{
int i,j;
for(i=0;i<row;i++)
{
for(j=0;j<col;j++)
{
if(i==cur_row && j==cur_col)
{
System.out.print("* ");
}
else
System.out.print(arr[i][j]+" ");
}
System.out.println("");
}
System.out.println("");
}
public static void main(String[] args) throws FileNotFoundException {
// TODO Auto-generated method stub
int row;
int col;
Scanner scanner = new Scanner(System.in);
System.out.println("How many rows are in the maze?");
row = scanner.nextInt();
System.out.println("How many columns are in the maze?");
col = scanner.nextInt();
int arr[][] = new int[row][col];
int cur_row=0, cur_col=0;
//System.out.println(row + " " + col);
int i,j;
for(i=0;i<row;i++)
{
System.out.println("Enter the danger in row " + i + " separated by spaces ");
for(j=0;j<col;j++)
arr[i][j]=scanner.nextInt();
}
//displayBoard(arr, row, col, cur_row, cur_col);
System.out.println("Enter the starting x coordinate");
cur_row=scanner.nextInt();
System.out.println("Enter the starting y coordinate");
cur_col=scanner.nextInt();
String pos,pos1;
int total_danger = 0;
int temp=0,temp1=0;
StringBuilder positions = new StringBuilder();
displayBoard(arr,row,col, cur_row, cur_col);
while(true)
{
total_danger += arr[cur_row][cur_col];
pos = Integer.toString(cur_row) + "," + Integer.toString(cur_col);
positions.append(pos);
//System.out.println(positions);
int min = 100000; //giving a enough big number
pos1 = Integer.toString(cur_row+1) + "," + Integer.toString(cur_col); // checking for the all possible 4 positions and where danger is minimum
//System.out.println(pos1);
if(!positions.toString().contains(pos1) && min>arr[cur_row+1][cur_col])
{
min = arr[cur_row+1][cur_col];
temp=cur_row+1;
temp1=cur_col;
}
pos1 = Integer.toString(cur_row) + "," + Integer.toString(cur_col+1);
//System.out.println(pos1);
if(!positions.toString().contains(pos1) && min>arr[cur_row][cur_col+1] )
{
min = arr[cur_row][cur_col+1];
temp=cur_row+1;
temp1=cur_col;
}
pos1 = Integer.toString(cur_row-1) + "," + Integer.toString(cur_col);
//System.out.println(pos1);
if(!positions.toString().contains(pos1) && min>arr[cur_row-1][cur_col])
{
min = arr[cur_row-1][cur_col];
temp=cur_row-1;
temp1=cur_col;
}
pos1 = Integer.toString(cur_row) + "," + Integer.toString(cur_col-1);
//System.out.println(pos1);
if(!positions.toString().contains(pos1) && min>arr[cur_row][cur_col-1])
{
min = arr[cur_row][cur_col-1];
temp=cur_row;
temp1=cur_col-1;
}
cur_row = temp; // temp temp1 are the coordinate where the next danger is minimum
cur_col = temp1;
//System.out.println(min +" " + cur_row + " " + cur_col);
displayBoard(arr,row,col, cur_row, cur_col);
if( cur_row==0 || cur_row==row-1 || cur_col ==0 || cur_col ==col-1) // come out of the loop when it is at the edge
{
total_danger += arr[cur_row][cur_col];
break;
}
}
System.out.println("Exited the world at" + cur_row + "," + cur_col+ " Total danger faced: "+total_danger);
}
}
// sample output
How many rows are in the maze?
5
How many columns are in the maze?
7
Enter the danger in row 0 separated by spaces
0 1 4 2 6 0 1
Enter the danger in row 1 separated by spaces
1 7 4 2 2 6 0
Enter the danger in row 2 separated by spaces
0 0 0 8 1 4 1
Enter the danger in row 3 separated by spaces
1 1 2 7 3 4 2
Enter the danger in row 4 separated by spaces
5 1 6 4 2 2 1
Enter the starting x coordinate
2
Enter the starting y coordinate
4
0 1 4 2 6 0 1
1 7 4 2 2 6 0
0 0 0 8 * 4 1
1 1 2 7 3 4 2
5 1 6 4 2 2 1
0 1 4 2 6 0 1
1 7 4 2 * 6 0
0 0 0 8 1 4 1
1 1 2 7 3 4 2
5 1 6 4 2 2 1
0 1 4 2 6 0 1
1 7 4 * 2 6 0
0 0 0 8 1 4 1
1 1 2 7 3 4 2
5 1 6 4 2 2 1
0 1 4 * 6 0 1
1 7 4 2 2 6 0
0 0 0 8 1 4 1
1 1 2 7 3 4 2
5 1 6 4 2 2 1
Exited the world at0,3 Total danger faced: 7
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.