实例介绍
【实例简介】java做的迷宫游戏,可以制动随机生成迷宫,也可以制动寻找最短路径,netbeans制作
【实例截图】
【核心代码】
/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package maze; import java.util.*; import java.awt.*; import javax.swing.*; import java.awt.event.*; /** * * @author Side */ public class Maze extends JFrame { static int max = 51; static int time = 5; static int tab[][]; static Label label[][]; Vector<Integer> vb; Vector<Integer> vt; GridLayout grid; //网格布局的面板 JPanel p; JMenuBar menubar; static JMenuItem createMenu; static JMenuItem DFSMenu; static JMenuItem aboutMenu; Maze(){ setTitle("迷宫游戏"); menubar=new JMenuBar(); createMenu=new JMenuItem("创建迷宫"); DFSMenu=new JMenuItem("寻找路径"); aboutMenu=new JMenuItem("关于"); menubar.add(createMenu); menubar.add(DFSMenu); menubar.add(aboutMenu); setJMenuBar(menubar); vb = new Vector<>(); vt = new Vector<>(); tab = new int[max][max]; p = new JPanel(); grid = new GridLayout(max,max); label = new Label[max][max]; p.setLayout(grid); for(int i=0;i<max;i ) { for(int j=0;j<max;j ) { label[i][j]=new Label(); label[i][j].setBackground(Color.gray); p.add(label[i][j]); } } label[1][0].setBackground(Color.red); label[max-2][max-1].setBackground(Color.red); add(p,BorderLayout.CENTER); setBounds(10,10,810,810); setVisible(true); setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); validate(); } /** * @param args the command line arguments */ public static void main(String[] args) { Maze maze = new Maze(); ActionListener menuListener = e -> { String cmd = e.getActionCommand(); if (cmd.equals("寻找路径")) { maze.DFS(1, 0); } if (cmd.equals("创建迷宫")) { maze.createMaze(); } }; DFSMenu.addActionListener(menuListener); createMenu.addActionListener(menuListener); // maze.createMaze(); // maze.DFS(1, 0); } void createMaze(){ /* vb.clear(); vt.clear(); for(int i=0;i<max;i ) { for(int j=0;j<max;j ) { tab[i][j] = 0; label[i][j].setBackground(Color.gray); p.add(label[i][j]); } } label[1][0].setBackground(Color.red); label[max-2][max-1].setBackground(Color.red); */ // validate(); int fx,tmp; int tmp1 = (int)(Math.random()*((max -1)/2)); int tmp2 = (int)(Math.random()*((max -1)/2)); tmp = (2*tmp1 1) * 1000 (2*tmp2 1); for(int i = 1;i < max-1;i =2){ for(int j = 1; j < max - 1; j ){ if(j%2 == 1){ vb.add(i*1000 j); } } } addInto_vt(tmp); while(!vt.isEmpty()){ Vector<Integer> temp_vec = new Vector<>(); int ran_num = (int)(Math.random()*(vt.size())); tmp = vt.get(ran_num); fx = tmp / 1000000; tmp = tmp % 1000000; tab[tmp/1000][tmp%1000] = 1; label[tmp/1000][tmp%1000].setBackground(Color.WHITE); try { Thread.currentThread().sleep(time);//毫秒 } catch(Exception e){} if(fx == 1){ tab[tmp/1000 1][tmp%1000] = 1; label[tmp/1000 1][tmp%1000].setBackground(Color.WHITE); try { Thread.currentThread().sleep(time);//毫秒 } catch(Exception e){} } if(fx == 2){ tab[tmp/1000][tmp%1000 1] = 1; label[tmp/1000][tmp%1000 1].setBackground(Color.WHITE); try { Thread.currentThread().sleep(time);//毫秒 } catch(Exception e){} } if(fx == 3){ tab[tmp/1000-1][tmp%1000] = 1; label[tmp/1000-1][tmp%1000].setBackground(Color.WHITE); try { Thread.currentThread().sleep(time);//毫秒 } catch(Exception e){} } if(fx == 4){ tab[tmp/1000][tmp%1000-1] = 1; label[tmp/1000][tmp%1000-1].setBackground(Color.WHITE); try { Thread.currentThread().sleep(time);//毫秒 } catch(Exception e){} } addInto_vt(tmp); temp_vec.add(tmp fx*1000000); vt.removeAll(temp_vec); } } void addInto_vt(int n){ Vector<Integer> temp_vec = new Vector<>(); n = n % 1000000; int y = n / 1000; int x = n % 1000; int temp; if( y-2 > 0){ temp = (y-2) * 1000 x; if(vb.contains(temp)){ temp_vec.add(temp); vt.add(temp 1000000); } } if( y 2 < max){ temp = (y 2) * 1000 x; if(vb.contains(temp)){ temp_vec.add(temp); vt.add(temp 3000000); } } if( x-2 > 0){ temp = y * 1000 (x-2); if(vb.contains(temp)){ vt.add(temp 2000000); temp_vec.add(temp); } } if( x 2 < max){ temp = y * 1000 (x 2); if(vb.contains(temp)){ temp_vec.add(temp); vt.add(temp 4000000); } } vb.removeAll(temp_vec); } boolean DFS(int y,int x){ if(y == max-2 && x == max -2){ return true; } if(y-1 > 0 && tab[y-1][x] == 1){ label[y-1][x].setBackground(Color.GREEN); tab[y-1][x] = 2; try { Thread.currentThread().sleep(time);//毫秒 } catch(Exception e){} if(!DFS(y-1,x)){ tab[y-1][x] = 1; label[y-1][x].setBackground(Color.white); }else{ return true; } } if(x-1 > 0 && tab[y][x-1] == 1){ label[y][x-1].setBackground(Color.GREEN); tab[y][x-1] = 2; try { Thread.currentThread().sleep(time);//毫秒 } catch(Exception e){} if(!DFS(y,x-1)){ tab[y][x-1] = 1; label[y][x-1].setBackground(Color.white); }else{ return true; } } if(y 1 < max && tab[y 1][x] == 1){ label[y 1][x].setBackground(Color.GREEN); tab[y 1][x] = 2; try { Thread.currentThread().sleep(time);//毫秒 } catch(Exception e){} if(!DFS(y 1,x)){ tab[y 1][x] = 1; label[y 1][x].setBackground(Color.white); }else{ return true; } } if(x 1 < max && tab[y][x 1] == 1){ label[y][x 1].setBackground(Color.GREEN); tab[y][x 1] = 2; try { Thread.currentThread().sleep(time);//毫秒 } catch(Exception e){} if(!DFS(y,x 1)){ tab[y][x 1] = 1; label[y][x 1].setBackground(Color.white); }else{ return true; } } return false; } }
好例子网口号:伸出你的我的手 — 分享!
相关软件
网友评论
小贴士
感谢您为本站写下的评论,您的评论对其它用户来说具有重要的参考价值,所以请认真填写。
- 类似“顶”、“沙发”之类没有营养的文字,对勤劳贡献的楼主来说是令人沮丧的反馈信息。
- 相信您也不想看到一排文字/表情墙,所以请不要反馈意义不大的重复字符,也请尽量不要纯表情的回复。
- 提问之前请再仔细看一遍楼主的说明,或许是您遗漏了。
- 请勿到处挖坑绊人、招贴广告。既占空间让人厌烦,又没人会搭理,于人于己都无利。
关于好例子网
本站旨在为广大IT学习爱好者提供一个非营利性互相学习交流分享平台。本站所有资源都可以被免费获取学习研究。本站资源来自网友分享,对搜索内容的合法性不具有预见性、识别性、控制性,仅供学习研究,请务必在下载后24小时内给予删除,不得用于其他任何用途,否则后果自负。基于互联网的特殊性,平台无法对用户传输的作品、信息、内容的权属或合法性、安全性、合规性、真实性、科学性、完整权、有效性等进行实质审查;无论平台是否已进行审查,用户均应自行承担因其传输的作品、信息、内容而可能或已经产生的侵权或权属纠纷等法律责任。本站所有资源不代表本站的观点或立场,基于网友分享,根据中国法律《信息网络传播权保护条例》第二十二与二十三条之规定,若资源存在侵权或相关问题请联系本站客服人员,点此联系我们。关于更多版权及免责申明参见 版权及免责申明
支持(0) 盖楼(回复)