Temple of Arrays FRQ - 2D Arrays - Answers
Temple of Arrays FRQ - 2D Arrays - Answers
He is in a room
with a floor that changes the symbols on its tiles every minute.
The floor is represented by a 2D array of strings, each representing a symbol on a tile.
For the floor to be safe to cross, the following conditions must be met:
1. There must be at least one straight line of tiles from the left side of the floor to the
right side, where all tiles in the line have the same symbol.
2. The floor must NOT contain either X or Y, which are "danger symbols."
3. Not all symbols on the floor can be the same.
A D D X Q
B B B B B
E C F D R
String[][] templeFloor =
{{"A", "D", "D", "X", "Q"},
{"B", "B", "B", "B", "B"},
{"E", "C", "F", "D", "R"}};
/**
* Determines if the floor is safe based on:
* 1. the absence of danger symbols (X or Y)
* 2. not all symbols are the same
* Returns true if safe, otherwise false
* Precondition: floor contains at least one symbol
* No element of floor is null
* Each String in floor has a length of 1
*/
public static boolean isFloorSafe(String[][] floor)
{ /* To be completed in part (b) */ }
/**
* Determines if Nevada Smith can safely cross the floor.
* returns true if the floor is passable, based on the three
* criteria, false otherwise.
* Precondition: floor contains at least one symbol
* No element of floor is null
* Each String in floor has a length of 1
*/
public static boolean canCrossFloor(String[][] floor)
{ /* To be completed in part (c) */ }
}
/**
* Checks if all symbols in a given line (1D array) are the same.
* Returns true if all symbols are the same, false otherwise.
* Precondition: line contains at least one symbol
* No element of line is null
* Each String in line has a length of 1
*/
public static boolean lineOfIdentical(String[] line)
{
for(int i = 0; i < line.length; i++)
{
if (!line[i].equals(line[0]))
{
return false;
}
}
return true;
}
/**
* Determines if the floor is safe based on:
* 1. the absence of danger symbols (X or Y)
* 2. not all symbols are the same
* Returns true if safe, otherwise false
* Precondition: floor contains at least one symbol
* No element of floor is null
* Each String in floor has a length of 1
*/
public static boolean isFloorSafe(String[][] floor)
{
String firstSymbol = floor[0][0];
boolean allSymbolsSame = true;
/**
* Determines if Nevada Smith can safely cross the floor.
* returns true if the floor is passable, based on the three
* criteria, false otherwise.
* Precondition: floor contains at least one symbol
* No element of floor is null
* Each String in floor has a length of 1
*/
public static boolean canCrossFloor(String[][] floor)
{
if(isFloorSafe(floor) == false)
{
return false;
}
return false;
}