2009-2010-2 课程实验报告 高级程序设计(JAV A) 专业班级 姓 名 学 号 高级程序设计(JAVA)实验报告 实验名称:迷宫实验 1 实验要求 I.用面向对象的思想编写扩展的“猜数字”游戏。 II.走迷宫游戏: i 设置一个迷宫(如:大小10×10、16×16 等) ii 迷宫固定一个入口,一个出口 iii 设计算法来找出走出迷宫的路线 iv 如果迷宫是死胡同,则提示并结束游戏 2 设计及实现 2 .1 设计思路 类图: Place Int place[][]; Int n; Place() makeplace() printplace() site Int sitex; Int sitey; 程序流程图: FindWay site site; Point point; boolean visted[][]; Place pl=new Place(); FindWay() findTheWay(int[][] maze) Point goup() godown() goright() goleft() 高级程序设计(JAVA)实验报告 If input 3 if input 2 YES NO 2 or 3 Have way? Make the map Start End Find way Input the number Output“设置迷宫大小” Input the style of maze Output “输入整数 2 显示迷宫答案; 输入整数 3 结束游戏 请选择操作序号:” output the way of the maze Output“没有出路” Output “用户终止了程序” 高级程序设计(JAVA)实验报告 2 .2 核心算法 //向上走 site goup(site s) { s.sitex=s.sitex; s.sitey=s.sitey-1; return s; } //向下走 site godown(site s) { s.sitex=s.sitex; s.sitey=s.sitey+1; return s; } //向右走 site goright(site s) { s.sitex=s.sitex+1; s.sitey=s.sitey; return s; } //向左走 site goleft(site s) { s.sitex=s.sitex-1; s.sitey=s.sitey; return s; } //寻找迷宫路径 boolean findTheWay(int[][] maze) { Stack seekStack =new Stack(); //路径储存堆栈 while(site.sitey!=pl.n-1||site.sitex!=pl.n-2&&0<=site.sitey&&site.sitey