将随机迷宫生成纳入我的游戏(Java)

时间:2015-04-16 02:20:56

标签: java algorithm

我目前正在使用Java制作一个迷宫解决游戏,目前我正陷入困境。我能找到的所有随机迷宫生成算法以我无法弄清楚如何实现到当前代码的方式输出。我正在考虑使用Depth First SearchRecursive BacktrackerPrim's Algorithm,因为我认为它们最容易实现,同时仍会产生良好的迷宫。那些与我当前程序一起使用的算法之一的工作用途是什么?这是我的游戏类:(随意指出任何不良做法,我对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;
        }
    }
}

1 个答案:

答案 0 :(得分:2)

作为开始我会

  1. 清除迷宫
  2. 随意添加墙壁
  3. 为所有进入/退出点对创建随机路径

    • 沿着它们清除迷宫细胞。
  4. 如果你只需要迷宫解算器(迷宫生成的一些算法需要它们)

    使用求解器可以将算法更改为

    1. 清除迷宫
    2. 添加随机墙

      • 如果添加它仍然提供解决方案
    3. 循环项目符号(2)N次

    4. 你需要注意如何添加墙壁

      • 例如,如果你只添加简单的线条,那么迷宫将如下所示:
      • maze example

      这是它的代码(使用链接答案中的类)

      // 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;
          }
      

      由此代码生成:

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