1 Star 0 Fork 123

邱钰煜/zuul

forked from 吕焱飞/zuul 
加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
Game.java 8.91 KB
一键复制 编辑 原始数据 按行查看 历史
root 提交于 2021-06-23 15:01 . add 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();
createRooms();
}
/**
* Create all the rooms and link their exits together.
*/
private void createRooms()
{
Room outside, apple, hp, chalu, xiaohu, muwu, chukou, kakashi, muz, renwudian;
// create the rooms
outside = new Room("森林入口,要选择去哪边呢?");
chukou = new Room("回到森林的入口了,再挑一个别的方向吧");
apple = new Room("到了一位老伯的苹果摊前。真是奇怪呢,怎么会把苹果摊摆在森林里。不过老伯的苹果看起来很好吃呢,要来一个吗?");
apple.putFood(new Food("苹果",20));
hp = new Room("看到一片湖泊,波光粼粼。但是没有小虎的踪迹,接下来去哪个呢。");
chalu = new Room("遇到了岔路口,要往哪边走呢?");
muz = new Room("一大片木桩前。遇到了小樱。啊,真是讨厌,这些滥砍滥伐的人。小樱一边轻声抱怨着,一边催你赶紧去规定的地点。");
muwu = new Room("到了一个小木屋前,啊,佐助也在呢。友好木屋主人拿出了一罐糖给你们。");
muwu.putFood(new Food("木屋主人的糖果",30));
xiaohu = new Room("终于到了指定的地方啦,啊!是小虎。嘘,慢慢靠近。");
kakashi = new Room("啊哦,你走到卡卡西老师的地方了。哎呀呀,快走吧,不然又要被大家笑话了。似乎有什么东西窜过去了!");
xiaohu.putFood(new Food("小虎",-10));
renwudian = new Room("已经完成了任务,恭喜!跟着卡卡西老师,回去任务点提交任务领取报酬吧。");
outside.setExit("west",apple);
outside.setExit("south",chalu);
outside.setExit("east",hp);
apple.setExit("east",chukou);
apple.setExit("south",muz);
chukou.setExit("west",apple);
chukou.setExit("east",hp);
chukou.setExit("south",chalu);
chalu.setExit("up",chukou);
chalu.setExit("left",muz);
chalu.setExit("right",muwu);
chalu.setExit("up",chukou);
muz.setExit("right",muwu);
muz.setExit("down",xiaohu);
muwu.setExit("left",muz);
muwu.setExit("down",xiaohu);
muwu.setExit("south",kakashi);
kakashi.setExit("west",xiaohu);
xiaohu.setExit("up",renwudian);
hp.setExit("west",chukou);
hp.setExit("down",kakashi);
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("今天,你将要和佐助、小樱还有卡卡西老师一起完成一个D级任务——捕捉一只右耳有蝴蝶结,名叫小虎的小猫。");
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;
}
String commandWord = command.getCommandWord();
CommandWord cw = CommandWord.valueOf(commandWord);
switch(cw){
case help:
printHelp();
break;
case go:
wantToQuit = processGo(command);
break;
case quit:
wantToQuit = quit(command);
break;
case eat:
eat();
break;
case pick:
pick();
default:
}
return wantToQuit;
}
private boolean processGo(Command command){
player.walk();
if(!player.isAlive()){
System.out.println("你已经没有体力了,很遗憾,你没有完成任务!");
return true;
}
goRoom(command);
return false;
}
private void eat(){
Food food=currentRoom.getFood();
if(food==null){
System.out.println("这里没有可以补充体力的东西");
} else{
System.out.println("吃掉了食物");
player.eat(food.getEnergy());
currentRoom.removeFood();
}
}
private void pick(){
Food mao=currentRoom.getFood();
if(mao!=null){
System.out.println("哈哈,抓到了!可以回去提交任务了!");
}else{
System.out.println("是只狸猫,不是小虎哦,下次要看清楚。");
}
}
// 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();
CommandWords.printCommands();
}
/**
* 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;
currentRoom.printInfo();
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("要放弃吗?");
return false;
}
else {
return true; // signal that we want to quit
}
}
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Java
1
https://gitee.com/qiu-yuyu/zuul.git
git@gitee.com:qiu-yuyu/zuul.git
qiu-yuyu
zuul
zuul
master

搜索帮助

D67c1975 1850385 1daf7b77 1850385