首页 > 编程语言 > 详细

Java简单走迷宫

时间:2019-03-22 10:13:55      阅读:208      评论:0      收藏:0      [点我收藏+]

import java.math.;
import java.util.
;

import javax.swing.text.ChangedCharSetException;
class Point{
public int x;
public int y;
public int getx() {
return x;
}
public int gety() {
return y;
}
}

class Game{
Stack posiPoints = new Stack();
void runGame() {
//last x last y you must remember!
Point lastPoint = new Point();
lastPoint.x=0;
lastPoint.y=1;
System.out.println("Game Start!");
Point a = new Point();
a.x=0;a.y=1;posiPoints.push(a);

    a.x=1;posiPoints.push(a);
    
    while(true) {
        System.out.println(point.x+"    "+point.y);
        //printDoor();
        if(point.x==9&&point.y==8) break;
        Point tempoint =  new Point();
        tempoint.x=point.x;
        tempoint.y=point.y;
        
        if(door[tempoint.x+1][tempoint.y]==' '&&(tempoint.x+1!=lastPoint.x)&&tempoint.y!=lastPoint.y) {
            point.x++;
            posiPoints.push(point);
        }
        else if(door[tempoint.x][tempoint.y++]==' '&&tempoint.x!=lastPoint.x&&(tempoint.y+1!=lastPoint.y)) {
            point.y++;
            posiPoints.push(point);
        }
        else if(door[tempoint.x-1][tempoint.y]==' '&&(tempoint.x-1!=lastPoint.x)&&tempoint.y!=lastPoint.y) {
            point.x--;
            posiPoints.push(point);
        }
        else if(door[tempoint.x][tempoint.y--]==' '&&tempoint.x!=lastPoint.x&&(tempoint.y-1!=lastPoint.y)) {
            point.y--;
            posiPoints.push(point);
        }
        else {
        
        door[point.x][point.y]='#';
        posiPoints.pop();
        point = posiPoints.pop();
        posiPoints.push(point);
        
        }
        
        
        
    }
    
    
    
}
public Game() {
    // TODO Auto-generated constructor stub
    point.x=1;
    point.y=1;
}
Point point = new Point();
char[][] door = new char[10][10];
public void makeDoor() {
    
    for(int i=0;i<10;i++) {
        for(int j=0;j<10;j++) {
            door[i][j]='#';
        }
    }
    for(int i=1;i<9;i++) {
        for(int j=1;j<9;j++) {
            if(Math.random()>0.1) {
                door[i][j]=' ';
                
            }
            
            else door[i][j]='#';
        }
    }
    door[9][8]=' ';
    door[0][1]=' ';
    door[1][1]=' ';
}
public void printDoor() {
    for(int i=0;i<10;i++) {
        for(int j=0;j<10;j++) {
            System.out.print(door[i][j]);
        }
        System.out.println("");
    }
}

}
public class Maze {
public static void main(String[] args) {
Game game = new Game();
game.makeDoor();
System.out.println("The door is ");
game.printDoor();
System.out.println("The way is");
game.runGame();
System.out.println("Game over!");

}

}

Java简单走迷宫

原文:https://www.cnblogs.com/Archerme/p/10576350.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!