1 Star 0 Fork 122

xc/zuul

forked from 吕焱飞/zuul 
加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
Game.java 7.68 KB
一键复制 编辑 原始数据 按行查看 历史
xc 提交于 2020-06-21 09:16 . update Game.java.
/**
* 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 Player player;
/**
* Create the game and initialise its internal map.
*/
public Game()
{
createRooms();
parser = new Parser();
player =new Player();
}
/**
* Create all the rooms and link their exits together.
*/
private void createRooms()
{
Room outside, squirrl1,squirrl2, fox,find , wolf1, wolf2, home;
// create the rooms
outside = new Room("起点");
find =new Room("你发现了小松鼠也迷路了,打算带他回家");
squirrl1 = new Room("到土豪松鼠洞里,由于你找回他的儿子小松鼠,送你一颗金松果",new Thing("金松果+1",10));
squirrl2 = new Room("到土豪松鼠洞里,他们很伤心,好像发生了生命事情,你就匆匆走了");
fox = new Room("到狡猾小狐狸洞里,被小狐狸骗走一颗金松果",new Thing("金松果-1,幸福感下降-50",-50));
wolf1 = new Room("到穷酸大灰狼洞里,由于你拥有一颗金松果,成功换回自己的性命",new Thing("幸福感上升+",10));
wolf2 = new Room("到穷酸大灰狼洞里,由于你没有任何东西给他,他把你关起来了",new Thing("幸福感下降-50",-50));
home = new Room("恭喜你!到家了");
// initialise room exits
outside.setExit("east",squirrl2);
outside.setExit("south",wolf2);
outside.setExit("west",find);
squirrl2.setExit("east",wolf2);
squirrl2.setExit("south",null);
squirrl2.setExit("north",home);
squirrl1.setExit("east",wolf1);
squirrl1.setExit("north",home);
squirrl1.setExit("west",fox);
fox.setExit("east",wolf2);
fox.setExit("south",home);
fox.setExit("west",null);
wolf1.setExit("east",home);
find.setExit("east",wolf2);
find.setExit("south",null);
find.setExit("west",squirrl1);
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.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 PICK:
pickThing();
case EAT:
eatThing(command);
break;
case CHECK:
player.checkBag();
break;
case LOOK:
lookRoom();
break;
}
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("你不知道该如何操作了吗?");
System.out.println();
System.out.println("让我们来告诉你");
System.out.println();
System.out.println("你可以使用的指令有:");
System.out.println();
System.out.println("go :后面加要去的方向");
System.out.println("quit:退出游戏");
System.out.println("help:寻求帮助");
System.out.println("look:寻找需要的东西");
System.out.println("check:查看背包");
System.out.println("pick:获得物品");
System.out.println("eat:使用物品");
}
/**
* 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("去哪里?");
return;
}
String direction = command.getSecondWord();
// Try to leave current room.
Room nextRoom = currentRoom.goNext(direction);
if (nextRoom == null) {
System.out.println("你回到原点呢!");
}
else {
currentRoom = nextRoom;
System.out.println("你正在 " + currentRoom.getDescription());
currentRoom.printExits();
}
}
private void lookRoom()
{
if(currentRoom.getThing()==null)
System.out.println("没有什么变化");
else System.out.println("这里有 "+currentRoom.getThing().getName());
}
private void pickThing()
{
player.pick(currentRoom.getThing());
currentRoom.remove();
}
private void eatThing(Command command)
{
if(!command.hasSecondWord()) {
// if there is no second word, we don't know where to go...
System.out.println("你是否准备用它?");
return;
}
player.eat(command.getSecondWord());
}
/**
* "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("退出?");
return false;
}
else {
return true; // signal that we want to quit
}
}
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Java
1
https://gitee.com/bwcx020/zuul.git
git@gitee.com:bwcx020/zuul.git
bwcx020
zuul
zuul
master

搜索帮助