当前位置:首页 » 编程语言 » 迷宫java代码

迷宫java代码

发布时间: 2023-09-05 00:28:27

java怎么生成迷宫地图

//作者:zhongZw

  • packagecn.zhongZw.model;

  • importjava.util.ArrayList;

  • importjava.util.Random;

  • publicclassMazeModel{

  • privateintwidth=0;

  • privateintheight=0;

  • privateRandomrnd=newRandom();

  • publicMazeModel(){

  • this.width=50;//迷宫宽度

  • this.height=50;//迷宫高度

  • }

  • publicintgetWidth(){

  • returnwidth;

  • }

  • publicvoidsetWidth(intwidth){

  • this.width=width;

  • }

  • publicintgetHeight(){

  • returnheight;

  • }

  • publicvoidsetHeight(intheight){

  • this.height=height;

  • }

  • publicMazeModel(intwidth,intheight){

  • super();

  • this.width=width;

  • this.height=height;

  • }

  • publicArrayList<MazePoint>getMaze(){

  • ArrayList<MazePoint>maze=newArrayList<MazePoint>();

  • for(inth=0;h<height;h++){

  • for(intw=0;w<width;w++){

  • MazePointpoint=newMazePoint(w,h);

  • maze.add(point);

  • }

  • }

  • returnCreateMaze(maze);

  • }

  • privateArrayList<MazePoint>CreateMaze(ArrayList<MazePoint>maze){

  • inttop=0;

  • intx=0;

  • inty=0;

  • ArrayList<MazePoint>team=newArrayList<MazePoint>();

  • team.add(maze.get(x+y*width));

  • while(top>=0){

  • int[]val=newint[]{

  • -1,-1,-1,-1

  • };

  • inttimes=0;

  • booleanflag=false;

  • MazePointpt=(MazePoint)team.get(top);

  • x=pt.getX();

  • y=pt.getY();

  • pt.visted=true;

  • ro1:while(times<4){

  • intdir=rnd.nextInt(4);

  • if(val[dir]==dir)

  • continue;

  • else

  • val[dir]=dir;

  • switch(dir){

  • case0://左边

  • if((x-1)>=0&&maze.get(x-1+y*width).visted==false){

  • maze.get(x+y*width).setLeft();

  • maze.get(x-1+y*width).setRight();

  • team.add(maze.get(x-1+y*width));

  • top++;

  • flag=true;

  • breakro1;

  • }

  • break;

  • case1://右边

  • if((x+1)<width&&maze.get(x+1+y*width).visted==false){

  • maze.get(x+y*width).setRight();

  • maze.get(x+1+y*width).setLeft();

  • team.add(maze.get(x+1+y*width));

  • top++;

  • flag=true;

  • breakro1;

  • }

  • break;

  • case2://上边

  • if((y-1)>=0&&maze.get(x+(y-1)*width).visted==false){

  • maze.get(x+y*width).setUp();

  • maze.get(x+(y-1)*width).setDown();

  • team.add(maze.get(x+(y-1)*width));

  • top++;

  • flag=true;

  • breakro1;

  • }

  • break;

  • case3://下边

  • if((y+1)<height&&maze.get(x+(y+1)*width).visted==false){

  • maze.get(x+y*width).setDown();

  • maze.get(x+(y+1)*width).setUp();

  • team.add(maze.get(x+(y+1)*width));

  • top++;

  • flag=true;

  • breakro1;

  • }

  • break;

  • }

  • times+=1;

  • }

  • if(!flag){

  • team.remove(top);

  • top-=1;

  • }

  • }

  • returnmaze;

  • }

  • }

  • 迷宫
  • [java]view plain

  • //作者:zhongZw

  • //邮箱:[email protected]

  • packagecn.zhongZw.model;

  • importjava.util.*;

  • importjava.lang.*;

  • publicclassMazePoint{

  • privateintleft=0;

  • privateintright=0;

  • privateintup=0;

  • privateintdown=0;

  • privateintx;

  • privateinty;

  • publicbooleanvisted;

  • publicMazePoint(intx,inty){

  • this.x=x;

  • this.y=y;

  • }

  • publicintgetLeft(){

  • returnleft;

  • }

  • publicvoidsetLeft(){

  • this.left=1;

  • }

  • publicintgetRight(){

  • returnright;

  • }

  • publicvoidsetRight(){

  • this.right=1;

  • }

  • publicintgetUp(){

  • returnup;

  • }

  • publicvoidsetUp(){

  • this.up=1;

  • }

  • publicintgetDown(){

  • returndown;

  • }

  • publicvoidsetDown(){

  • this.down=1;

  • }

  • publicintgetX(){

  • returnx;

  • }

  • publicvoidsetX(intx){

  • this.x=x;

  • }

  • publicintgetY(){

  • returny;

  • }

  • publicvoidsetY(inty){

  • this.y=y;

  • }

  • }

⑵ 急求java课程设计,内容可以是小游戏的,如(迷宫,计算器,停车场之恋的),要能运行,谢谢

连连看java源代码
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class lianliankan implements ActionListener
{
JFrame mainFrame; //主面板
Container thisContainer;
JPanel centerPanel,southPanel,northPanel; //子面板
JButton diamondsButton[][] = new JButton[6][5];//游戏按钮数组
JButton exitButton,resetButton,newlyButton; //退出,重列,重新开始按钮
JLabel fractionLable=new JLabel("0"); //分数标签
JButton firstButton,secondButton; //分别记录两次被选中的按钮
int grid[][] = new int[8][7];//储存游戏按钮位置
static boolean pressInformation=false; //判断是否有按钮被选中
int x0=0,y0=0,x=0,y=0,fristMsg=0,secondMsg=0,validateLV; //游戏按钮的位置坐标
int i,j,k,n;//消除方法控制
public void init(){
mainFrame=new JFrame("JKJ连连看");
thisContainer = mainFrame.getContentPane();
thisContainer.setLayout(new BorderLayout());
centerPanel=new JPanel();
southPanel=new JPanel();
northPanel=new JPanel();
thisContainer.add(centerPanel,"Center");
thisContainer.add(southPanel,"South");
thisContainer.add(northPanel,"North");
centerPanel.setLayout(new GridLayout(6,5));
for(int cols = 0;cols < 6;cols++){
for(int rows = 0;rows < 5;rows++ ){
diamondsButton[cols][rows]=new JButton(String.valueOf(grid[cols+1][rows+1]));
diamondsButton[cols][rows].addActionListener(this);
centerPanel.add(diamondsButton[cols][rows]);
}
}
exitButton=new JButton("退出");
exitButton.addActionListener(this);
resetButton=new JButton("重列");
resetButton.addActionListener(this);
newlyButton=new JButton("再来一局");
newlyButton.addActionListener(this);
southPanel.add(exitButton);
southPanel.add(resetButton);
southPanel.add(newlyButton);
fractionLable.setText(String.valueOf(Integer.parseInt(fractionLable.getText())));
northPanel.add(fractionLable);
mainFrame.setBounds(280,100,500,450);
mainFrame.setVisible(true);
}
public void randomBuild() {
int randoms,cols,rows;
for(int twins=1;twins<=15;twins++) {
randoms=(int)(Math.random()*25+1);
for(int alike=1;alike<=2;alike++) {
cols=(int)(Math.random()*6+1);
rows=(int)(Math.random()*5+1);
while(grid[cols][rows]!=0) {
cols=(int)(Math.random()*6+1);
rows=(int)(Math.random()*5+1);
}
this.grid[cols][rows]=randoms;
}
}
}
public void fraction(){
fractionLable.setText(String.valueOf(Integer.parseInt(fractionLable.getText())+100));
}
public void reload() {
int save[] = new int[30];
int n=0,cols,rows;
int grid[][]= new int[8][7];
for(int i=0;i<=6;i++) {
for(int j=0;j<=5;j++) {
if(this.grid[i][j]!=0) {
save[n]=this.grid[i][j];
n++;
}
}
}
n=n-1;
this.grid=grid;
while(n>=0) {
cols=(int)(Math.random()*6+1);
rows=(int)(Math.random()*5+1);
while(grid[cols][rows]!=0) {
cols=(int)(Math.random()*6+1);
rows=(int)(Math.random()*5+1);
}
this.grid[cols][rows]=save[n];
n--;
}
mainFrame.setVisible(false);
pressInformation=false; //这里一定要将按钮点击信息归为初始
init();
for(int i = 0;i < 6;i++){
for(int j = 0;j < 5;j++ ){
if(grid[i+1][j+1]==0)
diamondsButton[i][j].setVisible(false);
}
}
}
public void estimateEven(int placeX,int placeY,JButton bz) {
if(pressInformation==false) {
x=placeX;
y=placeY;
secondMsg=grid[x][y];
secondButton=bz;
pressInformation=true;
}
else {
x0=x;
y0=y;
fristMsg=secondMsg;
firstButton=secondButton;
x=placeX;
y=placeY;
secondMsg=grid[x][y];
secondButton=bz;
if(fristMsg==secondMsg && secondButton!=firstButton){
xiao();
}
}
}
public void xiao() { //相同的情况下能不能消去。仔细分析,不一条条注释
if((x0==x &&(y0==y+1||y0==y-1)) || ((x0==x+1||x0==x-1)&&(y0==y))){ //判断是否相邻
remove();
}
else{
for (j=0;j<7;j++ ) {
if (grid[x0][j]==0){ //判断第一个按钮同行哪个按钮为空
if (y>j) { //如果第二个按钮的Y坐标大于空按钮的Y坐标说明第一按钮在第二按钮左边
for (i=y-1;i>=j;i-- ){ //判断第二按钮左侧直到第一按钮中间有没有按钮
if (grid[x][i]!=0) {
k=0;
break;
}
else{ k=1; } //K=1说明通过了第一次验证
}
if (k==1) {
linePassOne();
}
}
if (y<j){ //如果第二个按钮的Y坐标小于空按钮的Y坐标说明第一按钮在第二按钮右边
for (i=y+1;i<=j ;i++ ){ //判断第二按钮左侧直到第一按钮中间有没有按钮
if (grid[x][i]!=0){
k=0;
break;
}
else { k=1; }
}
if (k==1){
linePassOne();
}
}
if (y==j ) {
linePassOne();
}
}
if (k==2) {
if (x0==x) {
remove();
}
if (x0<x) {
for (n=x0;n<=x-1;n++ ) {
if (grid[n][j]!=0) {
k=0;
break;
}
if(grid[n][j]==0 && n==x-1) {
remove();
}
}
}
if (x0>x) {
for (n=x0;n>=x+1 ;n-- ) {
if (grid[n][j]!=0) {
k=0;
break;
}
if(grid[n][j]==0 && n==x+1) {
remove();
}
}
}
}
}
for (i=0;i<8;i++ ) { //列
if (grid[i][y0]==0) {
if (x>i) {
for (j=x-1;j>=i ;j-- ) {
if (grid[j][y]!=0) {
k=0;
break;
}
else { k=1; }
}
if (k==1) {
rowPassOne();
}
}
if (x<i) {
for (j=x+1;j<=i;j++ ) {
if (grid[j][y]!=0) {
k=0;
break;
}
else { k=1; }
}
if (k==1) {
rowPassOne();
}
}
if (x==i) {
rowPassOne();
}
}
if (k==2){
if (y0==y) {
remove();
}
if (y0<y) {
for (n=y0;n<=y-1 ;n++ ) {
if (grid[i][n]!=0) {
k=0;
break;
}
if(grid[i][n]==0 && n==y-1) {
remove();
}
}
}
if (y0>y) {
for (n=y0;n>=y+1 ;n--) {
if (grid[i][n]!=0) {
k=0;
break;
}
if(grid[i][n]==0 && n==y+1) {
remove();
}
}
}
}
}
}
}
public void linePassOne(){
if (y0>j){ //第一按钮同行空按钮在左边
for (i=y0-1;i>=j ;i-- ){ //判断第一按钮同左侧空按钮之间有没按钮
if (grid[x0][i]!=0) {
k=0;
break;
}
else { k=2; } //K=2说明通过了第二次验证
}
}
if (y0<j){ //第一按钮同行空按钮在与第二按钮之间
for (i=y0+1;i<=j ;i++){
if (grid[x0][i]!=0) {
k=0;
break;
}
else{ k=2; }
}
}
}
public void rowPassOne(){
if (x0>i) {
for (j=x0-1;j>=i ;j-- ) {
if (grid[j][y0]!=0) {
k=0;
break;
}
else { k=2; }
}
}
if (x0<i) {
for (j=x0+1;j<=i ;j++ ) {
if (grid[j][y0]!=0) {
k=0;
break;
}
else { k=2; }
}
}
}
public void remove(){
firstButton.setVisible(false);
secondButton.setVisible(false);
fraction();
pressInformation=false;
k=0;
grid[x0][y0]=0;
grid[x][y]=0;
}
public void actionPerformed(ActionEvent e) {
if(e.getSource()==newlyButton){
int grid[][] = new int[8][7];
this.grid = grid;
randomBuild();
mainFrame.setVisible(false);
pressInformation=false;
init();
}
if(e.getSource()==exitButton)
System.exit(0);
if(e.getSource()==resetButton)
reload();
for(int cols = 0;cols < 6;cols++){
for(int rows = 0;rows < 5;rows++ ){
if(e.getSource()==diamondsButton[cols][rows])
estimateEven(cols+1,rows+1,diamondsButton[cols][rows]);
}
}
}
public static void main(String[] args) {
lianliankan llk = new lianliankan();
llk.randomBuild();
llk.init();
}
}

//old 998 lines
//new 318 lines

⑶ java老鼠迷宫代码难吗

非常难。思路:
1、设老鼠的行进路线都是优先选择下->右->上->左。
2、设老鼠很聪明,走过的路线走撒泡尿,表示鼠大爷到此一游,我们可以把数组的值改为3,表示走过,但走不通。
3、这是一个int[8][8]的二位数组,那么开始位置下标是1,1,结束位置是6,6。行和列分别用、j表示。
4、实际路线我们可以设置2表示,我们可以使用递归,让老鼠不断测试路线。
5、最后打印数组,看老鼠的实际路线。

⑷ 求走迷宫问题的算法,要求用Java写的

public class Maze { private int[][] maze = null;
private int[] xx = { 1, 0, -1, 0 };
private int[] yy = { 0, 1, 0, -1 };
private Queue queue = null; public Maze(int[][] maze) {
this.maze = maze;
queue = new Queue(maze.length * maze.length);
} public void go() {
Point outPt = new Point(maze.length - 1, maze[0].length - 1);
Point curPt = new Point(0, 0);
Node curNode = new Node(curPt, null);
maze[curPt.x][curPt.y] = 2;
queue.entryQ(curNode); while (!queue.isEmpty()) {
curNode = queue.outQ();
for (int i = 0; i < xx.length; ++i) {
Point nextPt = new Point();
nextPt.x = (curNode.point).x + xx[i];
nextPt.y = (curNode.point).y + yy[i];
if (check(nextPt)) {
Node nextNode = new Node(nextPt, curNode);
queue.entryQ(nextNode);
maze[nextPt.x][nextPt.y] = 2;
if (nextPt.equals(outPt)) {
java.util.Stack<Node> stack = new java.util.Stack<Node>();
stack.push(nextNode);
while ((curNode = nextNode.previous) != null) {
nextNode = curNode;
stack.push(curNode);
}
System.out.println("A Path is:");
while (!stack.isEmpty()) {
curNode = stack.pop();
System.out.println(curNode.point);
}
return;
}
}
}
}
System.out.println("Non solution!");
} private boolean check(Point p) {
if (p.x < 0 || p.x >= maze.length || p.y < 0 || p.y >= maze[0].length) {
return false;
}
if (maze[p.x][p.y] != 0) {
return false;
}
return true;
} public static void main(String[] args) {
int[][] maze = {
{ 0, 0, 1, 0, 1, 0, 1, 0, 1, 0 },
{ 0, 0, 1, 1, 1, 0, 0, 0, 1, 0 },
{ 0, 1, 0, 0, 1, 0, 0, 0, 0, 1 },
{ 0, 0, 0, 0, 1, 0, 0, 0, 1, 1 },
{ 0, 0, 1, 0, 0, 0, 0, 0, 0, 1 },
{ 1, 0, 1, 0, 1, 0, 0, 1, 0, 0 },
{ 0, 1, 0, 0, 1, 0, 0, 1, 0, 1 },
{ 1, 0, 1, 0, 1, 1, 0, 1, 0, 0 }
};
new Maze(maze).go();
} private class Queue { Node[] array = null;
int size = 0;
int len = 0;
int head = 0;
int tail = 0; public Queue(int n) {
array = new Node[n + 1];
size = n + 1;
} public boolean entryQ(Node node) {
if (isFull()) {
return false;
}
tail = (tail + 1) % size;
array[tail] = node;
len++;
return true;
} public Node outQ() {
if (isEmpty()) {
return null;
}
head = (head + 1) % size;
len--;
return array[head];
} public boolean isEmpty() {
return (len == 0 || head == tail) ? true : false;
} public boolean isFull() {
return ((tail + 1) % size == head) ? true : false;
}
} private class Node { Point point = null;
Node previous = null; public Node() {
this(null,null);
} public Node(Point point, Node node) {
this.point = point;
this.previous = node;
}
} private class Point { int x = 0;
int y = 0; public Point() {
this(0, 0);
} public Point(int x, int y) {
this.x = x;
this.y = y;
} public boolean equals(Point p) {
return (x == p.x) && (y == p.y);
} @Override
public String toString() {
return "(" + x + "," + y + ")";
}
}
}

⑸ 求Java关于迷宫的算法(用栈实现)

packagecom.Albert.LabyringhStack;

publicclassPoint{
intx;
inty;
intdirection;//direction指向此点附近的一个点应该有四个编号为1234
publicintgetX(){
returnx;
}
publicvoidsetX(intx){
this.x=x;
}
publicintgetY(){
returny;
}
publicvoidsetY(inty){
this.y=y;
}

publicintgetDirection(){
returndirection;
}
publicvoidsetDirection(intdirection){
this.direction=direction;
}
publicvoidaddDirection(){
this.direction++;

}
publicPoint(){
}
publicPoint(intx,inty){
super();
this.x=x;
this.y=y;
this.direction=1;
}
publicPoint(intx,inty,intdirection){
super();
this.x=x;
this.y=y;
this.direction=direction;
}

}
packagecom.Albert.LabyringhStack;

importjava.util.*;

publicclassLabyringhStack{

publicPointS;
publicPointF;
char[][]mazemap;
Stack<Point>path;

publicLabyringhStack(){
}
publicLabyringhStack(char[][]ma){//初始化存入数组
this.mazemap=newchar[ma.length][ma[0].length];
for(inti=0;i<ma.length;i++){
for(intj=0;j<ma[0].length;j++){//mazemap[0]必须有元素不可为空
this.mazemap[i][j]=ma[i][j];
}
}
S=returnPlace('S');
F=returnPlace('F');
}
publicPointreturnPlace(chars){//返回数组中字符的位置
Pointpoint=newPoint();
for(inti=0;i<this.mazemap.length;i++){
for(intj=0;j<this.mazemap[0].length;j++){//mazemap[0]必须有元素不可为空
if(this.mazemap[i][j]==s)
{point.setX(i);
point.setY(j);
point.setDirection(1);
}
}
}
returnpoint;
}
publiccharreturnChar(Pointpoint){
if(point.getX()>=0&&point.getY()>=0)
returnthis.mazemap[point.getX()][point.getY()];
else
return'#';
}
publicvoidreplacePlace(Pointpoint,chars){//更改特定位置处的字符
mazemap[point.getX()][point.getY()]=s;
}
publicvoidprintPath(){
Stack<Point>tempPath=newStack<Point>();
while(!path.empty()){//对栈进行反序
tempPath.push(path.pop());
}
while(!tempPath.empty()){
System.out.print("("+tempPath.peek().getX()+","+tempPath.pop().getY()+")");
}
}
publicbooleangetPath(){//取得路径的算法如果有路径就返回真
path=newStack<Point>();
S.setDirection(1);
path.push(S);
replacePlace(S,'X');
while(!path.empty()){
PointnowPoint=path.peek();//取得当前位置
if(nowPoint.getX()==F.getX()&&nowPoint.getY()==F.getY()){
//printPath();
returntrue;
}
Pointtemp=newPoint();//存放下一个可走的位置
intfind=0;//标志是否可向下走
while(nowPoint.getDirection()<5&&find==0){
switch(nowPoint.getDirection()){
case1:temp=newPoint(nowPoint.getX(),nowPoint.getY()-1,1);break;//取得当前位置左边的位置
case2:temp=newPoint(nowPoint.getX()+1,nowPoint.getY(),1);break;//取得当前位置下边的位置
case3:temp=newPoint(nowPoint.getX(),nowPoint.getY()+1,1);break;//取得当前位置右边的位置
case4:temp=newPoint(nowPoint.getX()-1,nowPoint.getY(),1);break;//取得当前位置上边的位置
}
nowPoint.addDirection();//指向下一个需要验证的点
if(returnChar(temp)=='O'||returnChar(temp)=='F')find=1;//如果能向下走则置为1
}
if(find==1){//如果可走就进栈
replacePlace(temp,'X');//设置成X防止回走
// printArr();
path.push(temp);
}else{//如果不可走就退栈
replacePlace(nowPoint,'O');
path.pop();
}
}
returnfalse;
}
publicvoidprintArr(){
for(inti=0;i<mazemap.length;i++){
for(intj=0;j<mazemap[0].length;j++){//mazemap[0]必须有元素不可为空
System.out.print(mazemap[i][j]);
}
System.out.println();
}
System.out.println();
}

}
packagecom.Albert.LabyringhStack;

publicclassMain{

/**
*@paramargs
*/
publicstaticvoidmain(String[]args){
//TODOAuto-generatedmethodstub
char[][]mazemap={
{'M','M','M','M','M','M','M','M'},
{'M','S','O','O','M','M','M','M'},
{'M','M','M','O','M','M','M','M'},
{'M','M','O','O','O','O','M','M'},
{'M','M','M','M','M','F','M','M'},
{'M','M','M','M','M','M','M','M'},
{'M','M','M','M','M','M','M','M'}
};
LabyringhStacksolution=newLabyringhStack(mazemap);
if(solution.getPath()){
System.out.print("迷宫路径如下:");
solution.printPath();
}
else{
System.out.println("没有可走的路");
}
}
}

热点内容
滑板鞋脚本视频 发布:2025-02-02 09:48:54 浏览:433
群晖怎么玩安卓模拟器 发布:2025-02-02 09:45:23 浏览:557
三星安卓12彩蛋怎么玩 发布:2025-02-02 09:44:39 浏览:744
电脑显示连接服务器错误 发布:2025-02-02 09:24:10 浏览:537
瑞芯微开发板编译 发布:2025-02-02 09:22:54 浏览:147
linux虚拟机用gcc编译时显示错误 发布:2025-02-02 09:14:01 浏览:240
java驼峰 发布:2025-02-02 09:13:26 浏览:652
魔兽脚本怎么用 发布:2025-02-02 09:10:28 浏览:538
linuxadobe 发布:2025-02-02 09:09:43 浏览:212
sql2000数据库连接 发布:2025-02-02 09:09:43 浏览:726