1 Star 0 Fork 123

钟益鹏/zuul

forked from 吕焱飞/zuul 
加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
Game.java 9.94 KB
一键复制 编辑 原始数据 按行查看 历史
钟益鹏 提交于 2021-06-21 13:55 . zuoye
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 Player player;
private Thing currentThing;
private int count = 0;
/**
* 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, pub, Build, GhostBuild1, GhostBuild2, GhostBuild3;
Room church, field, hall, cinema, store, pool, basketball_field;
// create the rooms
outside = new Room("你攥紧了佛珠,决心一定要跑出去");
Build = new Room("公交车站");
GhostBuild1 = new Room("鬼楼一");
GhostBuild2 = new Room("鬼楼二");
GhostBuild3 = new Room("鬼楼三");
field = new Room("田地");
cinema = new Room("电影院");
church = new Room("教堂");
store = new Room("商店");
hall = new Room("报告厅");
pool = new Room("游泳池");
// initialise room exits
outside.setExit("北", hall);
outside.setThing(new Thing("苹果",10));
outside.setThing(new Thing("香蕉",15));
hall.setExit("北", GhostBuild1);
hall.setExit("南", outside);
hall.setExit("东", store);
hall.setThing(new Thing("苹果", 10));
hall.setThing(new Thing("金钥匙", -1));
GhostBuild1.setExit("东", GhostBuild2);
GhostBuild1.setExit("南", hall);
GhostBuild1.setThing(new Thing("红色木盒", -1));
GhostBuild1.setThing(new Thing("textbook", -5));
GhostBuild2.setExit("东", GhostBuild3);
GhostBuild2.setExit("南", store);
GhostBuild2.setThing(new Thing("红色衣服", -5));
GhostBuild3.setExit("南", cinema);
GhostBuild3.setExit("西", GhostBuild2);
store.setExit("北", GhostBuild2);
store.setExit("南", church);
store.setExit("东", cinema);
store.setThing(new Thing("薯片", 5));
store.setThing(new Thing("水", 5));
store.setThing(new Thing("面包", 15));
church.setExit("北",store);
church.setExit("东",store);
church.setThing(new Thing("刘大师的宝器", 50));
church.setThing(new Thing("牛奶", 15));
pool.setExit("西", church);
pool.setExit("北", church);
pool.setThing(new Thing("小吃", 15));
cinema.setExit("北", GhostBuild3);
cinema.setExit("南", pool);
cinema.setExit("西", store);
cinema.setThing(new Thing("地图", 3));
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("Thank you for playing. Good bye.");
}
/**
* Print out the opening message for the player.
*/
private void printWelcome()
{
System.out.println();
System.out.println("徒弟:师傅,你看有个叫唐显生的人叫我拿了一盒东西给您");
System.out.println("师傅:唐显生!!十年前死掉的那个13路公交车司机才叫唐显生!!");
System.out.println("师傅:快,快去山东菏泽曹县找刘庆柱!!");
System.out.println("十年前的那场意外就算是我吴云波也无能为力啊,这个佛珠可保你一时平安");
System.out.println("然后鬼魂抢先一步,刘大师近在咫尺,你却无能为力,绝望之际刘大师在鬼打墙中为你部下法宝");
System.out.println("只要找到法宝在阵眼便可以出去");
System.out.println("有问题请输入‘help’寻求帮助");
System.out.println();
System.out.println("你在" + currentRoom.getDescription());
System.out.println();
currentRoom.checkThing();
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 cmd = CommandWord.valueOf(commandWord.toUpperCase());
switch(cmd) {
case HELP:
printHelp();
break;
case GO:
player.walk();
if (!player.isAlive()) {
System.out.println("你已经没有体力");
return true;
}
goRoom(command);
break;
case QUIT:
wantToQuit = quit(command);
break;
case EAT:
eat(command);
break;
case PICK:
pickThing(command);
break;
case BAG:
player.checkBag();
break;
default:
}
return wantToQuit;
}
private void eat(Command command) {
String name = command.getSecondWord();
Thing thing = currentRoom.getThing(name);
if(!command.hasSecondWord()) {
System.out.println("请与对象一起输入");
return;
}
if(currentRoom.getThing(name) != null) {
Thing food = currentRoom.getThing(name);
player.eatInRoom(thing);
System.out.println("吃掉了:" + food.toString());
player.check();
currentRoom.removeThing(name);
currentRoom.checkThing();
currentRoom.printExits();
return;
}
else {
player.eat(command.getSecondWord());
}
}
// 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();
CommandWords.addCommandsList();
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?");
return;
}
String direction = command.getSecondWord();
Room nextRoom = currentRoom.goNext(direction);
currentRoom = nextRoom;
if (nextRoom == null) {
System.out.println("没有这个出口!");
}
else
{
if(currentRoom.checkroom().equals(currentRoom.getDescription()))
{
if(count == 1){
System.out.println("通关");
System.exit(0);
}
else if(count == 2) {
System.out.println("特殊通关");
System.exit(0);
}
}
else{
System.out.println("你在 " + currentRoom.getDescription());
currentRoom.checkThing();
currentRoom.printExits();
player.check();
}
}
}
private void pickThing(Command command)
{
String name = command.getSecondWord();
if(!command.hasSecondWord()) {
if (currentRoom.getThing(name) == null) {
System.out.println("请与对象一起输入");
return;
}
}
else if(currentRoom.getThing(name) == null) {
System.out.println("这里没有可以捡的东西");
return;
}
if(currentRoom.checkbox().equals(name)){
count++;
}
if(currentRoom.checkgoods().equals(name)){
count++;
}
player.pick(currentRoom.getThing(name));
currentRoom.removeThing(name);
}
/**
* "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
}
}
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Java
1
https://gitee.com/zhong-yipeng/zuul.git
git@gitee.com:zhong-yipeng/zuul.git
zhong-yipeng
zuul
zuul
master

搜索帮助

D67c1975 1850385 1daf7b77 1850385