找传奇、传世资源到传世资源站!

迷宫随机生成和寻找最短路径

8.5玩家评分(1人评分)
下载后可评
介绍 评论 失效链接反馈

java做的迷宫游戏,可以制动随机生成迷宫,也可以制动寻找最短路径,netbeans制作
迷宫随机生成和寻找最短路径 Java-第1张
/* * 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; }}

评论

发表评论必须先登陆, 您可以 登陆 或者 注册新账号 !


在线咨询: 问题反馈
客服QQ:174666394

有问题请留言,看到后及时答复