Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.ArrayList;
- import java.util.Random;
- import javafx.scene.shape.Rectangle;
- import javafx.application.Application;
- import javafx.scene.Node;
- import javafx.scene.Scene;
- import javafx.scene.layout.GridPane;
- import javafx.scene.paint.Color;
- import javafx.stage.Stage;
- public class Window extends Application{
- private static final long DELAY = 10;
- public int rowNum = 50;
- public int colNum = 50;
- public Box[][] mazeGrid_box;
- public int nextPosX, nextPosY;
- public static void main(String[] args) {
- launch(args);
- }
- @Override
- public void start(Stage primaryStage) throws Exception {
- primaryStage.setTitle("Maciej Lacwik || maze generator + pathfinding");
- primaryStage.setHeight(600);
- primaryStage.setWidth(600);
- GridPane grid = new GridPane();
- setMazeToStart();
- setGridToStart(grid);
- generateMaze();
- for(int i=0;i<rowNum;i++) {
- for(int j=0;j<colNum;j++) {
- if(mazeGrid_box[i][j].getType() == FieldType.IN_MAZE) {
- colorField(i, j, grid, Color.WHITE);
- }
- }
- }
- Scene scene = new Scene(grid, 500, 500);
- primaryStage.setScene(scene);
- primaryStage.show();
- }
- /*
- * access specify rectangle and color it
- */
- public void colorField(int x_, int y_, GridPane grid_, Color newColor) {
- for (Node child : grid_.getChildren()) {
- Integer r = GridPane.getRowIndex(child);
- Integer c = GridPane.getColumnIndex(child);
- int row = r == null ? 0 : r;
- int column = c == null ? 0 : c;
- if (row == x_ && column == y_ && (child instanceof Rectangle)) {
- Rectangle rect = (Rectangle) child;
- rect.setFill(newColor);
- break;
- }
- }
- }
- /*
- * filling GridPane with black rectangles
- */
- public void setGridToStart(GridPane grid_) {
- for (int row = 0; row < rowNum; row++) {
- for (int col = 0; col < colNum; col++) {
- Rectangle rec = new Rectangle();
- rec.setWidth(10);
- rec.setHeight(10);
- rec.setFill(Color.BLACK);
- GridPane.setRowIndex(rec, row);
- GridPane.setColumnIndex(rec, col);
- grid_.getChildren().addAll(rec);
- }
- }
- }
- /*
- * setting Box grid to OUT_OF_MAZE except left bottom corner
- */
- public void setMazeToStart() {
- mazeGrid_box = new Box[rowNum][colNum];
- for(int i=0;i<rowNum;i++) {
- for(int j=0;j<colNum;j++) {
- mazeGrid_box[i][j] = new Box(i,j,FieldType.OUT_OF_MAZE);
- //mazeGrid_box[i][j].setType(FieldType.OUT_OF_MAZE);
- }
- }
- mazeGrid_box[rowNum-1][0].setType(FieldType.IN_MAZE);
- }
- /*
- * main loop with logic functions
- */
- public void generateMaze() {
- nextPosX = colNum;
- nextPosY = 5;
- while (nextPosX > colNum && nextPosY > rowNum) {
- erasedLoopWalk(nextPosX, nextPosY);
- findNextStartCell(nextPosX,nextPosY);
- }
- }
- /*
- * search bottom to top, left to right, for a black cell with two adjacent black cells
- */
- private void findNextStartCell(int startX, int startY) {
- boolean firstLoop = true;
- for (int y = mazeGrid_box.length-2; y >= 1; y--) {
- for (int x = 1; x <= mazeGrid_box[y].length-2; x++) {
- if (firstLoop) {
- x = startX;
- y = startY;
- firstLoop = false;
- }
- int numberOfBlackFields = 0;
- if (mazeGrid_box[y][x-1].getType() == FieldType.OUT_OF_MAZE) numberOfBlackFields++;
- if (mazeGrid_box[y][x+1].getType() == FieldType.OUT_OF_MAZE) numberOfBlackFields++;
- if (mazeGrid_box[y-1][x].getType() == FieldType.OUT_OF_MAZE) numberOfBlackFields++;
- if (mazeGrid_box[y+1][x].getType() == FieldType.OUT_OF_MAZE) numberOfBlackFields++;
- if (mazeGrid_box[y-1][x-1].getType() == FieldType.OUT_OF_MAZE) numberOfBlackFields++;
- if (mazeGrid_box[y-1][x+1].getType() == FieldType.OUT_OF_MAZE) numberOfBlackFields++;
- if (mazeGrid_box[y+1][x-1].getType() == FieldType.OUT_OF_MAZE) numberOfBlackFields++;
- if (mazeGrid_box[y+1][x+1].getType() == FieldType.OUT_OF_MAZE) numberOfBlackFields++;
- if (numberOfBlackFields == 8) {
- nextPosX = x;
- nextPosY = y;
- }
- }
- }
- }
- private void erasedLoopWalk(int x, int y) {
- ArrayList<Box> trail = new ArrayList<Box>();
- ArrayList<Integer> moveLog = new ArrayList<Integer>();
- Random rand = new Random();
- trail.add(mazeGrid_box[y][x]);
- mazeGrid_box[y][x].setType(FieldType.CANDIDATE);
- moveLog.add(-1);//placeholder that hopefully won't break anything
- while (true) {
- //decide candidates based on borders and previous move
- ArrayList<Integer> candidates = new ArrayList<Integer>();
- int prev = moveLog.get(moveLog.size()-1);
- if (x-2 > 0 && prev != 1) {
- candidates.add(0);
- }
- if (x+2 < mazeGrid_box[0].length-1 && prev != 0) {
- candidates.add(1);
- }
- if (y-2 > 0 && prev != 3) {
- candidates.add(2);
- }
- if (y+2 < mazeGrid_box.length-1 && prev != 2) {
- candidates.add(3);
- }
- int move = candidates.get(rand.nextInt(candidates.size()));
- for (int i = 0; i < 2; i++) {
- if (move == 0) x--;
- if (move == 1) x++;
- if (move == 2) y--;
- if (move == 3) y++;
- trail.add(mazeGrid_box[y][x]);
- mazeGrid_box[y][x].setType(FieldType.CANDIDATE);
- //look for loops and erase back if found
- boolean looped = false;
- if (mazeGrid_box[y][x+1].getType() == FieldType.CANDIDATE && move != 0) {
- x++;
- looped = true;
- }
- else if (mazeGrid_box[y][x-1].getType() == FieldType.CANDIDATE && move != 1) {
- x--;
- looped = true;
- }
- else if (mazeGrid_box[y+1][x].getType() == FieldType.CANDIDATE && move != 2) {
- y++;
- looped = true;
- }
- else if (mazeGrid_box[y-1][x].getType() == FieldType.CANDIDATE && move != 3) {
- y--;
- looped = true;
- }
- else if (mazeGrid_box[y-1][x-1].getType() == FieldType.CANDIDATE && mazeGrid_box[y-1][x-1] != trail.get(trail.size()-3)) {
- x--;
- y--;
- looped = true;
- }
- else if (mazeGrid_box[y-1][x+1].getType() == FieldType.CANDIDATE && mazeGrid_box[y-1][x+1] != trail.get(trail.size()-3)) {
- x++;
- y--;
- looped = true;
- }
- else if (mazeGrid_box[y+1][x-1].getType() == FieldType.CANDIDATE && mazeGrid_box[y+1][x-1] != trail.get(trail.size()-3)) {
- x--;
- y++;
- looped = true;
- }
- else if (mazeGrid_box[y+1][x+1].getType() == FieldType.CANDIDATE && mazeGrid_box[y+1][x+1] != trail.get(trail.size()-3)) {
- x++;
- y++;
- looped = true;
- }
- if (looped) {
- int retrace = trail.indexOf(mazeGrid_box[y][x]);
- for (int j = retrace + 1; j < trail.size(); j++) {
- trail.get(j).setType(FieldType.OUT_OF_MAZE);
- mazeGrid_box[trail.get(j).getX()][trail.get(j).getY()].setType(FieldType.OUT_OF_MAZE);
- }
- trail.subList(retrace + 1, trail.size()).clear();
- moveLog.subList(retrace/2 + 1, moveLog.size()).clear();
- break;
- }
- //look for connections and commit if found
- if ( i == 0 && (mazeGrid_box[y][x-1].getType() == FieldType.IN_MAZE ||
- mazeGrid_box[y][x+1].getType() == FieldType.IN_MAZE ||
- mazeGrid_box[y-1][x].getType() == FieldType.IN_MAZE ||
- mazeGrid_box[y+1][x].getType() == FieldType.IN_MAZE)) {
- for(int k=0;k<rowNum-1;k++) {
- for(int l=0;l<colNum-1;l++) {
- for (Box b : trail) {
- b.setType(FieldType.IN_MAZE);
- if(mazeGrid_box[k][l].getX() == b.getX() && mazeGrid_box[k][l].getY() == b.getY()){
- mazeGrid_box[k][l].setType(FieldType.IN_MAZE);
- }
- }
- }
- }
- return;
- }
- }
- moveLog.add(move);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement