Advertisement
Guest User

Untitled

a guest
Dec 9th, 2018
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.61 KB | None | 0 0
  1. import java.util.*;
  2.  
  3. public enum PathFinderEnum implements PathFinderInterface {
  4. LEFT_HAND_TRAFFIC,
  5. RIGHT_HAND_TRAFFIC;
  6.  
  7. private int [][]map;
  8.  
  9. private Vector<Vector<Position>> positionsMap;
  10.  
  11. enum ModeHack{
  12. easiestModeHack,
  13. fastModeHack,
  14. shortModeHack
  15. }
  16.  
  17. enum Direction{
  18. left,
  19. right,
  20. straight
  21. }
  22.  
  23. ModeHack modeHack;
  24.  
  25. @Override
  26. public void setMap(int[][] map) {
  27. this.map = map;
  28. this.positionsMap = stupidArrayToObjectArray(map);
  29. }
  30.  
  31. private Vector<Vector<Position>> stupidArrayToObjectArray(int [][]map){
  32. int x=0;
  33. int y=0;
  34. Vector<Vector<Position>> positions = new Vector<>();
  35. for (int[] m:map) {
  36. positions.add(x, new Vector<>());
  37. for (int w: m) {
  38. var p = new Position(x,y);
  39. if (w != 0) {
  40. p.setCost(w);
  41. }
  42. else p.setBlocked(true);
  43. positions.get(x).add(y, p);
  44. y++;
  45. }
  46. x++;
  47. y = 0;
  48. }
  49.  
  50. return positions;
  51. }
  52.  
  53.  
  54.  
  55. private PositionInterface[] ownResultToStupidOResult(Position start, Position end){
  56. Vector<Position> result = new Vector<>();
  57. Position next = end;
  58. while(next != null){
  59. result.add(next);
  60. next = next.getVisitedFromPosition();
  61. }
  62. Collections.reverse(result);
  63.  
  64. return result.toArray(new PositionInterface[]{});
  65.  
  66. }
  67.  
  68. @Override
  69. public PositionInterface[] getFastestRoute(PositionInterface begin, PositionInterface end) {
  70. modeHack = ModeHack.fastModeHack;
  71. dijkstra(stupidOPointToOwnObject(begin), stupidOPointToOwnObject(end));
  72. return ownResultToStupidOResult(stupidOPointToOwnObject(begin), stupidOPointToOwnObject(end));
  73. }
  74.  
  75. @Override
  76. public PositionInterface[] getShortestRoute(PositionInterface begin, PositionInterface end) {
  77. modeHack = ModeHack.shortModeHack;
  78. dijkstra(stupidOPointToOwnObject(begin), stupidOPointToOwnObject(end));
  79. return ownResultToStupidOResult(stupidOPointToOwnObject(begin), stupidOPointToOwnObject(end));
  80. }
  81.  
  82. @Override
  83. public PositionInterface[] getEasiestRoute(PositionInterface begin, PositionInterface end) {
  84. modeHack = ModeHack.easiestModeHack;
  85. dijkstra(stupidOPointToOwnObject(begin), stupidOPointToOwnObject(end));
  86. return ownResultToStupidOResult(stupidOPointToOwnObject(begin), stupidOPointToOwnObject(end));
  87. }
  88.  
  89.  
  90. private Position stupidOPointToOwnObject(PositionInterface positionInterface){
  91. return positionsMap.get(positionInterface.getCol()).get((positionInterface.getRow()));
  92. }
  93.  
  94.  
  95. /// TO można przerzucic do osobna klasy tylko od dijkstry
  96. private List<DQueen> queen = new ArrayList<>();
  97. private boolean dijkstra(Position begin, Position end){
  98. begin.setTotalCost(begin.getCost());
  99. queen.add(new DQueen(begin, null));
  100. while (!queen.isEmpty()){
  101. var q = queen.get(0);
  102. var currentPosition = q.position;
  103. if (currentPosition.getTotalCost() > 1000 || currentPosition.getTotalCost() < 0){
  104. throw new InternalError();
  105. }
  106. queen.remove(0);
  107. if(currentPosition.isVisited()) continue;
  108. currentPosition.setVisited(true);
  109. q.position.setVisitedFromPosition(q.visitedFrom);
  110. if (currentPosition == end){
  111. queen.clear();
  112. return true;
  113. }
  114. checkAllDirection(currentPosition);
  115. }
  116. return false;
  117. }
  118.  
  119. private void checkAllDirection(Position position){
  120. addToQueen(getUp(position),position);
  121. addToQueen(getDown(position),position);
  122. addToQueen(getLeft(position),position);
  123. addToQueen(getRight(position),position);
  124. }
  125.  
  126. private void calculateTotalCostFast(Position position, Position prv) {
  127. int totalCost = 0;
  128. if (prv != null) {
  129. totalCost += prv.getTotalCost();
  130. }
  131. if (this.name().equals("LEFT_HAND_TRAFFIC")){
  132. if (checkDirection(position,prv) == Direction.right){
  133. totalCost += 1;
  134. }
  135. }
  136. if (this.name().equals("RIGHT_HAND_TRAFFIC")){
  137. if (checkDirection(position,prv) == Direction.left){
  138. totalCost += 2;
  139. }
  140. if (checkDirection(position,prv) == Direction.right){
  141. totalCost += 1;
  142. }
  143. }
  144. totalCost += position.getCost();
  145. position.setTotalCost(totalCost);
  146. }
  147.  
  148. private void calculateTotalCostEasy(Position position, Position prv) {
  149. int totalCost = 0;
  150. if (prv != null) {
  151. totalCost += prv.getTotalCost();
  152.  
  153. }
  154. position.setTotalCost(totalCost);
  155. }
  156.  
  157. private void calculateTotalCostShort(Position position, Position prv) {
  158. int totalCost = 0;
  159. if (prv != null) {
  160. totalCost += prv.getTotalCost();
  161. }
  162. totalCost += 1;
  163. position.setTotalCost(totalCost);
  164. }
  165.  
  166. private Direction checkDirection(Position position, Position prv){
  167. if(position.getVisitedFromPosition() != null && position.getVisitedFromPosition().getVisitedFromPosition() != null)
  168. prv = position.getVisitedFromPosition().getVisitedFromPosition();
  169. if (prv.getY() == position.getY() || position.getX() == prv.getX()){
  170. return Direction.straight;
  171. }
  172.  
  173. if (position.getX() < prv.getX() || position.getY() < prv.getY()){
  174. return Direction.left;
  175. }
  176. else return Direction.right;
  177.  
  178. }
  179.  
  180.  
  181. private void addToQueen(Position position, Position visitedFrom){
  182. if (position == null) return;
  183. if (position.isBlocked()) return;
  184. switch (modeHack){
  185. case fastModeHack:
  186. calculateTotalCostFast(position, visitedFrom);
  187. break;
  188. case shortModeHack:
  189. calculateTotalCostEasy(position, visitedFrom);
  190. break;
  191. case easiestModeHack:
  192. calculateTotalCostShort(position, visitedFrom);
  193. }
  194. queen.add(new DQueen(position, visitedFrom));
  195. queen.sort(Comparator.comparingInt(DQueen::getTotalCost));
  196. }
  197.  
  198. private Boolean testIsExist(int x, int y){
  199. try {
  200. var test = this.map[x][y];
  201. }
  202. catch (ArrayIndexOutOfBoundsException e){
  203. return false;
  204. }
  205. return true;
  206. }
  207.  
  208. private Position getUp(Position position){
  209. int x = position.getX();
  210. int y = position.getY() + 1;
  211. if (testIsExist(x, y))
  212. return positionsMap.get(x).get((y));
  213. else return null;
  214. }
  215.  
  216. private Position getDown(Position position){
  217. int x = position.getX();
  218. int y = position.getY() - 1;
  219. if (testIsExist(x, y))
  220. return positionsMap.get(x).get((y));
  221. else return null;
  222. }
  223.  
  224. private Position getLeft(Position position){
  225. int x = position.getX() -1;
  226. int y = position.getY();
  227. if (testIsExist(x, y))
  228. return positionsMap.get(x).get((y));
  229. else return null;
  230. }
  231.  
  232. private Position getRight(Position position){
  233. int x = position.getX() + 1;
  234. int y = position.getY();
  235. if (testIsExist(x, y))
  236. return positionsMap.get(x).get((y));
  237. else return null;
  238. }
  239.  
  240. ///DO tego miejsca
  241. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement