Including a random generation of mazes in my game (Java)

I am currently creating a game for solving mazes in Java, and currently I'm confused. All the random labyrinth generation algorithms that I could find the output in such a way that I could not figure out how to implement in my current code. I considered using Depth First Search , Recursive Backtracker, or Prima Algorithm , as I thought they would be the easiest to implement, while still creating nice mazes. What will be the use of one of those algorithms that work with my current program? This is my game class: (Feel free to point out any bad practices, I'm pretty new to Java)

package game; import javax.swing.*; import java.awt.*; import java.awt.event.*; public class Game extends JPanel implements ActionListener, KeyListener { private boolean upPressed = false; private boolean downPressed = false; private boolean rightPressed = false; private boolean leftPressed = false; private final int playerDiam = 100; private final int playerSpeed = 15; private final int tileSize = 400; private int[][] maze = {{1, 1, 1, 1, 1, 1}, {1, 2, 1, 1, 3, 1}, {1, 0, 1, 0, 0, 1}, {1, 0, 1, 0, 1, 1}, {1, 0, 0, 0, 1, 1}, {1, 1, 1, 1, 1, 1}, }; private int[][] initX = new int[maze.length][maze.length]; private int[][] initY = new int[maze.length][maze.length]; private int deltaX = -210; private int deltaY = -210; private String screen = "menu"; public Game() { setFocusable(true); addKeyListener(this); setUpInitialCoordinates(); Timer timer = new Timer(1000 / 60, this); timer.start(); } @Override public void actionPerformed(ActionEvent e) { tick(); } private void setUpInitialCoordinates() { int x = 0; int y; for (int[] rowData : maze) { y = 0; for (int ignored : rowData) { initX[x][y] = x * tileSize; initY[x][y] = y * tileSize; y++; } x++; } } private void generateMaze() { } private void tick() { if (screen.equals("playing")) { if (upPressed) { deltaY += playerSpeed; } else if (downPressed) { deltaY -= playerSpeed; } if (rightPressed) { deltaX -= playerSpeed; } else if (leftPressed) { deltaX += playerSpeed; } } repaint(); } @Override public void keyTyped(KeyEvent e) {} @Override public void keyPressed(KeyEvent e) { if (e.getKeyCode() == KeyEvent.VK_UP || e.getKeyCode() == KeyEvent.VK_W) { upPressed = true; } else if (e.getKeyCode() == KeyEvent.VK_DOWN || e.getKeyCode() == KeyEvent.VK_S) { downPressed = true; } else if (e.getKeyCode() == KeyEvent.VK_RIGHT || e.getKeyCode() == KeyEvent.VK_D) { rightPressed = true; } else if (e.getKeyCode() == KeyEvent.VK_LEFT || e.getKeyCode() == KeyEvent.VK_A) { leftPressed = true; } } @Override public void keyReleased(KeyEvent e) { if (screen.equals("menu") && e.getKeyCode() == KeyEvent.VK_ENTER) { upPressed = false; downPressed = false; rightPressed = false; leftPressed = false; screen = "playing"; } else if (screen.equals("playing")) { if (e.getKeyCode() == KeyEvent.VK_UP || e.getKeyCode() == KeyEvent.VK_W) { upPressed = false; } else if (e.getKeyCode() == KeyEvent.VK_DOWN || e.getKeyCode() == KeyEvent.VK_S) { downPressed = false; } else if (e.getKeyCode() == KeyEvent.VK_RIGHT || e.getKeyCode() == KeyEvent.VK_D) { rightPressed = false; } else if (e.getKeyCode() == KeyEvent.VK_LEFT || e.getKeyCode() == KeyEvent.VK_A) { leftPressed = false; } else if (e.getKeyCode() == KeyEvent.VK_P) { screen = "paused"; } } else if (screen.equals("paused" ) && e.getKeyCode() == KeyEvent.VK_P) { upPressed = false; downPressed = false; rightPressed = false; leftPressed = false; screen = "playing"; } } @Override public void paintComponent(Graphics g) { super.paintComponent(g); g.setFont(new Font("Aharoni", Font.PLAIN, 36)); g.setColor(Color.WHITE); g.fillRect(0, 0, getWidth(), getHeight()); switch (screen) { case "menu": g.setColor(Color.BLACK); g.drawString("Labyrinth", 300, 200); g.drawString("Press Enter to Play!", getWidth() / 3, 500); break; case "playing": int x = 0; int y = 0; for (int[] rowData : maze) { for (int cellData : rowData) { if (cellData == 1) { g.setColor(Color.DARK_GRAY); g.fillRect(x + deltaX, y + deltaY, tileSize, tileSize); } else if (cellData == 2) { g.setColor(Color.GREEN); g.fillRect(x + deltaX, y + deltaY, tileSize, tileSize); } else if (cellData == 3) { g.setColor(Color.YELLOW); g.fillRect(x + deltaX, y + deltaY, tileSize, tileSize); } x += tileSize; if (x == maze.length * tileSize) { x = 0; y += tileSize; } } } g.setColor(Color.RED); g.fillOval(getWidth() / 2, getHeight() / 2, playerDiam, playerDiam); break; case "gameOver": g.setColor(Color.BLACK); g.drawString("Game Over",getWidth() / 3 ,50 ); break; case "paused": g.setColor(Color.BLACK); g.drawString("Paused", getWidth() / 3, 50); break; } } } 
+6
source share
1 answer

as a beginning, I would

  • clean maze
  • randomly add walls
  • create a random path for all input / output point pairs present

    • Clean the labyrinth cells along them.

If you just need a maze solver (some algos need them to generate a maze)

using the solver you can change the algorithm to

  • clean maze
  • add random wall

    • if the add still provides a solution
  • loop bullet (2) N times

You need to be careful to add walls.

  • For example, if you add only simple lines, the maze will look like this:
  • maze example

This is the code for it (uses the class from the linked answer)

 // generate random maze with path from x0,y0 to x1,y1 present, n walls void A_star::generate(int x0,int y0,int x1,int y1,int n) { int x,y,i,j,dx,dy,l,*p; // [clear map] for (y=0;y<ys;y++) for (x=0;x<xs;x++) map[y][x]=A_star_space; // temp space p=new int [xs>>1]; if (p==NULL) return; // generate n walls for (i=0;i<n;i++) { // random start pos,dir,length x =Random(xs); y =Random(ys); dx=Random(4); l =Random(xs>>2)+2; if (dx==0) { dx=+1; dy= 0; } else if (dx==1) { dx=-1; dy= 0; } else if (dx==2) { dx= 0; dy=+1; } else if (dx==3) { dx= 0; dy=-1; } // add wall to maze remember set cells (for remowal if needed) for (j=0;l;l--,x+=dx,y+=dy) if ((x>=0)&&(x<xs)) if ((y>=0)&&(y<ys)) if (map[y][x]!=A_star_wall) { p[j]=x; j++; p[j]=y; j++; map[y][x]=A_star_wall; } // is there solution? compute(x0,y0,x1,y1); // if not remowe last added wall if (ps==0) for (;j;) { j--; y=p[j]; j--; x=p[j]; map[y][x]=A_star_space; } } delete[] p; } 

generated by this code:

  A_star map; map.resize(256,256); map.generate(5,5,250,250,500); 
+2
source

Source: https://habr.com/ru/post/985231/


All Articles