import java.util.*;
//Maze 类
class Maze {
int maze [][]; //声明迷宫数组矩阵
Maze () //构造函数
{
maze=new int[10][10]; //创建数组矩阵
for (int i=0;i<10;i++)
{
for (int j=0;j<10;j++)
maze[i][j]=1;
}
}
public void productMaze() //生成迷宫数据
{
//暂且固定迷宫的入口与出口
maze[0][1]=maze[1][1]=0; //迷宫入口
maze[9][8]=0; //迷宫出口
//随机创建内部矩阵
for (int i=1;i<9;i++)
{
for (int k=1;k<9;k++)
{
int ran ;
ran=(int)(Math.random()*3)+1;
if(ran%3==0||ran%3==1)
maze[i][k]=0;
}
}
}
public void printMaze() //输出数组矩阵
{
for(int i=0;i<10;i++)
{
for(int j=0;j<10;j++)
{
if (maze[i][j]==1)
System.out.print("■");
else if(maze[i][j]==0)
System.out.print("□");
else if(maze[i][j]==2)
System.out.print("→");
else if (maze[i][j]==3)
System.out.print("↓");
else if (maze[i][j]==4)
System.out.print("←");
else if (maze[i][j]==5)
System.out.print("↑");
}
System.out.println(); //换行
}
}
}
//Location 类
class Location {
protected int locationi;
protected int locationj;
Location ()
{
locationi=0;
locationj=1;
}
}
//walker 类
class Walker
{
Location location=new Location(); //Location对象作为成员变量
boolean visted[][]; //访问标记数组
Walker()
{
visted=new boolean [10][10]; //初始化visted数组 初值为false
for (int i=0;i<10;i++)
{
for (int j=0;j<10;j++)
visted[i][j]=false;
}
}
/**
* * 迷宫走法器的移动方法
**/
/**
* * 迷宫走法器的移动方法
**/
public Location goUp() //向上走
{
Location nextlocation;
nextlocation =new Location();
nextlocation.locationi=location.locationi-1;
nextlocation.locationj=location.locationj;
return nextlocation;
}
public Location goDown() //向下走
{
Location nextlocation;
nextlocation =new Location();
nextlocation.locationi=location.locationi+1;
nextlocation.locationj=location.locationj;
return nextlocation;
}
public Location goRight() //向右走
{
Location nextlocation;
nextlocation =new Location();
nextlocation.locationi=location.locationi;
nextlocation.locationj=location.locationj+1;
return nextlocation;
}
public Location goLeft() //向左走
{
Location nextlocation;
nextlocation =new Location();
nextlocation.locationi=location.locationi;
nextlocation.locationj=location.locationj-1;
return nextlocation;
}
//迷宫找路径方法
public boolean findTheWay(int[][] maze)
{
Stack seekStack =new Stack(); //探查堆栈
while(location.locationi!=9||location.locationj!=8
&&0<=location.locationi&&location.locationi<=9
&&0<=location.locationj&&location.locationj<=9) //当还没到达出口*/
{
if(visted[location.locationi][location.locationj]==false)
{
seekStack.push(location); //位置入栈
visted[location.locationi][location.locationj]=true; //该位置已被访问
}
if(maze[location.locationi][location.locationj+1]== 0&&visted[location.locationi][location.locationj+1]==false) //该位置向右可通
{
maze[location.locationi][location.locationj]=2; //用→表示该位置向右可通
location=goRight();//移动到右侧下一位置
}
else if(maze[location.locationi+1][location.locationj]== 0&&visted[location.locationi+1][location.locationj]==false)
{
maze[location.locationi][location.locationj]=3; //用↓表示该处向下可通
location=goDown(); //移动到下一位置
}
else if(maze[location.locationi][location.locationj-1]== 0&&visted[location.locationi][location.locationj-1]==false) //表示改点可通且未被访问
{
maze[location.locationi][location.locationj]=4; //用 ←表示向左可通
location=goLeft(); //向左移动到下一位置
}
else if (location.locationi!=0&&maze[location.locationi-1][location.locationj]== 0&&visted[location.locationi-1][location.locationj]==false) //向上可通并且未被访问
{
maze[location.locationi][location.locationj]=5; //用↑表示向上可通
location=goUp(); //移动到上一个相邻的位置
}
else //该位置不通
{
Location die;
die=(Location)seekStack.pop(); //将该位置从堆栈中退出
maze[die.locationi][die.locationj]=0;
if (seekStack.empty()==true)
{
break;
}
location=(Location) seekStack.peek();
}
}
if (location.locationi==9&&location.locationj==8)
{
return true;
}
else
{
System.out.println("没有出路,游戏结束!");
return false;
}
}
}
//Game 类(游戏主类)
public class Game {
public static void main(String args[])
{
Maze p=null;//迷宫对象
int operation;
for (int i=0;i<4;i++)
{
System.out.println();
}
System.out.println("走迷宫游戏说明:");
System.out.println();
System.out.println("输入整数 1 产生随机迷宫;");
System.out.println("输入整数 2 显示迷宫答案;");
System.out.println("输入整数 3 结束游戏 。");
while (true)
{
System.out.println("\n请选择操作序号:");
java.util.Scanner input = new java.util.Scanner(System.in);
operation=input.nextInt();
if (operation==1)
{
// 产生随机迷宫并显示
p=new Maze();//迷宫对象
p.productMaze();
p.printMaze();
}
else if (operation==2)
{
//显示走迷宫答案
Walker walker=new Walker();
if (p==null)
{
System.out.println("迷宫还没生成,请先选择操作1生成迷宫!");
operation=input.nextInt();
}
else if(walker.findTheWay(p.maze))
{
p.maze[9][8]=3;
p.printMaze();
}
}
else if (operation==3)
{
System.out.println("用户终止了程序!");
break;
}
}
}
}