package if (direction == 4) { newPosition = new

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

public class LongestPath {
LabyrinthUserInterface ui;
static final int LABYRINTH_WIDTH = 32;
static final int LABYRINTH_HEIGHT = 24;
static final int NORTH = 1;
static final int EAST = 2;
static final int SOUTH = 3;
static final int WEST = 4;
static final int WAITING_TIME = 10;
Coordinate startCoordinate;
Coordinate finalCoordinate;
Coordinate newPosition;
CoordinateRow pathArray = new CoordinateRow();
CoordinateRow longestPathArray = new CoordinateRow();
CoordinateRow wallArray = new CoordinateRow();

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

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

void makeWall(Scanner wallScanner) {
while (wallScanner.hasNext()) {
wallArray.addBehind(new Coordinate(wallScanner.nextInt(), wallScanner.nextInt()));
}
}

void initializeLabyrinth() {
ui.place(startCoordinate.x, startCoordinate.y, ui.PATH);
ui.encircle(finalCoordinate.x, finalCoordinate.y);
for (int i = 0; i < wallArray.numberOfElements; i++) { ui.place(wallArray.coordinateArrayi.x, wallArray.coordinateArrayi.y, ui.WALL); } ui.showChanges(); } void processFile(Scanner fileScanner) { fileScanner.useDelimiter("="); while (fileScanner.hasNext()) { startCoordinate = storePosition(new Scanner(fileScanner.next())); pathArray.addBehind(startCoordinate); finalCoordinate = storePosition(new Scanner(fileScanner.next())); makeWall(new Scanner(fileScanner.next())); } initializeLabyrinth(); } void analyseLengthPath() { if (pathArray.numberOfElements > longestPathArray.numberOfElements) {
longestPathArray.numberOfElements = pathArray.numberOfElements;
for (int i = 0; i < pathArray.numberOfElements; i++) { longestPathArray.coordinateArrayi = new Coordinate(pathArray.coordinateArrayi.x, pathArray.coordinateArrayi.y); } } } boolean collision(Coordinate newPosition) { for (int i = 0; i < wallArray.numberOfElements; i++) { if (newPosition.equals(wallArray.coordinateArrayi)) { return true; } } for (int i = 0; i < pathArray.numberOfElements; i++) { if (newPosition.equals(pathArray.coordinateArrayi)) { return true; } } return false; } boolean straightAheadPossible(int direction) { if (direction == 1) { newPosition = new Coordinate(pathArray.coordinateArraypathArray.numberOfElements - 1.x, pathArray.coordinateArraypathArray.numberOfElements - 1.y - 1); } else if (direction == 2) { newPosition = new Coordinate(pathArray.coordinateArraypathArray.numberOfElements - 1.x + 1, pathArray.coordinateArraypathArray.numberOfElements - 1.y); } else if (direction == 3) { newPosition = new Coordinate(pathArray.coordinateArraypathArray.numberOfElements - 1.x, pathArray.coordinateArraypathArray.numberOfElements - 1.y + 1); } else if (direction == 4) { newPosition = new Coordinate(pathArray.coordinateArraypathArray.numberOfElements - 1.x - 1, pathArray.coordinateArraypathArray.numberOfElements - 1.y); } return (! (collision(newPosition))); } void expandPath(Coordinate newPosition) { ui.place(newPosition.x, newPosition.y, ui.PATH); ui.showChanges(); pathArray.addBehind(newPosition); ui.wait(WAITING_TIME); } void removePath(Coordinate lastPosition) { ui.place(lastPosition.x, lastPosition.y, ui.EMPTY); ui.showChanges(); pathArray.remove(lastPosition); ui.wait(WAITING_TIME); } void determineLongestPath() { if (pathArray.coordinateArraypathArray.numberOfElements - 1.equals(finalCoordinate)) { analyseLengthPath(); return; } for (int i = NORTH; i <= WEST; i++) { if (straightAheadPossible(i)) { expandPath(newPosition); determineLongestPath(); removePath(pathArray.coordinateArraypathArray.numberOfElements - 1); } } } void showLongestPath() { for(int i = 0; i < longestPathArray.numberOfElements; i++){ ui.place(longestPathArray.coordinateArrayi.x, longestPathArray.coordinateArrayi.y, ui.PATH); ui.showChanges(); } } void start() { Scanner fileScanner = UIAuxiliaryMethods.askUserForInput().getScanner(); processFile(fileScanner); determineLongestPath(); showLongestPath(); } public static void main(String args) { new LongestPath().start(); }  }