java 随机迷宫游戏.docx

上传人:b****5 文档编号:7288863 上传时间:2023-01-22 格式:DOCX 页数:15 大小:18.45KB
下载 相关 举报
java 随机迷宫游戏.docx_第1页
第1页 / 共15页
java 随机迷宫游戏.docx_第2页
第2页 / 共15页
java 随机迷宫游戏.docx_第3页
第3页 / 共15页
java 随机迷宫游戏.docx_第4页
第4页 / 共15页
java 随机迷宫游戏.docx_第5页
第5页 / 共15页
点击查看更多>>
下载资源
资源描述

java 随机迷宫游戏.docx

《java 随机迷宫游戏.docx》由会员分享,可在线阅读,更多相关《java 随机迷宫游戏.docx(15页珍藏版)》请在冰豆网上搜索。

java 随机迷宫游戏.docx

java随机迷宫游戏

随机迷宫游戏(面向对象)

1.实验要求

•设置一个迷宫(如:

大小10×10、16×16等)

•迷宫固定一个入口,一个出口

•设计算法来找出走出迷宫的路线

•如果迷宫是死胡同,则提示并结束游戏

2设计及实现

2.1设计思想

Walker

Locationlocation;

booleanvisted[][];

Walker()

goUp()

goDown()

goRight()

goLeft()

findTheWay(int[][]maze)

类图:

Location

Intlocationi;

Intlocationj;

Maze

intmaze[][];

Maze()

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

&&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();//移动到右侧下一位置

}

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<10;j++)

maze[i][j]=1;

}

}

publicvoidproductMaze()//生成迷宫数据

{

//暂且固定迷宫的入口与出口

maze[0][1]=maze[1][1]=0;//迷宫入口

maze[9][8]=0;//迷宫出口

//随机创建内部矩阵

for(inti=1;i<9;i++)

{

for(intk=1;k<9;k++)

{

intran;

ran=(int)(Math.random()*3)+1;

if(ran%3==0||ran%3==1)

maze[i][k]=0;

}

}

}

/*publicvoidproductMaze()//生成迷宫数据

{

maze=newint[4][4];//创建数组矩阵

for(inti=0;i<4;i++)

{

for(intj=0;j<4;j++)

maze[i][j]=1;

}

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;i<10;i++)

{

for(intj=0;j<10;j++)

{

if(maze[i][j]==1)

System.out.print("■");

//System.out.print(maze[i][j]);

elseif(maze[i][j]==0)

System.out.print("□");

elseif(maze[i][j]==2)

System.out.print("→");

elseif(maze[i][j]==3)

System.out.print("↓");

elseif(maze[i][j]==4)

System.out.print("←");

elseif(maze[i][j]==5)

System.out.print("↑");

}

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

for(inti=0;i<10;i++)

{

for(intj=0;j<10;j++)

visted[i][j]=false;

}

}

/**

**迷宫走法器的移动方法

**/

/**

**迷宫走法器的移动方法

**/

publicLocationgoUp()//向上走

{

Locationnextlocation;

nextlocation=newLocation();

nextlocation.locationi=location.locationi-1;

nextlocation.locationj=location.locationj;

returnnextlocation;

}

publicLocationgoDown()//向下走

{

Locationnextlocation;

nextlocation=newLocation();

nextlocation.locationi=location.locationi+1;

nextlocation.locationj=location.locationj;

returnnextlocation;

}

publicLocationgoRight()//向右走

{

Locationnextlocation;

nextlocation=newLocation();

nextlocation.locationi=location.locationi;

nextlocation.locationj=location.locationj+1;

returnnextlocation;

}

publicLocationgoLeft()//向左走

{

Locationnextlocation;

nextlocation=newLocation();

nextlocation.locationi=location.locationi;

nextlocation.locationj=location.locationj-1;

returnnextlocation;

}

//迷宫找路径方法

publicbooleanfindTheWay(int[][]maze)

{

StackseekStack=newStack();//探查堆栈

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();//移动到右侧下一位置

}

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;

}

}

}

//Game类(游戏主类)

publicclassGame{

publicstaticvoidmain(Stringargs[])

{

Mazep=null;//迷宫对象

intoperation;

for(inti=0;i<4;i++)

{

System.out.println();

}

System.out.println("\t\t\t走迷宫游戏说明:

");

System.out.println();

System.out.println("\t\t\t输入整数1产生随机迷宫;");

System.out.println("\t\t\t输入整数2显示迷宫答案;");

System.out.println("\t\t\t输入整数3结束游戏。

");

while(true)

{

System.out.println("请选择操作序号:

");

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

没有出路,游戏结束!

请选择操作序号:

1

■□■■■■■■■■

■□■□■□□□■■

■□■□□■□□□■

■□□□□□□□■■

■□■■□□■□□■

■□□□□■□■□■

■□■□□□□□■■

■□■□■□□■□■

■□■□□□■□■■

■■■■■■■■□■

请选择操作序号:

2

没有出路,游戏结束!

请选择操作序号:

1

■□■■■■■■■■

■□□□■□□□□■

■□□■■□□□□■

■□□□□□■□□■

■■□□■□□■■■

■■□□■□□□■■

■■□□□□□■□■

■□□□■□■□■■

■□□□□□□□□■

■■■■■■■■□■

请选择操作序号:

2

■↓■■■■■■■■

■→↓□■□□□□■

■□↓■■□□□□■

■□→→→↓■□□■

■■□□■→↓■■■

■■□□■□↓□■■

■■□□□↓←■□■

■□□□■↓■□■■

■□□□□→→→↓■

■■■■■■■■↓■

请选择操作序号:

3

用户终止了程序!

 

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 农林牧渔 > 林学

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1