Advertisement
Guest User

Untitled

a guest
Jun 14th, 2018
198
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.56 KB | None | 0 0
  1. import java.util.ArrayList;
  2. import java.util.Random;
  3.  
  4. import javafx.scene.shape.Rectangle;
  5.  
  6. import javafx.application.Application;
  7. import javafx.scene.Node;
  8. import javafx.scene.Scene;
  9. import javafx.scene.layout.GridPane;
  10. import javafx.scene.paint.Color;
  11. import javafx.stage.Stage;
  12.  
  13. public class Window extends Application{
  14.  
  15. private static final long DELAY = 10;
  16. public int rowNum = 50;
  17. public int colNum = 50;
  18. public Box[][] mazeGrid_box;
  19. public int nextPosX, nextPosY;
  20.  
  21. public static void main(String[] args) {
  22. launch(args);
  23. }
  24.  
  25. @Override
  26. public void start(Stage primaryStage) throws Exception {
  27.  
  28. primaryStage.setTitle("Maciej Lacwik || maze generator + pathfinding");
  29. primaryStage.setHeight(600);
  30. primaryStage.setWidth(600);
  31.  
  32. GridPane grid = new GridPane();
  33. setMazeToStart();
  34. setGridToStart(grid);
  35. generateMaze();
  36.  
  37. for(int i=0;i<rowNum;i++) {
  38. for(int j=0;j<colNum;j++) {
  39. if(mazeGrid_box[i][j].getType() == FieldType.IN_MAZE) {
  40. colorField(i, j, grid, Color.WHITE);
  41. }
  42.  
  43. }
  44. }
  45.  
  46.  
  47. Scene scene = new Scene(grid, 500, 500);
  48. primaryStage.setScene(scene);
  49.  
  50.  
  51. primaryStage.show();
  52. }
  53.  
  54.  
  55.  
  56.  
  57.  
  58.  
  59. /*
  60. * access specify rectangle and color it
  61. */
  62. public void colorField(int x_, int y_, GridPane grid_, Color newColor) {
  63.  
  64. for (Node child : grid_.getChildren()) {
  65. Integer r = GridPane.getRowIndex(child);
  66. Integer c = GridPane.getColumnIndex(child);
  67. int row = r == null ? 0 : r;
  68. int column = c == null ? 0 : c;
  69. if (row == x_ && column == y_ && (child instanceof Rectangle)) {
  70. Rectangle rect = (Rectangle) child;
  71. rect.setFill(newColor);
  72. break;
  73. }
  74. }
  75. }
  76.  
  77. /*
  78. * filling GridPane with black rectangles
  79. */
  80. public void setGridToStart(GridPane grid_) {
  81.  
  82. for (int row = 0; row < rowNum; row++) {
  83. for (int col = 0; col < colNum; col++) {
  84.  
  85. Rectangle rec = new Rectangle();
  86. rec.setWidth(10);
  87. rec.setHeight(10);
  88. rec.setFill(Color.BLACK);
  89.  
  90. GridPane.setRowIndex(rec, row);
  91. GridPane.setColumnIndex(rec, col);
  92. grid_.getChildren().addAll(rec);
  93. }
  94. }
  95. }
  96.  
  97. /*
  98. * setting Box grid to OUT_OF_MAZE except left bottom corner
  99. */
  100. public void setMazeToStart() {
  101. mazeGrid_box = new Box[rowNum][colNum];
  102.  
  103. for(int i=0;i<rowNum;i++) {
  104. for(int j=0;j<colNum;j++) {
  105. mazeGrid_box[i][j] = new Box(i,j,FieldType.OUT_OF_MAZE);
  106. //mazeGrid_box[i][j].setType(FieldType.OUT_OF_MAZE);
  107. }
  108. }
  109. mazeGrid_box[rowNum-1][0].setType(FieldType.IN_MAZE);
  110. }
  111.  
  112. /*
  113. * main loop with logic functions
  114. */
  115. public void generateMaze() {
  116. nextPosX = colNum;
  117. nextPosY = 5;
  118. while (nextPosX > colNum && nextPosY > rowNum) {
  119. erasedLoopWalk(nextPosX, nextPosY);
  120. findNextStartCell(nextPosX,nextPosY);
  121. }
  122. }
  123.  
  124. /*
  125. * search bottom to top, left to right, for a black cell with two adjacent black cells
  126. */
  127. private void findNextStartCell(int startX, int startY) {
  128.  
  129. boolean firstLoop = true;
  130. for (int y = mazeGrid_box.length-2; y >= 1; y--) {
  131. for (int x = 1; x <= mazeGrid_box[y].length-2; x++) {
  132. if (firstLoop) {
  133. x = startX;
  134. y = startY;
  135. firstLoop = false;
  136. }
  137.  
  138. int numberOfBlackFields = 0;
  139.  
  140. if (mazeGrid_box[y][x-1].getType() == FieldType.OUT_OF_MAZE) numberOfBlackFields++;
  141. if (mazeGrid_box[y][x+1].getType() == FieldType.OUT_OF_MAZE) numberOfBlackFields++;
  142. if (mazeGrid_box[y-1][x].getType() == FieldType.OUT_OF_MAZE) numberOfBlackFields++;
  143. if (mazeGrid_box[y+1][x].getType() == FieldType.OUT_OF_MAZE) numberOfBlackFields++;
  144. if (mazeGrid_box[y-1][x-1].getType() == FieldType.OUT_OF_MAZE) numberOfBlackFields++;
  145. if (mazeGrid_box[y-1][x+1].getType() == FieldType.OUT_OF_MAZE) numberOfBlackFields++;
  146. if (mazeGrid_box[y+1][x-1].getType() == FieldType.OUT_OF_MAZE) numberOfBlackFields++;
  147. if (mazeGrid_box[y+1][x+1].getType() == FieldType.OUT_OF_MAZE) numberOfBlackFields++;
  148.  
  149. if (numberOfBlackFields == 8) {
  150. nextPosX = x;
  151. nextPosY = y;
  152. }
  153. }
  154. }
  155. }
  156.  
  157. private void erasedLoopWalk(int x, int y) {
  158. ArrayList<Box> trail = new ArrayList<Box>();
  159. ArrayList<Integer> moveLog = new ArrayList<Integer>();
  160. Random rand = new Random();
  161.  
  162. trail.add(mazeGrid_box[y][x]);
  163. mazeGrid_box[y][x].setType(FieldType.CANDIDATE);
  164.  
  165. moveLog.add(-1);//placeholder that hopefully won't break anything
  166. while (true) {
  167. //decide candidates based on borders and previous move
  168. ArrayList<Integer> candidates = new ArrayList<Integer>();
  169. int prev = moveLog.get(moveLog.size()-1);
  170. if (x-2 > 0 && prev != 1) {
  171. candidates.add(0);
  172. }
  173. if (x+2 < mazeGrid_box[0].length-1 && prev != 0) {
  174. candidates.add(1);
  175. }
  176. if (y-2 > 0 && prev != 3) {
  177. candidates.add(2);
  178. }
  179. if (y+2 < mazeGrid_box.length-1 && prev != 2) {
  180. candidates.add(3);
  181. }
  182.  
  183. int move = candidates.get(rand.nextInt(candidates.size()));
  184.  
  185. for (int i = 0; i < 2; i++) {
  186. if (move == 0) x--;
  187. if (move == 1) x++;
  188. if (move == 2) y--;
  189. if (move == 3) y++;
  190.  
  191. trail.add(mazeGrid_box[y][x]);
  192. mazeGrid_box[y][x].setType(FieldType.CANDIDATE);
  193.  
  194. //look for loops and erase back if found
  195. boolean looped = false;
  196. if (mazeGrid_box[y][x+1].getType() == FieldType.CANDIDATE && move != 0) {
  197. x++;
  198. looped = true;
  199. }
  200. else if (mazeGrid_box[y][x-1].getType() == FieldType.CANDIDATE && move != 1) {
  201. x--;
  202. looped = true;
  203. }
  204. else if (mazeGrid_box[y+1][x].getType() == FieldType.CANDIDATE && move != 2) {
  205. y++;
  206. looped = true;
  207. }
  208. else if (mazeGrid_box[y-1][x].getType() == FieldType.CANDIDATE && move != 3) {
  209. y--;
  210. looped = true;
  211. }
  212. else if (mazeGrid_box[y-1][x-1].getType() == FieldType.CANDIDATE && mazeGrid_box[y-1][x-1] != trail.get(trail.size()-3)) {
  213. x--;
  214. y--;
  215. looped = true;
  216. }
  217. else if (mazeGrid_box[y-1][x+1].getType() == FieldType.CANDIDATE && mazeGrid_box[y-1][x+1] != trail.get(trail.size()-3)) {
  218. x++;
  219. y--;
  220. looped = true;
  221. }
  222. else if (mazeGrid_box[y+1][x-1].getType() == FieldType.CANDIDATE && mazeGrid_box[y+1][x-1] != trail.get(trail.size()-3)) {
  223. x--;
  224. y++;
  225. looped = true;
  226. }
  227. else if (mazeGrid_box[y+1][x+1].getType() == FieldType.CANDIDATE && mazeGrid_box[y+1][x+1] != trail.get(trail.size()-3)) {
  228. x++;
  229. y++;
  230. looped = true;
  231. }
  232.  
  233. if (looped) {
  234. int retrace = trail.indexOf(mazeGrid_box[y][x]);
  235.  
  236. for (int j = retrace + 1; j < trail.size(); j++) {
  237. trail.get(j).setType(FieldType.OUT_OF_MAZE);
  238. mazeGrid_box[trail.get(j).getX()][trail.get(j).getY()].setType(FieldType.OUT_OF_MAZE);
  239. }
  240.  
  241. trail.subList(retrace + 1, trail.size()).clear();
  242. moveLog.subList(retrace/2 + 1, moveLog.size()).clear();
  243.  
  244. break;
  245. }
  246.  
  247. //look for connections and commit if found
  248. if ( i == 0 && (mazeGrid_box[y][x-1].getType() == FieldType.IN_MAZE ||
  249. mazeGrid_box[y][x+1].getType() == FieldType.IN_MAZE ||
  250. mazeGrid_box[y-1][x].getType() == FieldType.IN_MAZE ||
  251. mazeGrid_box[y+1][x].getType() == FieldType.IN_MAZE)) {
  252.  
  253.  
  254. for(int k=0;k<rowNum-1;k++) {
  255. for(int l=0;l<colNum-1;l++) {
  256. for (Box b : trail) {
  257. b.setType(FieldType.IN_MAZE);
  258. if(mazeGrid_box[k][l].getX() == b.getX() && mazeGrid_box[k][l].getY() == b.getY()){
  259. mazeGrid_box[k][l].setType(FieldType.IN_MAZE);
  260. }
  261. }
  262. }
  263. }
  264. return;
  265. }
  266. }
  267. moveLog.add(move);
  268. }
  269. }
  270.  
  271. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement