package boolean hitsArray(CoordinateRow array) { for (int i

package LongestPath;

import java.util.Scanner;
import ui.UserInterfaceFactory;
import ui.LabyrinthUserInterface;
import ui.UIAuxiliaryMethods;

We Will Write a Custom Essay Specifically
For You For Only $13.90/page!


order now

class LongestPath {
LabyrinthUserInterface ui;
static final int LABYRINTH_WIDTH = 32;
static final int LABYRINTH_HEIGHT = 24;
static final int LEFT = 1;
static final int RIGHT = 2;
static final int UP = 3;
static final int DOWN = 4;
static final int WAITING_TIME = 10;
Coordinate startCoordinate;
Coordinate endCoordinate;
Coordinate nextPosition;
CoordinateRow tempPath;
CoordinateRow longestPath;
CoordinateRow wall;

LongestPath() {
UserInterfaceFactory.enableLowResolution(true);
ui = UserInterfaceFactory.getLabyrinthUI(LABYRINTH_WIDTH,LABYRINTH_HEIGHT);
}

Coordinate storeCoordinate(Scanner coordinateScanner) {
return new Coordinate(coordinateScanner.nextInt(), coordinateScanner.nextInt());
}

void makeWallArray(Scanner wallScanner) {
wall = new CoordinateRow();
wallScanner.useDelimiter(”
“);
while (wallScanner.hasNext()) {
Scanner wallPieceScanner = new Scanner(wallScanner.next());
wall.addBehind(storeCoordinate(wallPieceScanner));
}
}

void arrangeFile(Scanner fileScanner) {
fileScanner.useDelimiter(“=”);
while (fileScanner.hasNext()) {
startCoordinate = storeCoordinate(new Scanner(fileScanner.next()));
endCoordinate = storeCoordinate(new Scanner(fileScanner.next()));
makeWallArray(new Scanner(fileScanner.next()));
}
}

void initializeLabyrinth() {
for (int i = 0; i < wall.numberOfElements; i++) { ui.place(wall.coordinateArrayi.x, wall.coordinateArrayi.y, ui.WALL); } ui.place(startCoordinate.x, startCoordinate.y, ui.PATH); ui.encircle(endCoordinate.x, endCoordinate.y); ui.showChanges(); tempPath = new CoordinateRow(); tempPath.addBehind(startCoordinate); longestPath = new CoordinateRow(); } void updateLongestPath() { for (int i = 0; i < tempPath.numberOfElements; i++) { longestPath.coordinateArrayi = tempPath.coordinateArrayi; } longestPath.numberOfElements = tempPath.numberOfElements; } void decideIfLongestPath() { if (tempPath.numberOfElements > longestPath.numberOfElements) {
updateLongestPath();
}
}

boolean hitsArray(CoordinateRow array) {
for (int i = 0; i < array.numberOfElements; i++) { if (nextPosition.x == array.coordinateArrayi.x && nextPosition.y == array.coordinateArrayi.y) { return true; } } return false; } boolean isCollision() { return hitsArray(tempPath) || hitsArray(wall); } Coordinate storeNextPosition(int x, int y) { return new Coordinate(x,y); } boolean isMovement(int direction) { if (direction == LEFT) { nextPosition = storeNextPosition(tempPath.coordinateArraytempPath.numberOfElements-1.x-1, tempPath.coordinateArraytempPath.numberOfElements-1.y); } if (direction == RIGHT) { nextPosition = storeNextPosition(tempPath.coordinateArraytempPath.numberOfElements-1.x+1, tempPath.coordinateArraytempPath.numberOfElements-1.y); } if (direction == UP) { nextPosition = storeNextPosition(tempPath.coordinateArraytempPath.numberOfElements-1.x, tempPath.coordinateArraytempPath.numberOfElements-1.y-1); } if (direction == DOWN) { nextPosition = storeNextPosition(tempPath.coordinateArraytempPath.numberOfElements-1.x, tempPath.coordinateArraytempPath.numberOfElements-1.y+1); } return (!isCollision()); } void updatePath() { tempPath.addBehind(nextPosition); for (int i = 0; i < tempPath.numberOfElements; i++) { ui.place(tempPath.coordinateArrayi.x, tempPath.coordinateArrayi.y, ui.PATH); } ui.showChanges(); ui.wait(WAITING_TIME); } void moveBack(Coordinate tempCoordinate) { tempPath.eliminateCoordinate(); ui.place(tempCoordinate.x, tempCoordinate.y, ui.EMPTY); ui.showChanges(); } void determineLongestPath() { if (tempPath.coordinateArraytempPath.numberOfElements - 1.x == endCoordinate.x && tempPath.coordinateArraytempPath.numberOfElements - 1.y == endCoordinate.y) { decideIfLongestPath(); return; } for (int direction = LEFT; direction <= DOWN; direction ++) { if (isMovement(direction)) { updatePath(); determineLongestPath(); moveBack(tempPath.coordinateArraytempPath.numberOfElements - 1); } } } void displayLongestPath() { for (int i = 0; i < longestPath.numberOfElements; i++) { ui.place(longestPath.coordinateArrayi.x, longestPath.coordinateArrayi.y, ui.PATH); } ui.showChanges(); } void start() { Scanner fileScanner = UIAuxiliaryMethods.askUserForInput().getScanner(); arrangeFile(fileScanner); initializeLabyrinth(); determineLongestPath(); displayLongestPath(); ui.printf("The longest path has a length of %d", longestPath.numberOfElements); } public static void main(String args) { new LongestPath().start(); } }