Advertisement
Guest User

Untitled

a guest
Oct 22nd, 2016
141
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.14 KB | None | 0 0
  1. package LongestPath;
  2.  
  3. import ui.UIAuxiliaryMethods;
  4. import ui.UserInterfaceFactory;
  5. import java.util.Scanner;
  6. import ui.LabyrinthUserInterface;
  7. import java.io.PrintStream;
  8.  
  9. public class LongestPath {
  10. //Name: Amir Abounnasr
  11. //Assignment: LongestPath
  12. //Date: 17/10/16
  13.  
  14. PrintStream out;
  15.  
  16. public final int LABYRINTH_HEIGHT = 24;
  17. public final int LABYRINTH_WIDTH = 32;
  18. public final int SEARCH_SPEED = 1;
  19.  
  20. public final Coordinate WEST = new Coordinate(-1,0);
  21. public final Coordinate SOUTH = new Coordinate(0,1);
  22. public final Coordinate EAST = new Coordinate(1,0);
  23. public final Coordinate NORTH = new Coordinate(0,-1);
  24.  
  25. public final CoordinateRow DIRECTIONS = new CoordinateRow();
  26.  
  27. LabyrinthUserInterface ui;
  28. Scanner in;
  29.  
  30. Coordinate currentWall;
  31. Coordinate startCoordinate;
  32. Coordinate finishCoordinate;
  33. Coordinate currentCoordinate;
  34.  
  35. CoordinateRow wall = new CoordinateRow();
  36. CoordinateRow visitedPath = new CoordinateRow();
  37. CoordinateRow currentLongestPath = new CoordinateRow();
  38. CoordinateRow successfulPath = new CoordinateRow();
  39.  
  40. LongestPath(){
  41.  
  42. out = new PrintStream(System.out);
  43.  
  44. DIRECTIONS.addCoordinate(WEST);
  45. DIRECTIONS.addCoordinate(SOUTH);
  46. DIRECTIONS.addCoordinate(EAST);
  47. DIRECTIONS.addCoordinate(NORTH);
  48.  
  49. UserInterfaceFactory.enableLowResolution(true);
  50. ui = UserInterfaceFactory.getLabyrinthUI(LABYRINTH_WIDTH,LABYRINTH_HEIGHT);
  51.  
  52. }
  53.  
  54. void placeWalls(CoordinateRow wall){
  55.  
  56. for (int i = 0; i < wall.numberOfElements; i++){
  57.  
  58. ui.place(wall.coordinateRow[i].x, wall.coordinateRow[i].y, LabyrinthUserInterface.WALL);
  59.  
  60. }
  61.  
  62. }
  63.  
  64. void parseWallCoordinate(Scanner wallCoordinateScanner){
  65.  
  66. int x,y;
  67.  
  68. wallCoordinateScanner.delimiter();
  69.  
  70. x = wallCoordinateScanner.nextInt();
  71. y = wallCoordinateScanner.nextInt();
  72.  
  73. currentWall = new Coordinate(x,y);
  74.  
  75. wall.addCoordinate(currentWall);
  76.  
  77. }
  78.  
  79. void parseWallCoordinates(Scanner wallCoordinatesScanner){
  80.  
  81. Scanner wallCoordinateScanner;
  82.  
  83. String wallCoordinate;
  84.  
  85. while(wallCoordinatesScanner.hasNextLine()){
  86.  
  87. wallCoordinate = wallCoordinatesScanner.nextLine();
  88. wallCoordinateScanner = new Scanner(wallCoordinate);
  89. parseWallCoordinate(wallCoordinateScanner);
  90. }
  91.  
  92. placeWalls(wall);
  93.  
  94. }
  95.  
  96. void encircleFinishPosition(Coordinate finishCoordinate){
  97.  
  98. ui.encircle(finishCoordinate.x,finishCoordinate.y);
  99.  
  100. }
  101.  
  102. Coordinate parseFinishCoordinate(Scanner finishCoordinateScanner){
  103.  
  104. int x,y;
  105.  
  106. finishCoordinateScanner.delimiter();
  107.  
  108. x = finishCoordinateScanner.nextInt();
  109. y = finishCoordinateScanner.nextInt();
  110.  
  111. finishCoordinate = new Coordinate(x,y);
  112.  
  113. encircleFinishPosition(finishCoordinate);
  114.  
  115. return finishCoordinate;
  116.  
  117. }
  118.  
  119. void placePath(Coordinate coordinate){
  120.  
  121. ui.place(coordinate.x, coordinate.y, LabyrinthUserInterface.PATH);
  122.  
  123. }
  124.  
  125. Coordinate parseStartCoordinate(Scanner startCoordinateScanner){
  126.  
  127. int x,y;
  128.  
  129. startCoordinateScanner.delimiter();
  130.  
  131. x = startCoordinateScanner.nextInt();
  132. y = startCoordinateScanner.nextInt();
  133.  
  134. startCoordinate = new Coordinate(x,y);
  135.  
  136. placePath(startCoordinate);
  137.  
  138. visitedPath.addCoordinate(startCoordinate);
  139. currentCoordinate = startCoordinate;
  140.  
  141. return startCoordinate;
  142.  
  143. }
  144.  
  145. void parseInput(){
  146.  
  147. in = UIAuxiliaryMethods.askUserForInput().getScanner();
  148.  
  149. Scanner startCoordinateScanner;
  150. Scanner finishCoordinateScanner;
  151. Scanner wallCoordinatesScanner;
  152.  
  153. String startCoordinate;
  154. String finishCoordinate;
  155. String wallCoordinates;
  156.  
  157. in.useDelimiter("=");
  158.  
  159. while(in.hasNext()){
  160.  
  161. startCoordinate = in.next();
  162. startCoordinateScanner = new Scanner(startCoordinate);
  163. parseStartCoordinate(startCoordinateScanner);
  164.  
  165. finishCoordinate = in.next();
  166. finishCoordinateScanner = new Scanner(finishCoordinate);
  167. parseFinishCoordinate(finishCoordinateScanner);
  168.  
  169. wallCoordinates = in.next();
  170. wallCoordinatesScanner = new Scanner(wallCoordinates);
  171. parseWallCoordinates(wallCoordinatesScanner);
  172.  
  173. }
  174.  
  175. ui.showChanges();
  176.  
  177. }
  178.  
  179. void restorePrevious(){
  180.  
  181. ui.place(currentCoordinate.x, currentCoordinate.y, LabyrinthUserInterface.EMPTY);
  182. ui.wait(SEARCH_SPEED);
  183. ui.showChanges();
  184.  
  185. visitedPath.removeCoordinate();
  186.  
  187. if(visitedPath.numberOfElements > 0){
  188.  
  189. currentCoordinate.x = visitedPath.coordinateRow[visitedPath.numberOfElements-1].x;
  190. currentCoordinate.y = visitedPath.coordinateRow[visitedPath.numberOfElements-1].y;
  191.  
  192. }
  193.  
  194. }
  195.  
  196. void printLongestPath(){
  197.  
  198. for(int i = 0; i < currentLongestPath.numberOfElements; i++){
  199.  
  200. ui.place(currentLongestPath.coordinateRow[i].x, currentLongestPath.coordinateRow[i].y, LabyrinthUserInterface.PATH);
  201.  
  202. }
  203.  
  204. ui.printf("Longest Path Length: %d", currentLongestPath.numberOfElements);
  205.  
  206. ui.showChanges();
  207. }
  208.  
  209. void findPath(){
  210.  
  211. if(currentCoordinate.isEqual(finishCoordinate)){
  212.  
  213. if(visitedPath.numberOfElements > currentLongestPath.numberOfElements){
  214.  
  215. currentLongestPath = new CoordinateRow();
  216.  
  217. for (int j = 0; j < visitedPath.numberOfElements; j++){
  218.  
  219. currentLongestPath.addCoordinate(visitedPath.coordinateRow[j]);
  220.  
  221. }
  222.  
  223.  
  224. }
  225.  
  226. }
  227.  
  228. for (int i = 0; i < DIRECTIONS.numberOfElements; i++){
  229.  
  230. int x = currentCoordinate.x + DIRECTIONS.coordinateRow[i].x;
  231. int y = currentCoordinate.y + DIRECTIONS.coordinateRow[i].y;
  232.  
  233. Coordinate temporaryCoordinate = new Coordinate(x,y);
  234.  
  235. if (!wall.contains(temporaryCoordinate) && !visitedPath.contains(temporaryCoordinate)){
  236.  
  237. currentCoordinate = temporaryCoordinate;
  238.  
  239. visitedPath.addCoordinate(currentCoordinate);
  240.  
  241. ui.place(currentCoordinate.x, currentCoordinate.y, LabyrinthUserInterface.PATH);
  242.  
  243. ui.wait(SEARCH_SPEED);
  244.  
  245. ui.showChanges();
  246.  
  247. findPath();
  248.  
  249. restorePrevious();
  250.  
  251. }
  252.  
  253. }
  254.  
  255. }
  256.  
  257. void start(){
  258.  
  259. parseInput();
  260. findPath();
  261. printLongestPath();
  262.  
  263. }
  264.  
  265. public static void main(String[] args) {
  266. new LongestPath().start();
  267.  
  268. }
  269.  
  270. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement