/**
* 该类是“World-of-Zuul”应用程序的主类。
* 《World of Zuul》是一款简单的文本冒险游戏。用户可以在一些房间组成的迷宫中探险。
* 你们可以通过扩展该游戏的功能使它更有趣!.
*
* 如果想开始执行这个游戏,用户需要创建Game类的一个实例并调用“play”方法。
*
* Game类的实例将创建并初始化所有其他类:它创建所有房间,并将它们连接成迷宫;它创建解析器
* 接收用户输入,并将用户输入转换成命令后开始运行游戏。
*
* @author Michael Kölling and David J. Barnes
* @version 1.0
*/
package cn.edu.whut.sept.zuul;
import java.util.*;
public class Game
{
private Parser parser;
Stack stack=new Stack(); //储存玩家的移动过程
Stack roomStack=new Stack(); //储存玩家被传送前的位置
private Room currentRoom; //玩家当前所处位置
ArrayList<Room> rooms = new ArrayList<>(); //存储所有房间对象
Room outside = new Room("outside the main entrance of the university");
/**
* 创建游戏并初始化内部数据和解析器.
*/
public Game()
{
createRooms();
parser = new Parser();
}
/**
* 创建所有房间对象并连接其出口用以构建迷宫.
*/
private void createRooms()
{
Room theater, pub, lab;
TransporterRoom office;
// create the rooms
theater = new Room("in a lecture theater");
pub = new Room("in the campus pub");
lab = new Room("in a computing lab");
office = new TransporterRoom("in the computing admin office");
// initialise room exits
outside.setExit("east", theater);
outside.setExit("south", lab);
outside.setExit("west", pub);
theater.setExit("west", outside);
pub.setExit("east", outside);
lab.setExit("north", outside);
lab.setExit("east", office);
office.setExit("west", lab);
currentRoom = outside; // 从outside开始游戏
rooms.add(outside);
rooms.add(theater);
rooms.add(pub);
rooms.add(lab);
}
/**
* 游戏主控循环,直到用户输入退出命令后结束整个程序.
*/
public void play()
{
printWelcome();
// Enter the main command loop. Here we repeatedly read commands and
// execute them until the game is over.
boolean finished = false;
while (! finished) {
Command command = parser.getCommand();
finished = processCommand(command);
}
System.out.println("Thank you for playing. Good bye.");
}
/**
* 向用户输出欢迎信息.
*/
private void printWelcome()
{
System.out.println();
System.out.println("Welcome to the World of Zuul!");
System.out.println("World of Zuul is a new, incredibly boring adventure game.");
System.out.println("Type 'help' if you need help.");
System.out.println();
System.out.println(currentRoom.getLongDescription());
}
/**
* 执行用户输入的游戏指令.
* @param command 待处理的游戏指令,由解析器从用户输入内容生成.
* @return 如果执行的是游戏结束指令,则返回true,否则返回false.
*/
private boolean processCommand(Command command)
{
boolean wantToQuit = false;
if(command.isUnknown()) {
System.out.println("I don't know what you mean...");
return false;
}
String commandWord = command.getCommandWord();
String words[] = parser.getCommandWords();
int index = 0;
for(; index<words.length; index++)
if (commandWord.equals(words[index]))
break;
switch(index) {
case 0: goRoom(command); break;
case 1: wantToQuit = quit(command); break;
case 2: printHelp(); break;
case 3: backRoom(); break;
}
/*
if (commandWord.equals("help")) {
printHelp();
}
else if (commandWord.equals("go")) {
goRoom(command);
}
else if (commandWord.equals("quit")) {
wantToQuit = quit(command);
}
*/
// else command not recognised.
return wantToQuit;
}
// implementations of user commands:
/**
* 执行help指令,在终端打印游戏帮助信息.
* 此处会输出游戏中用户可以输入的命令列表
*/
private void printHelp()
{
System.out.println("You are lost. You are alone. You wander");
System.out.println("around at the university.");
System.out.println();
System.out.println("Your command words are:");
parser.showCommands();
}
/**
*执行go指令,向房间的指定方向出口移动,如果该出口连接了另一个房间,则会进入该房间,
* 否则打印输出错误提示信息.
* 保存玩家的移动路径
* 若进入有传送门的房间则传送到任意房间
*/
private void goRoom(Command command)
{
if(!command.hasSecondWord()) {
// if there is no second word, we don't know where to go...
System.out.println("Go where?");
return;
}
String direction = command.getSecondWord();
stack.push(direction);
// Try to leave current room.
Room nextRoom = currentRoom.getExit(direction, rooms);
if (nextRoom == null) {
System.out.println("There is no door!");
}
else {
if(currentRoom.getExit(direction, rooms) instanceof TransporterRoom){
roomStack.push(currentRoom);
stack.push("tran");
nextRoom = currentRoom.getExit(direction, rooms);
currentRoom = nextRoom.getExit(direction, rooms);
System.out.println(currentRoom.getLongDescription());
}
else{
currentRoom = nextRoom;
System.out.println(currentRoom.getLongDescription());
}
}
}
/* 执行back指令,若栈为空则说明玩家已经回到起点
* 如果弹栈元素为方位指令,则玩家反向移动以退回上一个房间
* 如果弹栈元素为tran,则将房间栈弹出一个元素,即玩家被传送之前的位置
*/
private void backRoom()
{
if(stack.empty())
System.out.println("You have been in the staring point.\nExits: east south west");
else{
String direction = (String)stack.pop();
if(direction.equals("west"))
direction = "east";
if(direction.equals("east"))
direction = "west";
if(direction.equals("south"))
direction = "north";
if(direction.equals("north"))
direction = "south";
if(direction.equals("tran")) {
currentRoom = (Room)roomStack.pop();
stack.pop();
}
else {
Room nextRoom = currentRoom.getExit(direction, rooms);
currentRoom = nextRoom;
}
if (currentRoom == null)
currentRoom = outside;
System.out.println(currentRoom.getLongDescription());
}
}
/**
* 执行Quit指令,用户退出游戏。如果用户在命令中输入了其他参数,则进一步询问用户是否真的退出.
* @return 如果游戏需要退出则返回true,否则返回false.
*/
private boolean quit(Command command)
{
if(command.hasSe
谛凌
- 粉丝: 3w+
- 资源: 89
最新资源
- SimpleComTools开发的TCP Test Tool和UDP Test Tool
- PLC小车自动控制往返系统设计与仿真 《可编程控制器原理与应用》综合设计性实验 s7-1200系列,博途V15 基于博途平台小车自动往返控制系统的设计与仿真,包括硬件组态、变量定义、PLC程序设计
- 企业管理-OKR计划表
- Jmeter-01、08
- 改进二进制粒子群算法配电网重构 可以动态生成配电网重构过程,目标函数为功率损耗,算例为IEEE33节点系统 程序简洁明了,注释详细
- 多智能体、一致性、时滞 含通信时滞和输入时滞的多智能体一致性仿真 简单的多智能体一致性性仿真图,包含状态轨迹图和控制输入图 适用于初学者
- 齿轮、行星齿轮、端面齿轮、斜齿轮、非圆齿轮、圆弧齿轮……啮合理论、啮合原理、齿面求解、传动特性、接触分析tca、传动误差等技术matlab程序实现 参照李特文《齿轮几何学与啮合理论》
- 直流无刷电机模型+三闭环-simulink
- Java毕设项目:基于spring+mybatis+maven+mysql实现的养老院老人健康监护平台【含源码+数据库+开题报告+任务书+毕业论文】
- 51单片机开发的光照强度检测程序源码,用滑动变阻器分压代替采集电压信号光敏电阻, 包括程序源码和原理图和protues仿真, 程序源码注释详细,非常适合单片机开发人员,
- 1.Python简介.ipynb
- 基于simulink的永磁同步电机DTC控制系统仿真 基于模糊控制的pmsm的DTC控制系统仿真
- Java毕设项目:基于spring+mybatis+maven+mysql实现的农家乐系统分前后台【含源码+数据库+毕业论文】
- 双向LLC比较新的拓扑结构,双变压器,CDT-LC双向直流变器 只有开环仿真,可实现软开关 送对应参考文县
- Java毕设项目:基于spring+mybatis+maven+mysql实现的药品管理系统【含源码+数据库+毕业论文】
- 光伏发电并网逆变simulink matlab仿真 两级三相 单相系统 前级采用boost升压斩波电路 mppt最大功率点跟踪采用扰动观察法 可接单相或者三相并网逆变
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈