in java language 0 Open space Wall 2 Enemy Main character An example level is sh
ID: 3705418 • Letter: I
Question
in java language
0 Open space Wall 2 Enemy Main character An example level is shown below: Columns 0 0 portion of non-wall cells that are occupied by an (6 pts) Let's define the "difficulty" of a level as the pro enmy. For example, in the data above, there are 19 non-wall cells. enemy. Thus, the difficulty would be 3/19 or about 0.158. L. Of those 19, 3 are occupied by an Write a method difficulty that takes a 2D array in the above format as a parameter. The method should return the difficulty of that level. 2. (10 pts) Let's say that the main character is considered "under threat" if there are at least two enemies within the eight cells immediately surrounding the main character (all four cardinal directions, and all four diagonal directions). For example, in the level above the main character is not under threat since there are no enemies in the immediately surrounding cells. Note that there may be fewer than eight surrounding cells if the main character is on the edge of the level. Write a method isUnder Threat that takes a 2D array in the above format as a parameter. The method should return whether the main character is under threat according to the definition above. You may assume that the main character shows up exactly once in the 2D array. Be careful not to go beyond the bounds of the array! (4 pts) Write a main method that creates a 2D array with the data from the example above, then calls both of your methods and shows the results. You do not need to read any user inputExplanation / Answer
// Class FindDifficultyThread definition
public class FindDifficultyThread
{
// Static method to display board
static void displayBoard(int board[][])
{
// Loops till number of rows
for(int row = 0; row < board.length; row++)
{
// Loops till number of columns
for(int col = 0; col < board[row].length; col++)
// Displays each cell position value
System.out.printf("%3d", board[row][col]);
// Displays new line for each row
System.out.println();
}// End of inner for loop
}// End of outer for loop
// Static method to calculate and return difficulty level
static double difficulty(int board[][])
{
// To store difficulty level calculated value
double difficultyLevel = 0;
// Counter for Open space, Wall, Enemy, main character
int openC, wallC, enemyC, mainCharacterC;
// To store calculated non walls
int nonWall;
// Initializes the counter to zero
openC = wallC = enemyC = mainCharacterC = 0;
// Loops till number of rows
for(int row = 0; row < board.length; row++)
{
// Loops till number of columns
for(int col = 0; col < board[row].length; col++)
{
// Checks if the current row and column index position of the board contains 0
if(board[row][col] == 0)
openC++;
// Checks if the current row and column index position of the board contains 1
else if(board[row][col] == 1)
wallC++;
// Checks if the current row and column index position of the board contains 2
else if(board[row][col] == 2)
enemyC++;
// Checks if the current row and column index position of the board contains 3
else if(board[row][col] == 3)
mainCharacterC++;
// Otherwise invalid data
else
System.out.println("Invalid Data!");
}// End of inner for loop
}// End of outer for loop
// Calculates number of non walls
nonWall = (openC + enemyC + mainCharacterC);
// Calculates the difficulty level
difficultyLevel = enemyC / (double)nonWall;
// Returns the difficulty level
return difficultyLevel;
}// End of method
// Static method to return threat status
// If threat counter value is 2 or more than 2 then returns true
// Otherwise returns false
static boolean isUnderThreat(int board[][])
{
// To store the threat status. Initially false for no threat
boolean threatStatus = false;
// To store the row and column position of the 3 found in matrix
int mainCharacterRow = -1, mainCharacterCol = -1;
// Counter for number of threats
int threatC = 0;
// Loops till number of rows
for(int row = 0; row < board.length; row++)
{
// Loops till number of columns
for(int col = 0; col < board[row].length; col++)
{
// Checks if the current row and column index position of the board contains 3
if(board[row][col] == 3)
{
// Sets the row and column position of the number 3 found position in board
mainCharacterRow = row;
mainCharacterCol = col;
}// End of if condition
}// End of inner for loop
}// End of outer for loop
// Left corner beginning
if(mainCharacterRow == 0 && mainCharacterCol == 0)
{
// Next column
if(board[mainCharacterRow][mainCharacterCol+1] == 2)
threatC++;
// Next Row
if(board[mainCharacterRow+1][mainCharacterCol] == 2)
threatC++;
// Right diagonal
if(board[mainCharacterRow+1][mainCharacterCol+1] == 2)
threatC++;
}// End of else if condition
// Right corner beginning
else if(mainCharacterRow == 0 && mainCharacterCol == board[0].length -1)
{
if(board[mainCharacterRow][mainCharacterCol-1] == 2)
threatC++;
if(board[mainCharacterRow+1][mainCharacterCol] == 2)
threatC++;
if(board[mainCharacterRow+1][mainCharacterCol-1] == 2)
threatC++;
}// End of else if condition
// Left corner end
else if(mainCharacterRow == board.length -1 && mainCharacterCol == 0)
{
if(board[mainCharacterRow-1][mainCharacterCol] == 2)
threatC++;
if(board[mainCharacterRow][mainCharacterCol+1] == 2)
threatC++;
if(board[mainCharacterRow-1][mainCharacterCol+1] == 2)
threatC++;
}// End of else if condition
// Right corner end
else if(mainCharacterRow == board.length -1 && mainCharacterCol == board[0].length -1)
{
if(board[mainCharacterRow-1][mainCharacterCol] == 2)
threatC++;
if(board[mainCharacterRow][mainCharacterCol-1] == 2)
threatC++;
if(board[mainCharacterRow-1][mainCharacterCol-1] == 2)
threatC++;
}// End of else if condition
// First Row any position
else if(mainCharacterRow == 0)
{
// Left
if(board[mainCharacterRow][mainCharacterCol-1] == 2)
threatC++;
// Right
if(board[mainCharacterRow][mainCharacterCol+1] == 2)
threatC++;
// Bottom
if(board[mainCharacterRow+1][mainCharacterCol] == 2)
threatC++;
// Left Diagonal
if(board[mainCharacterRow+1][mainCharacterCol-1] == 2)
threatC++;
// RightDiagonal
if(board[mainCharacterRow+1][mainCharacterCol+1] == 2)
threatC++;
}// End of else if condition
// Last Row any position
else if(mainCharacterRow == board.length-1)
{
// Left
if(board[mainCharacterRow][mainCharacterCol-1] == 2)
threatC++;
// Right
if(board[mainCharacterRow][mainCharacterCol+1] == 2)
threatC++;
// Top
if(board[mainCharacterRow-1][mainCharacterCol] == 2)
threatC++;
// Left Diagonal
if(board[mainCharacterRow-1][mainCharacterCol-1] == 2)
threatC++;
// RightDiagonal
if(board[mainCharacterRow-1][mainCharacterCol+1] == 2)
threatC++;
}// End of else if condition
// First column any position
else if(mainCharacterCol == 0)
{
// Previous
if(board[mainCharacterRow-1][mainCharacterCol] == 2)
threatC++;
// Next
if(board[mainCharacterRow+1][mainCharacterCol] == 2)
threatC++;
// Next column
if(board[mainCharacterRow][mainCharacterCol+1] == 2)
threatC++;
// Top Diagonal
if(board[mainCharacterRow-1][mainCharacterCol+1] == 2)
threatC++;
// Bottom Diagonal
if(board[mainCharacterRow+1][mainCharacterCol+1] == 2)
threatC++;
}// End of else if condition
// Last column any position
else if(mainCharacterCol == board[0].length-1)
{
// Previous
if(board[mainCharacterRow-1][mainCharacterCol] == 2)
threatC++;
// Next
if(board[mainCharacterRow+1][mainCharacterCol] == 2)
threatC++;
// Previous column
if(board[mainCharacterRow][mainCharacterCol-1] == 2)
threatC++;
// Top Diagonal
if(board[mainCharacterRow-1][mainCharacterCol-1] == 2)
threatC++;
// Bottom Diagonal
if(board[mainCharacterRow+1][mainCharacterCol-1] == 2)
threatC++;
}// End of else if condition
// Any other position
else if(mainCharacterRow != -1 && mainCharacterCol != -1)
{
// Previous Row bottom
if(board[mainCharacterRow-1][mainCharacterCol] == 2)
threatC++;
// Next Row Top
if(board[mainCharacterRow+1][mainCharacterCol] == 2)
threatC++;
// Same Row Previous column
if(board[mainCharacterRow][mainCharacterCol-1] == 2)
threatC++;
// Same Row Next column
if(board[mainCharacterRow][mainCharacterCol+1] == 2)
threatC++;
// Previous row left diagonal
if(board[mainCharacterRow-1][mainCharacterCol-1] == 2)
threatC++;
// Previous row right diagonal
if(board[mainCharacterRow-1][mainCharacterCol+1] == 2)
threatC++;
// Next row left diagonal
if(board[mainCharacterRow+1][mainCharacterCol-1] == 2)
threatC++;
// Next row right diagonal
if(board[mainCharacterRow+1][mainCharacterCol+1] == 2)
threatC++;
}// End of else if condition
// Checks if threat count is 2 or more than 2 then set the threatStatus to true
if(threatC >= 2)
threatStatus = true;
// Returns threat status
return threatStatus;
}// End of method
// main method definition
public static void main(String[] args)
{
// Creates a board
int board[][] = {{1,1,2,1,0,1,1}, {1,1,0,2,0,1,1}, {1,0,0,1,1,1,1}, {1,0,1,0,0,0,1},
{1,0,1,0,1,0,1}, {0,3,0,2,1,0,1}};
// Calls the method to display board
displayBoard(board);
// Calls the method to return difficulty level and display it
System.out.printf(" Difficulty Level: %.3f", difficulty(board));
// Call the method to check threat status
// If returns true there is a threat otherwise there is no threat
if(isUnderThreat(board))
System.out.println(" Uner Threat");
else
System.out.println(" Not Under Threat");
}// End of main method
}// End of class
Sample Output:
1 1 2 1 0 1 1
1 1 0 2 0 1 1
1 0 0 1 1 1 1
1 0 1 0 0 0 1
1 0 1 0 1 0 1
0 3 0 2 1 0 1
Difficulty Level: 0.158
Not Under Threat
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.