java 随机迷宫游戏Word格式文档下载.docx
《java 随机迷宫游戏Word格式文档下载.docx》由会员分享,可在线阅读,更多相关《java 随机迷宫游戏Word格式文档下载.docx(15页珍藏版)》请在冰豆网上搜索。
productMaze()
voidprintMaze()
2.2核心算法
publicLocationgoUp()//向上走
{
Locationnextlocation;
nextlocation=newLocation();
nextlocation.locationi=location.locationi-1;
nextlocation.locationj=location.locationj;
returnnextlocation;
}
//向下走,向左走,向右走代码省略
publicbooleanfindTheWay(int[][]maze)
{
StackseekStack=newStack();
//探查堆栈
while(location.locationi!
=9||location.locationj!
=8
&
&
0<
=location.locationi&
location.locationi<
=9
=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();
//移动到右侧下一位置
elseif(maze[location.locationi+1][location.locationj]==0&
visted[location.locationi+1][location.locationj]==false)
maze[location.locationi][location.locationj]=3;
//用↓表示该处向下可通
location=goDown();
//移动到下一位置
elseif(maze[location.locationi][location.locationj-1]==0&
visted[location.locationi][location.locationj-1]==false)//表示改点可通且未被访问
maze[location.locationi][location.locationj]=4;
//用←表示向左可通
location=goLeft();
//向左移动到下一位置
elseif(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//该位置不通
Locationdie;
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)
returntrue;
else
System.out.println("
没有出路,游戏结束!
"
);
returnfalse;
3心得及思考
如果说第一个猜数游戏是初试面向对象思想编程的话,这个走迷宫小游戏就是对面向对象编程的进一步体会。
这个迷宫小游戏的编写过程真的是艰辛啊。
由于对游戏的理解不深刻,在编程之前不能把所有的类一起抽象出来,在编写的过程中逐渐深入了解了游戏。
也逐渐摸清楚了类的划分。
抽象出了一个记录走法器的坐标位置的Location类。
遇到的其他问题也不少,其中一个就是数组越界问题,解决起来挺让人头疼的,不过还是在不懈努力下解决了数组越界问题。
曾经遇到的另一个问题是,堆栈的应用问题,几经检测终于发现问题在于,一改全改。
修改了移动算法,在算法中生成一个location对象并将其返回付给当前location对象。
问题的解决。
附录:
走迷宫游戏代码:
//Maze类
publicclassMaze{
intmaze[][];
//声明迷宫数组矩阵
Maze()//构造函数
maze=newint[10][10];
//创建数组矩阵
for(inti=0;
i<
10;
i++)
for(intj=0;
j<
j++)
maze[i][j]=1;
publicvoidproductMaze()//生成迷宫数据
//暂且固定迷宫的入口与出口
maze[0][1]=maze[1][1]=0;
//迷宫入口
maze[9][8]=0;
//迷宫出口
//随机创建内部矩阵
for(inti=1;
9;
for(intk=1;
k<
k++)
intran;
ran=(int)(Math.random()*3)+1;
if(ran%3==0||ran%3==1)
maze[i][k]=0;
/*publicvoidproductMaze()//生成迷宫数据
maze=newint[4][4];
4;
maze[0][1]=0;
maze[1][1]=maze[1][2]=0;
maze[2][1]=maze[2][2]=maze[2][3]=0;
}*/
publicvoidprintMaze()//输出数组矩阵
for(inti=0;
for(intj=0;
{
if(maze[i][j]==1)
System.out.print("
■"
//System.out.print(maze[i][j]);
elseif(maze[i][j]==0)
□"
elseif(maze[i][j]==2)
→"
elseif(maze[i][j]==3)
↓"
elseif(maze[i][j]==4)
←"
elseif(maze[i][j]==5)
↑"
}
System.out.println();
//换行
}
//Location类
publicclassLocation{
protectedintlocationi;
protectedintlocationj;
Location()
locationi=0;
locationj=1;
//walker类
importjava.util.*;
publicclassWalker
{
Locationlocation=newLocation();
//Location对象作为成员变量
booleanvisted[][];
//访问标记数组
Walker()
visted=newboolean[10][10];
//初始化visted数组初值为false
visted[i][j]=false;
/**
**迷宫走法器的移动方法
**/
/**
**/
publicLocationgoUp()//向上走
publicLocationgoDown()//向下走
nextlocation.locationi=location.locationi+1;
publicLocationgoRight()//向右走
nextlocation.locationi=location.locationi;
nextlocation.locationj=location.locationj+1;
publicLocationgoLeft()//向左走
nextlocation.locationj=location.locationj-1;
//迷宫找路径方法
publicbooleanfindTheWay(int[][]maze)
//Game类(游戏主类)
publicclassGame{
publicstaticvoidmain(Stringargs[])
Mazep=null;
//迷宫对象
intoperation;
\t\t\t走迷宫游戏说明:
System.out.println();
\t\t\t输入整数1产生随机迷宫;
\t\t\t输入整数2显示迷宫答案;
\t\t\t输入整数3结束游戏。
while(true)
请选择操作序号:
java.util.Scannerinput=newjava.util.Scanner(System.in);
operation=input.nextInt();
if(operation==1)
//产生随机迷宫并显示
p=newMaze();
p.productMaze();
p.printMaze();
elseif(operation==2)
//显示走迷宫答案
Walkerwalker=newWalker();
if(p==null)
System.out.println("
迷宫还没生成,请先选择操作1生成迷宫!
operation=input.nextInt();
elseif(walker.findTheWay(p.maze))
p.maze[9][8]=3;
p.printMaze();
elseif(operation==3)
System.out.println("
用户终止了程序!
break;
以下为走迷宫游戏运行界面及一次运行实例:
走迷宫游戏说明:
输入整数1产生随机迷宫;
输入整数2显示迷宫答案;
输入整数3结束游戏。
1
■□■■■■■■■■
■□□□■□■□■■
■□■□■■□■■■
■■□■□□□■■■
■■■□□□□■□■
■□□□■□□■□■
■□□■■□□■□■
■□■□■□□□□■
■■□□■□□□■■
■■■■■■■■□■
2
■□■□■□□□■■
■□■□□■□□□■
■□□□□□□□■■
■□■■□□■□□■
■□□□□■□■□■
■□■□□□□□■■
■□■□■□□■□■
■□■□□□■□■■
■□□□■□□□□■
■□□■■□□□□■
■□□□□□■□□■
■■□□■□□■■■
■■□□□□□■□■
■□□□□□□□□■
■↓■■■■■■■■
■→↓□■□□□□■
■□↓■■□□□□■
■□→→→↓■□□■
■■□□■→↓■■■
■■□□■□↓□■■
■■□□□↓←■□■
■□□□■↓■□■■
■□□□□→→→↓■
■■■■■■■■↓■
3