java 走迷宫_Java简单走迷宫

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{

StackposiPoints = 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!");

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值