1、数据结构课程设计迷宫算法的实现JAVA数据结构课程设计走迷宫 学号: 姓名: 日期:6月16日一、题目说明.分别用以下算法实现。并设计图形用户界面提供迷宫大小、入口及出口位置和初始状态等,演示走迷宫的过程和结果。 1.递归算法。 2.使用栈作为辅助结构。 3.使用队列作为辅助结构。二、总体设计方案以及细节设计为实现上述程序功能,主要使用的JAVA AWT和JAVA SWING包import java.awt.*;import javax.swing.*;import hartech.ui.*;3. 本程序包含四个模块:1) 主程序模块:package mg;import java.awt.*;
2、import javax.swing.*;/* * Title: maze Global class * * Description: * * Date: 2006-08-31 */public class Main / _reset 变量用于reset时用 static int rows = 12, cols = 14; static int speed_reset = 50, speed = speed_reset; static JToggleButton buttons; static Walking walking; static boolean brick, brick_reset
3、 = true, true, true, true, true, false, true, true, true, true, true, true, true, true, , true, false, false, false, true, false, true, true, true, true, false, false, false, true, , true, false, true, false, true, false, false, false, false, true, true, false, true, true, , true, false, true, false
4、, true, false, true, true, true, false, true, false, true, false, , true, true, true, false, false, false, true, false, true, false, true, false, true, true, , true, false, true, true, true, true, true, false, true, false, true, false, false, true, , true, false, true, true, true, true, true, false,
5、 true, false, true, false, true, true, , true, false, false, false, false, false, true, true, true, false, true, false, true, false, , true, false, true, true, true, false, false, false, false, false, true, false, true, true, , true, false, true, false, true, false, true, true, true, true, true, fal
6、se, false, true, , true, false, true, false, true, false, true, false, false, false, false, false, true, true, , true, true, true, false, true, true, true, true, true, true, true, false, true, true, ; static JFrame jFrame; static UI ui; public static void main(String args) /启动新线程,创建一个窗口 Runnable() p
7、ublic void run() /J.setLookAndFeel(Metal); jFrame = new JFrame( is there any way to go? Maze - );/建立一个Swing窗体 jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);/单击关闭图标后,程序退出并关闭 / add Main.ui = new UI(); jFrame.add(ui, BorderLayout.CENTER); jFrame.setSize(700, 400); /J.goCenter(jFrame); Main.draw
8、Buttons(); Main.reset(); jFrame.setVisible(true); ); / 用于重置到软件开始 public static void reset() if (walking != null) ; clean(); brick = copyBoolean(brick_reset); speed = speed_reset; ; setBricks(); / 用于清楚已标记上的数字 public static void clean() if (walking != null) ; for (int i = 0; i rows; i+) for (int j = 0
9、; j cols; j+) buttonsij.setText();/清除按钮的数字,设置名字为空 buttonsij.setForeground(null); UI.jLabel_state.setText( Move now?); / 去掉全部砖 public static void blank() if (walking != null) ; for (int i = 0; i rows; i+) for (int j = 0; j cols; j+) buttonsij.setText(); buttonsij.setForeground(null); buttonsij.setSel
10、ected(true); UI.jLabel_state.setText( Move now?); / 重画按钮图,根据rows、cols public static JPanel drawButtons() buttons = new JToggleButtonrowscols; UI.jPanel_map = new JPanel(new GridLayout(rows, cols); for (int i = 0; i rows; i+) for (int j = 0; j cols; j+) buttonsij = new JToggleButton(); UI.jPanel_map.
11、add(buttonsij); , BorderLayout.CENTER); ; return UI.jPanel_map; / 根据brick设置按钮障碍 public static void setBricks() for (int i = 0; i rows; i+) for (int j = 0; j cols; j+) buttonsij.setSelected(brickij); / 根据现在按钮情况设置brick数组,用于move()内前面 public static void readBricks() for (int i = 0; i rows; i+) for (int
12、j = 0; j cols; j+) brickij = buttonsij.isSelected(); / 开始走 public static void move() if (walking != null) ; clean(); readBricks(); /readToFile(); walking = new Walking(brick); /* / 用于把绘制好地图数据写入文件 public static void readToFile() String out = ; for (int i = 0; i rows; i+) out += ; for (int j = 0; j co
13、ls; j+) if (brickij) out += true,; else out += false,; out += ,rn; , E:/dest.txt); */ / 复制二维数组 public static boolean copyBoolean(boolean in) int row = in.length, col = in0.length; boolean out = new booleanrowcol; for (int i = 0; i row; i+) for (int j = 0; j col; j+) outij = inij; return out; import
14、java.awt.*;import javax.swing.*;import hartech.ui.*;/* * Title: maze Global class * * Description: * * Date: 2006-08-31 */public class Main / _reset 变量用于reset时用 static int rows = 12, cols = 14; static int speed_reset = 50, speed = speed_reset; static JToggleButton buttons; static Walking walking; st
15、atic boolean brick, brick_reset = true, true, true, true, true, false, true, true, true, true, true, true, true, true, , true, false, false, false, true, false, true, true, true, true, false, false, false, true, , true, false, true, false, true, false, false, false, false, true, true, false, true, t
16、rue, , true, false, true, false, true, false, true, true, true, false, true, false, true, false, , true, true, true, false, false, false, true, false, true, false, true, false, true, true, , true, false, true, true, true, true, true, false, true, false, true, false, false, true, , true, false, true,
17、 true, true, true, true, false, true, false, true, false, true, true, , true, false, false, false, false, false, true, true, true, false, true, false, true, false, , true, false, true, true, true, false, false, false, false, false, true, false, true, true, , true, false, true, false, true, false, tr
18、ue, true, true, true, true, false, false, true, , true, false, true, false, true, false, true, false, false, false, false, false, true, true, , true, true, true, false, true, true, true, true, true, true, true, false, true, true, ; static JFrame jFrame; static UI ui; public static void main(String a
19、rgs) /启动新线程,创建一个窗口 Runnable() public void run() J.setLookAndFeel(Metal); jFrame = new JFrame( is there any way to go? Maze - );/建立一个Swing窗体 jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);/单击关闭图标后,程序退出并关闭 / add Main.ui = new UI(); jFrame.add(ui, BorderLayout.CENTER); jFrame.setSize(700, 400);
20、J.goCenter(jFrame); Main.drawButtons(); Main.reset(); jFrame.setVisible(true); ); / 用于重置到软件开始 public static void reset() if (walking != null) ; clean(); brick = copyBoolean(brick_reset); speed = speed_reset; ; setBricks(); / 用于清楚已标记上的数字 public static void clean() if (walking != null) ; for (int i =
21、0; i rows; i+) for (int j = 0; j cols; j+) buttonsij.setText();/清除按钮的数字,设置名字为空 buttonsij.setForeground(null); UI.jLabel_state.setText( Move now?); / 去掉全部砖 public static void blank() if (walking != null) ; for (int i = 0; i rows; i+) for (int j = 0; j cols; j+) buttonsij.setText(); buttonsij.setForeg
22、round(null); buttonsij.setSelected(true); UI.jLabel_state.setText( Move now?); / 重画按钮图,根据rows、cols public static JPanel drawButtons() buttons = new JToggleButtonrowscols; UI.jPanel_map = new JPanel(new GridLayout(rows, cols); for (int i = 0; i rows; i+) for (int j = 0; j cols; j+) buttonsij = new JT
23、oggleButton(); UI.jPanel_map.add(buttonsij); , BorderLayout.CENTER); ; return UI.jPanel_map; / 根据brick设置按钮障碍 public static void setBricks() for (int i = 0; i rows; i+) for (int j = 0; j cols; j+) buttonsij.setSelected(brickij); / 根据现在按钮情况设置brick数组,用于move()内前面 public static void readBricks() for (int
24、 i = 0; i rows; i+) for (int j = 0; j cols; j+) brickij = buttonsij.isSelected(); / 开始走 public static void move() if (walking != null) ; clean(); readBricks(); /readToFile(); walking = new Walking(brick); /* / 用于把绘制好地图数据写入文件 public static void readToFile() String out = ; for (int i = 0; i rows; i+)
25、out += ; for (int j = 0; j cols; j+) if (brickij) out += true,; else out += false,; out += ,rn; , E:/dest.txt); */ / 复制二维数组 public static boolean copyBoolean(boolean in) int row = in.length, col = in0.length; boolean out = new booleanrowcol; for (int i = 0; i row; i+) for (int j = 0; j col; j+) outi
26、j = inij; return out; 2) UI模块实现整个控制面板内组件的布局管理;3)Walking模块实现走迷宫的算法;4)Applete模块设置控制面板。三、详细设计1UI模块package mg;import java.awt.*;import ;import javax.swing.*;import ;/* * Title: mazes MainUI * * Description: * * Date: 2006-08-31 */public class UI extends JPanel private static final long serialVersionUID = 008L; static JPanel jPanel_state, jPanel_map, jPanel_control; static JLabel jLabel_state; static JButton jButton_move, jButton_clean, jButton_blank, jButton_reset; static JSlider jSlider;/添加面板,向面板里添
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1