import java.util.*;
/**
* This class is the main class of the "World of Zuul" application.
* "World of Zuul" is a very simple, text based adventure game. Users
* can walk around some scenery. That's all. It should really be extended
* to make it more interesting!
*
* To play this game, create an instance of this class and call the "play"
* method.
*
* This main class creates and initialises all the others: it creates all
* rooms, creates the parser and starts the game. It also evaluates and
* executes the commands that the parser returns.
*
* @author Michael Kölling and David J. Barnes
* @version 2016.02.29
*/
public class Game
{
private Parser parser;
private Room currentRoom;
private Food bag = null;
/**
* Create the game and initialise its internal map.
*/
public Game()
{
createRooms();
parser = new Parser();
}
/**
* Create all the rooms and link their exits together.
*/
private void createRooms()
{
Room outside, emergency, Dressing, operating, duty, Sterilizing, pharmacy, storage;
// create the rooms
outside = new Room("神庙出口");
emergency = new Room("第一个十字路口", new Food("宝箱一",10));
Dressing = new Room("第二个十字路口",new Food("宝箱二",-10));
operating = new Room("第三个十字路口",new Food("宝箱三",10));
duty = new Room("第七个十字路口",new Food("宝箱四",20));
Sterilizing = new Room("第五个十字路口", new Food("宝箱五",-10));
pharmacy = new Room("第六个十字路口", new Food("宝箱六",10));
storage = new Room("第四个十字路口", new Food("宝箱七",-10));
// initialise room exits
outside.setExit("north", Sterilizing);
outside.setExit("east", emergency);
outside.setExit("west", Dressing);
Dressing.setExit("east", outside);
emergency.setExit("west", outside);
emergency.setExit("north", operating);
emergency.setExit("south", pharmacy);
operating.setExit("east", storage);
operating.setExit("west", Sterilizing);
operating.setExit("north", duty);
operating.setExit("south", emergency);
storage.setExit("west", operating);
Sterilizing.setExit("east", operating);
Sterilizing.setExit("south", outside);
pharmacy.setExit("north", emergency);
pharmacy.setExit("west", duty);
duty.setExit("你脱离了追击,传播了信息,感谢您做出的贡献。请输入‘quit’结束",duty);
currentRoom = outside; // start game outside
}
/**
* Main play routine. Loops until end of play.
*/
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("你成功的逃离了追击!!!");
}
/**
* Print out the opening message for the player.
*/
private void printWelcome()
{
System.out.println();
System.out.println("这里是克苏鲁大王的神庙");
System.out.println("你好不容易逃离了神庙,现在你需要逃离克苏鲁手下的追击!!");
System.out.println("你需要将神庙的消息告诉世人,拯救他们");
System.out.println("现在......逃亡开始!!!");
System.out.println("如果你奔溃了,,输入“help”");
System.out.println();
System.out.println("你在 " + currentRoom.getDescription());
currentRoom.printExits();
}
/**
* Given a command, process (that is: execute) the command.
* @param command The command to be processed.
* @return true If the command ends the game, false otherwise.
*/
private boolean processCommand(Command command)
{
boolean wantToQuit = false;
if(command.isUnknown()) {
System.out.println("我不知道怎么走......");
return false;
}
Word commandWord = command.getCommandWord();
switch(commandWord){
case HELP:
printHelp();
break;
case GO:
goRoom(command);
break;
case QUIT:
wantToQuit = quit(command);
break;
case LOOK:
Food food = currentRoom.getFood();
if(food != null){
System.out.println("房间里有" + food.getName());
}
else{
System.out.println("这个房间里什么都没有。");
}
break;
case PICK:
Food food1 = currentRoom.getFood();
if(bag != null){
System.out.println("你已经很饱了");
}
else{
System.out.println("你捡起了"+ food1.getName());
bag = food1;
}
break;
case EAT:
if(bag != null){
System.out.println("你喝掉了" + bag.getName());
bag=null;
}
else{
System.out.println("你什么都没有喝。");
}
break;
case CHECK:
if(bag != null){
System.out.println("你喝掉了" + bag.getName());
}
else{
System.out.println("你很渴。");
}
break;
}
// if (commandWord.equals("help")) {
// printHelp();
// }
// else if (commandWord.equals("go")) {
// goRoom(command);
// }
// else if (commandWord.equals("quit")) {
// wantToQuit = quit(command);
// }
// else if (commandWord.equals("look")) {
// }
return wantToQuit;
}
// implementations of user commands:
/**
* Print out some help information.
* Here we print some stupid, cryptic message and a list of the
* command words.
*/
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:");
// System.out.println(" go quit help");
System.out.println("你找不到路了。 你需要");
System.out.println("回到最初的地点(神庙出口)");
System.out.println("你的动作是:");
Word[] words = Word.values();
for(int i=0;i
System.out.print(words[i].getCommandWord()+" ");
System.out.println();
}
}
/**
* Try to go in one direction. If there is an exit, enter
* the new room, otherwise print an error message.
*/
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?");
System.out.println("你要去哪儿?");
return;
}
String direction = command.getSecondWord();
// Try to leave current room.
Room nextRoom = currentRoom.goNext(direction);
if(nextRoom == null){
//System.out.print("There is no door!");
System.out.println("这里出不去!");
}
else {
currentRoom = nextRoom;
System.out.println("你在 " + currentRoom.getDescription());
currentRoom.printExits();
}
}
/**
* "Quit" was entered. Check the rest of the command to see
* whether we really quit the game.
* @return true, if this command quits the game, false otherwise.
*/
private boolean quit(Command command)
{
if(command.hasSecondWord()) {
System.out.println("Quit what?");
return false;
}
else {
return true; // signal that we want to quit
}
}
}
一键复制
编辑
Web IDE
原始数据
按行查看
历史