《数据结构课程设计迷宫算法的实现_JAVA精品资料.doc》由会员分享,可在线阅读,更多相关《数据结构课程设计迷宫算法的实现_JAVA精品资料.doc(51页珍藏版)》请在taowenge.com淘文阁网|工程机械CAD图纸|机械工程制图|CAD装配图下载|SolidWorks_CaTia_CAD_UG_PROE_设计图分享下载上搜索。
1、数据结构课程设计走迷宫 学号:200908204136 姓名:熊军 日期: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.*;impor
2、t 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 = true, t
3、rue, 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, true, false
4、, 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, true, false,tru
5、e, 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, false, false, true, , t
6、rue, 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) /启动新线程,创建一个窗口javax.swing.SwingUtilities.invokeLater
7、(new Runnable() public void run() /J.setLookAndFeel(Metal);jFrame = new JFrame(is there any way to go? Maze - );/建立一个Swing窗体jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);/单击关闭图标后,程序退出并关闭/ addMain.ui = new UI();jFrame.add(ui, BorderLayout.CENTER);jFrame.setSize(700, 400);/J.goCenter(jFrame);M
8、ain.drawButtons();Main.reset();jFrame.setVisible(true););/ 用于重置到软件开始public static void reset() if (walking != null) walking.timer.stop();clean();brick = copyBoolean(brick_reset);speed = speed_reset;UI.jSlider.setValue(speed);setBricks();/ 用于清楚已标记上的数字public static void clean() if (walking != null) wa
9、lking.timer.stop();for (int i = 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) walking.timer.stop();for (int i = 0; i rows; i+) for (int j = 0; j co
10、ls; j+) buttonsij.setText();buttonsij.setForeground(null);buttonsij.setSelected(true);UI.jLabel_state.setText( Move now?);/ 重画按钮图,根据rows、colspublic static JPanel drawButtons() buttons = new JToggleButtonrowscols;UI.jPanel_map = new JPanel(new GridLayout(rows, cols);for (int i = 0; i rows; i+) for (i
11、nt j = 0; j cols; j+) buttonsij = new JToggleButton();UI.jPanel_map.add(buttonsij);Main.ui.add(UI.jPanel_map, BorderLayout.CENTER);Main.ui.setVisible(true);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
12、(brickij);/ 根据现在按钮情况设置brick数组,用于move()内前面public static void readBricks() for (int i = 0; i rows; i+) for (int j = 0; j cols; j+) brickij = buttonsij.isSelected();/ 开始走public static void move() if (walking != null) walking.timer.stop();clean();readBricks();/readToFile();walking = new Walking(brick);/
13、* / 用于把绘制好地图数据写入文件 public static void readToFile() String out = ; for (int i = 0; i rows; i+) out += ; for (int j = 0; j cols; j+) if (brickij) out += true,; else out += false,; out += ,rn; hartech.JFile.stringToFile(out, E:/dest.txt); */ 复制二维数组public static boolean copyBoolean(boolean in) int row =
14、 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 java.awt.*;import javax.swing.*;import hartech.ui.*;/* * Title: maze Global class * * Description: * * Date: 2006-08-31 */public class Main / _reset 变量用于r
15、eset时用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 = true, true, true, true, true, false, true, true, true, true,true, true, true, true, , true, false, false, false, true, false,
16、 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, true, false, true, true, true, false,true, false, true, false, , true, true, true, false, false, false, true, false, true, false,
17、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, true, false,true, false, true, true, , true, false, false, false, false, false, true, true, true, false,true, false, true, false, , t
18、rue, false, true, true, true, false, false, false, false, false,true, false, true, true, , true, false, true, false, true, false, true, 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, t
19、rue, true, true, true, true, true,true, false, true, true, ;static JFrame jFrame;static UI ui;public static void main(String args) /启动新线程,创建一个窗口javax.swing.SwingUtilities.invokeLater(new Runnable() public void run() J.setLookAndFeel(Metal);jFrame = new JFrame(is there any way to go? Maze - );/建立一个Sw
20、ing窗体jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);/单击关闭图标后,程序退出并关闭/ addMain.ui = new UI();jFrame.add(ui, BorderLayout.CENTER);jFrame.setSize(700, 400);J.goCenter(jFrame);Main.drawButtons();Main.reset();jFrame.setVisible(true););/ 用于重置到软件开始public static void reset() if (walking != null) walk
21、ing.timer.stop();clean();brick = copyBoolean(brick_reset);speed = speed_reset;UI.jSlider.setValue(speed);setBricks();/ 用于清楚已标记上的数字public static void clean() if (walking != null) walking.timer.stop();for (int i = 0; i rows; i+) for (int j = 0; j cols; j+) buttonsij.setText();/清除按钮的数字,设置名字为空buttonsij.
22、setForeground(null);UI.jLabel_state.setText( Move now?);/ 去掉全部砖public static void blank() if (walking != null) walking.timer.stop();for (int i = 0; i rows; i+) for (int j = 0; j cols; j+) buttonsij.setText();buttonsij.setForeground(null);buttonsij.setSelected(true);UI.jLabel_state.setText( Move now?
23、);/ 重画按钮图,根据rows、colspublic 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.add(buttonsij);Main.ui.add(UI.jPanel_map, BorderLayo
24、ut.CENTER);Main.ui.setVisible(true);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 j = 0; j c
25、ols; j+) brickij = buttonsij.isSelected();/ 开始走public static void move() if (walking != null) walking.timer.stop();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
26、 cols; j+) if (brickij) out += true,; else out += false,; out += ,rn; hartech.JFile.stringToFile(out, 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
27、 = inij;return out;2) UI模块实现整个控制面板内组件的布局管理;3)Walking模块实现走迷宫的算法;4)Applete模块设置控制面板。三、详细设计1UI模块package mg;import java.awt.*;import java.awt.event.*;import javax.swing.*;import javax.swing.event.*;/* * Title: mazes MainUI * * Description: * * Date: 2006-08-31 */public class UI extends JPanel private sta
28、tic final long serialVersionUID = 5924032800440919028L;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;/添加面板,向面板里添加各个组件public UI() super(new BorderLayout();/ addadd(jPane
29、l_control(), BorderLayout.SOUTH);/布局管理器,从左到右设置按钮public static JPanel jPanel_control() jLabel_state = new JLabel( Move now?);jLabel_state.setHorizontalTextPosition(JLabel.LEFT);/将Move now标签添加/在面板的左下角jSlider = new JSlider(JSlider.HORIZONTAL, 5, 400, Main.speed);/建立一个水平方向的滑竿jSlider.setPreferredSize(new
30、 Dimension(5, 5);/滑杆的大小jSlider.setBackground(new Color(208, 220, 255);jSlider.addChangeListener(new ChangeListener() public void stateChanged(ChangeEvent e) Main.speed = (JSlider) e.getSource().getValue();/处理changeEvent时间,当用户滑动杆时速度会改变);/为各个按钮添加监视器jButton_move = new JButton(Move!);jButton_move.addAct
31、ionListener(new ActionListener_button();jButton_move.setActionCommand(move);jButton_clean = new JButton(Clean);jButton_clean.addActionListener(new ActionListener_button();jButton_clean.setActionCommand(clean);jButton_blank = new JButton(Blank);jButton_blank.addActionListener(new ActionListener_butto
32、n();jButton_blank.setActionCommand(blank);jButton_reset = new JButton(Reset);jButton_reset.addActionListener(new ActionListener_button();jButton_reset.setActionCommand(reset);jPanel_control = new JPanel();/ Option: X_AXIS Y_AXIS LINE_AXIS PAGE_AXISjPanel_control.setLayout(new BoxLayout(jPanel_contro
33、l, BoxLayout.X_AXIS);/jPanel_control = new JPanel(new FlowLayout(FlowLayout.RIGHT);jPanel_control.setBackground(new Color(208, 220, 255);jPanel_control.add(jLabel_state);jPanel_control.add(Box.createHorizontalGlue();/从左到右设置按钮jPanel_control.add(jSlider);jPanel_control.add(jButton_move);jPanel_control
34、.add(jButton_clean);jPanel_control.add(jButton_blank);jPanel_control.add(jButton_reset);return jPanel_control;/监视器接口static class ActionListener_button implements ActionListener public void actionPerformed(ActionEvent e) if (e.getActionCommand().equals(move) Main.move(); else if (e.getActionCommand()
35、.equals(clean) Main.clean(); else if (e.getActionCommand().equals(blank) Main.blank(); else if (e.getActionCommand().equals(reset) Main.reset();package hartech.kids.maze;import java.awt.*;import java.awt.event.*;import javax.swing.*;import javax.swing.event.*;/* * Title: mazes MainUI * * Description
36、: * * Date: 2006-08-31 */public class UI extends JPanel private static final long serialVersionUID = 5924032800440919028L;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;
37、/添加面板,向面板里添加各个组件public UI() super(new BorderLayout();/ addadd(jPanel_control(), BorderLayout.SOUTH);/布局管理器,从左到右设置按钮public static JPanel jPanel_control() jLabel_state = new JLabel( Move now?);jLabel_state.setHorizontalTextPosition(JLabel.LEFT);/将Move now标签添加/在面板的左下角jSlider = new JSlider(JSlider.HORIZ
38、ONTAL, 5, 400, Main.speed);/建立一个水平方向的滑竿jSlider.setPreferredSize(new Dimension(5, 5);/滑杆的大小jSlider.setBackground(new Color(208, 220, 255);jSlider.addChangeListener(new ChangeListener() public void stateChanged(ChangeEvent e) Main.speed = (JSlider) e.getSource().getValue();/处理changeEvent时间,当用户滑动杆时速度会改
39、变);/为各个按钮添加监视器jButton_move = new JButton(Move!);jButton_move.addActionListener(new ActionListener_button();jButton_move.setActionCommand(move);jButton_clean = new JButton(Clean);jButton_clean.addActionListener(new ActionListener_button();jButton_clean.setActionCommand(clean);jButton_blank = new JButton(Blank);jButton_blank.addActionListener(new ActionListener_button();jButton_blank.setActionCommand(blank);jButton_reset = new JButton(Reset);jButton_reset.addActionListener(new ActionListener_button();jButton_reset.setActionCommand(reset);jPanel