基本信息
源码名称:迷宫随机生成和寻找最短路径
源码大小:1.18M
文件格式:.zip
开发语言:Java
更新时间:2018-05-07
友情提示:(无需注册或充值,赞助后即可获取资源下载链接)
嘿,亲!知识可是无价之宝呢,但咱这精心整理的资料也耗费了不少心血呀。小小地破费一下,绝对物超所值哦!如有下载和支付问题,请联系我们QQ(微信同号):813200300
本次赞助数额为: 2 元×
微信扫码支付:2 元
×
请留下您的邮箱,我们将在2小时内将文件发到您的邮箱
源码介绍
java做的迷宫游戏,可以制动随机生成迷宫,也可以制动寻找最短路径,netbeans制作
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; } }