⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 automaze.java

📁 迷宫问题即如何走出一个迷宫的问题。此程序可以完成此功能
💻 JAVA
字号:
// 程序:自动产生迷宫
// 范例文件:AutoMaze.java

import java.applet.*;
import java.awt.*;
import java.awt.event.*;

public class AutoMaze extends Applet
{
   int GridX=15,GridY=15;  // 迷宫格数
   int Si,Sj,Ei,Ej;        // 入口与出口
   int[][] maze = new int[30][30]; // 迷宫数组
   int enter=20,width=20;  // 迷宫入口位置与每格的大小

   Button btn; 
   Panel p;
   Image wall1, wall2;
   MediaTracker MT;

   public void init() 
   {
      // 取得外墙图片
      MT           = new MediaTracker(this);
      wall1 = getImage(getDocumentBase(),"Images/wall1.gif");
      wall2 = getImage(getDocumentBase(),"Images/wall2.gif");
      MT.addImage(wall1,0);
      MT.addImage(wall2,0);
     
      try
      {
         showStatus("图像加载中(Loading Images)...");
         MT.waitForAll();
      }
      catch(InterruptedException E){ }   //没有进行异常处理

      btn = new Button("建立迷宫");
      // 按下按钮自动产生迷宫数组并绘制迷宫
      btn.addActionListener(
         new ActionListener() {
            public void actionPerformed(ActionEvent e)
            {
               int i,j;
               Si=1;Sj=1;Ei=GridY;Ej=GridX; // 设定出入口
               for (i=0;i<=GridY+1;i++) {
                  for (j=0;j<=GridX+1;j++) {
                     if (i==0 || j==0 || 
                         i==GridY+1 || j==GridX+1)
                        maze[i][j]=15; // 设定外墙
                     else
                        maze[i][j]=3;  // 初始迷宫内部
                    }
                }
                genmaze(Ei,Ej); // 产生迷宫
                maze[Ei][Ej]=maze[Ei][Ej] & 0xd; // 拆掉入口右墙
                repaint();
            }
         }
      );

      p = new Panel();
      p.add(btn);
      setLayout(new BorderLayout()); // 设定版面配置
      add(p, "South");      
   }

   // 递归产生迷宫数组
   public void genmaze(int i, int j)
   {
      int n;
      maze[i][j] |= 0x4;  //  标示此格已设定
      while (maze[i][j+1]==3 || maze[i+1][j]==3 ||
             maze[i][j-1]==3 || maze[i-1][j]==3) // 如果不是外墙
      {
         n=(int)(4*Math.random()+1);  // 决定下一个位置
         if (n==1 && maze[i][j+1]==3) {  // 向右走
            maze[i][j] &= 0xd; // 拆掉右墙
            genmaze(i,j+1);  // 递归继续走访下一个位置 
            }
         else if (n==2 && maze[i-1][j]==3) { // 向下走
            maze[i][j] &= 0xe;    // 拆掉上墙
            genmaze(i-1,j); // 递归继续走访下一个位置 
         }
         else if (n==3 && maze[i][j-1]==3) { // 向左走
            maze[i][j-1] &= 0xd;  // 拆掉右墙
            genmaze(i,j-1);  // 递归继续走访下一个位置 
         }
         else if (n==4 && maze[i+1][j]==3) { // 向上走
            maze[i+1][j] &= 0xe; // 拆掉上墙
            genmaze(i+1,j); // 递归继续走访下一个位置 
         }
      }      
   }

   // 绘制迷宫
   public void paint(Graphics g)
   {
      int x, y, i, j;
      // 清除屏幕
      g.clearRect(0,0,(GridX+3)*width,(GridY+6)*width);
      // 绘制上外墙
      g.drawImage(wall1,enter,enter,(GridX+1)*width,2*width,0,0,100,100,this);

      // 绘制左外墙
      g.drawImage(wall2,0,2*width,width,(GridY+1)*width+5,0,0,100,100,this);

      // 绘制下外墙
      g.drawImage(wall1,enter,(GridY+1)*width,(GridX+1)*width,(GridY+2)*width,0,0,100,100,this);

      // 绘制右外墙
      g.drawImage(wall2,GridX*width,width,(GridX+1)*width,GridY*width,0,0,100,100,this);

      // 画迷宫内部
      for (i=1;i<=GridY;i++) {
         for (j=1;j<=GridX;j++) {
            x=(j-1)*width+enter;
            y=(i-1)*width+enter;

            // 画上墙
            if ((maze[i][j] & 1)==1) 
               g.drawImage(wall1,x-width/5,y,x+width,y+width,0,0,100,100,this);
            
            // 画右墙
            if ((maze[i][j] & 2)==2) 
               g.drawImage(wall2,x,y,x+width,y+width,0,0,100,100,this);
         }
      }
   }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -