package } } for (int i = 0; i

package LongestPath3;import java.util.Scanner;import ui.UserInterfaceFactory;import ui.LabyrinthUserInterface;import ui.UIAuxiliaryMethods;class LongestPath3 { LabyrinthUserInterface ui; static final int LABYRINTH_WIDTH = 32; static final int LABYRINTH_HEIGHT = 24; Coordinate startCoordinate; Coordinate endCoordinate; CoordinateRow wall = new CoordinateRow(); CoordinateRow tempPath = new CoordinateRow(); CoordinateRow longestPath = new CoordinateRow(); LongestPath3() { 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) { wallScanner.useDelimiter(”
“); while (wallScanner.hasNext()) { Scanner wallPieceScanner = new Scanner(wallScanner.next()); wall.addBehind(storeCoordinate(wallPieceScanner)); } } void placePieces(CoordinateRow array, int type) { for (int i = 0; i < array.numberOfElements; i++) { ui.place(array.coordinateArrayi.x, array.coordinateArrayi.y, type); } ui.showChanges(); } void showInitialPosition(Coordinate startCoordinate) { ui.place(startCoordinate.x, startCoordinate.y, ui.PATH); } void showEndPosition(Coordinate endCoordinate) { ui.encircle(endCoordinate.x, endCoordinate.y); } void initializeLabyrinth(Scanner fileScanner) { fileScanner.useDelimiter("="); while (fileScanner.hasNext()) { startCoordinate = storeCoordinate(new Scanner(fileScanner.next())); endCoordinate = storeCoordinate(new Scanner(fileScanner.next())); makeWallArray(new Scanner(fileScanner.next())); } placePieces(wall, ui.WALL); showInitialPosition(startCoordinate); showEndPosition(endCoordinate); ui.showChanges(); ui.wait(2000); } 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 isCollision(Coordinate nextPosition) { for (int i = 0; i < tempPath.numberOfElements; i++) { if (nextPosition.x == tempPath.coordinateArrayi.x && nextPosition.y == tempPath.coordinateArrayi.y) { return true; } } for (int i = 0; i < wall.numberOfElements; i++) { if (nextPosition.x == wall.coordinateArrayi.x && nextPosition.y == wall.coordinateArrayi.y) { return true; } } return false; } void moveBack(Coordinate tempCoordinate) { tempPath.eliminateCoordinate(); ui.place(tempCoordinate.x, tempCoordinate.y, ui.EMPTY); ui.showChanges(); } void determineLongestPath(Coordinate tempCoordinate) { ui.wait(300); tempPath.addBehind(tempCoordinate); placePieces(tempPath, ui.PATH); if (tempCoordinate.x == endCoordinate.x && tempCoordinate.y == endCoordinate.y) { decideIfLongestPath(); } Coordinate nextLeft = new Coordinate(tempCoordinate.x - 1, tempCoordinate.y); Coordinate nextRight = new Coordinate(tempCoordinate.x + 1, tempCoordinate.y); Coordinate nextUp = new Coordinate(tempCoordinate.x, tempCoordinate.y - 1); Coordinate nextDown = new Coordinate(tempCoordinate.x, tempCoordinate.y + 1); if (!isCollision(nextLeft)) { determineLongestPath(nextLeft); } if (!isCollision(nextRight)) { determineLongestPath(nextRight); } if (!isCollision(nextUp)) { determineLongestPath(nextUp); } if (!isCollision(nextDown)) { determineLongestPath(nextDown); } moveBack(tempCoordinate); } void start() { Scanner fileScanner = UIAuxiliaryMethods.askUserForInput().getScanner(); initializeLabyrinth(fileScanner); determineLongestPath(startCoordinate); placePieces(longestPath, ui.PATH); ui.printf("The longest path has a length of %d", longestPath.numberOfElements); } public static void main(String args) { new LongestPath3().start(); }}

x

Hi!
I'm Dianna!

Would you like to get a custom essay? How about receiving a customized one?

Check it out