chenchang12

导航

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

}

}

posted on 2019-03-22 09:43  chenchang12  阅读(646)  评论(0编辑  收藏  举报