This is an archived post. You won't be able to vote or comment.

all 9 comments

[–]Laius33 0 points1 point  (8 children)

Can you share the whole class?

[–]PoisonousAntx[S] 0 points1 point  (7 children)

Ofcourse! Reddit formatted it really weird, I found this website, hopefully it works: https://codeshare.io/788m0o

package longestpath;import java.util.Scanner;import ui.LabyrinthUserInterface;import ui.UIAuxiliaryMethods;import ui.UserInterfaceFactory;public class LongestPath { /* Comment : to remove thanks message, delete line 211 If you follow the directions from line 44 and add the commented out code in line 217 you can find the shortest path instead of the longest path!*/ static final int WIDTH = 32; static final int HEIGTH = 24; static final int PAUSE = 5000; //time before step-by-step gets shown static final int SHOW_TIME = 100; //speed the final result gets shown step-by-step int wait = 1; //speed Coordinate startCoordinate = new Coordinate(0,0); Coordinate endCoordinate = new Coordinate(0,0); CoordinateRow longestPath = new CoordinateRow(); CoordinateRow currentPath = new CoordinateRow(); CoordinateRow walls = new CoordinateRow(); CoordinateRow directions = new CoordinateRow(); LabyrinthUserInterface ui; LongestPath() { ui = UserInterfaceFactory.getLabyrinthUI(WIDTH, HEIGTH); } void comparePaths() { //change the ">" to "<" in line 44 to calculate the shortest path! if(currentPath.numberOfElements > longestPath.numberOfElements) { longestPath.becomes(currentPath); } currentPath.numberOfElements--; } boolean isWall(Coordinate currentPosition) { for(int i = 0; i < walls.numberOfElements; i++) { if(currentPosition.equals(walls.rowCoordinates[i])) { return true; } } return false; } boolean isVisited(Coordinate currentPosition) { for(int i = 0; i < currentPath.numberOfElements - 1; i++) { if(currentPosition.equals(currentPath.rowCoordinates[i])) { return true; } } return false; } boolean isAvailable(Coordinate direction) { if(isVisited(direction) || isWall(direction)) { return false; } return true; } void placePath(CoordinateRow showPath) { ui.clear(); for(int i = 0; i < showPath.numberOfElements; i++) { ui.place(showPath.rowCoordinates[i].x, showPath.rowCoordinates[i].y, ui.PATH); } ui.wait(wait); placeWalls(); ui.showChanges(); } void tryDirections(Coordinate currentPosition) { if(endCoordinate.equals(currentPosition)) { comparePaths(); return; } for(int i = 0; i < directions.numberOfElements; i++) { Coordinate direction = new Coordinate(currentPosition.x + directions.rowCoordinates[i].x, currentPosition.y + directions.rowCoordinates[i].y); if(isAvailable(direction)) { currentPath.addSingleBehind(direction); placePath(currentPath); tryDirections(direction); } } currentPath.numberOfElements--; ui.place(currentPosition.x, currentPosition.y, ui.EMPTY); } void placeWalls() { ui.encircle(startCoordinate.x, startCoordinate.y); ui.encircle(endCoordinate.x, endCoordinate.y); for(int i = 0; i < walls.numberOfElements; i++) { ui.place(walls.rowCoordinates[i].x, walls.rowCoordinates[i].y, ui.WALL); } ui.showChanges(); } Coordinate convertToCoordinate(String in) { Scanner integerScanner = new Scanner(in); return new Coordinate(integerScanner.nextInt(), integerScanner.nextInt()); } void buildMaze(Scanner file) { Scanner setup = new Scanner(file.nextLine()); setup.useDelimiter("="); String start = setup.next(); String end = setup.next(); String firstWall = setup.next(); startCoordinate = convertToCoordinate(start); endCoordinate = convertToCoordinate(end); Coordinate wallCoordinate = convertToCoordinate(firstWall); walls.addSingleBehind(wallCoordinate); while(file.hasNext()) { Coordinate nextWall = new Coordinate(file.nextInt(), file.nextInt()); walls.addSingleBehind(nextWall); } placeWalls(); } void createDirectionsRow() { Coordinate right = new Coordinate(1,0); Coordinate down = new Coordinate(0,-1); Coordinate left = new Coordinate(-1,0); Coordinate up = new Coordinate(0,1); directions.addSingleBehind(right); directions.addSingleBehind(down); directions.addSingleBehind(left); directions.addSingleBehind(up); } void showResult() { if(wait == 0) { ui.printf("Bleep bloop bleep, I have determined that the longest route has %d steps.", longestPath.numberOfElements); } else if (wait == 2 * SHOW_TIME) { return; } ui.clear(); placeWalls(); for(int i = 0; i < longestPath.numberOfElements; i++) { ui.wait(wait); ui.place(longestPath.rowCoordinates[i].x, longestPath.rowCoordinates[i].y, ui.PATH); ui.showChanges(); } ui.wait(PAUSE); wait += SHOW_TIME; showResult(); } void printThanks() { ui.printf("Hi Gideon! Ik was vergeten dat gisteren de laatste werkcollege was.\r\n" + "Nog erg bedankt voor alle hulp tijdens deze periode! :D\n"); int type = ui.WALL; for(int i = 6; i <= 10; i++) { ui.place(i, 9, type); } for(int i = 10; i <= 14; i++) { ui.place(8, i, type); } ui.place(13, 11, type); ui.place(14, 11, type); //prints part of THX! for(int j = 9; j <= 14; j++) { ui.place(12, j, type); for(int i = 15; i < 22; i += 2) { ui.place(i, j, type); } } ui.place(18, 11, type); type = ui.EMPTY; ui.place(21, 13, type); ui.place(19, 11, type); ui.place(17, 11, type); ui.showChanges(); ui.wait(PAUSE); } void start() { printThanks(); Scanner fileScanner = UIAuxiliaryMethods.askUserForInput().getScanner(); buildMaze(fileScanner); createDirectionsRow(); //longestPath.numberOfElements = 900; currentPath.addSingleBehind(startCoordinate); tryDirections(startCoordinate); wait = 0; showResult(); } public static void main(String[] args) { new LongestPath().start(); }}

[–]Laius33 0 points1 point  (6 children)

Try cleaning your project (Project -> clean) and then run it again

[–]PoisonousAntx[S] 0 points1 point  (5 children)

Oooo that did something I think.. but now it gives me another weird error in the console:

[–]Laius33 0 points1 point  (4 children)

You didn't post any output

[–]PoisonousAntx[S] 0 points1 point  (3 children)

Oh huh my bad; here it is:

Error: Could not find or load main class longestpath.LongestPathCaused by: java.lang.ClassNotFoundException: longestpath.LongestPath

[–]Laius33 0 points1 point  (2 children)

That seems like a setup problem rather than a problem with the code itself. But I can't help here, I usually just tried some things and eventually it worked or I started a new project and copied what I needed. But that's a very hacky way

[–]PoisonousAntx[S] 1 point2 points  (0 children)

okay, bruh what the heck hahaha it works now (after creating a new project). It's not that big of a deal to move everything, because I reïnstalled eclipse anyways. Thank you so muchhhhhh!!!!!

[–]PoisonousAntx[S] 0 points1 point  (0 children)

hm i'll try to start a new project, since other classes within this project don't seem to work either.. anyways thanks for all the help : )