加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
Game.java 7.88 KB
一键复制 编辑 原始数据 按行查看 历史
章强 提交于 2020-06-21 16:58 . 精灵宝可梦
/**
* 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 thing;
private Monster monster;
/**
* 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 volcano, pool, pub, office,room1,room2,room3,lotus_pond,room2_w,room2_e,
room3_e,room3_w,room4;
room1 = new Room("起点");
volcano = new Room("火山",new Monster("小火龙",2,10));
pool = new Room("水池",new Monster("杰尼龟",2,13));
room2 = new Room("空地");
room2_w = new Room("草地",new Monster("绿毛虫",1,9));
room2_e =new Room("海边",new Monster("长翅鸥",2,8));
room3 = new Room("灌木丛",new Monster("臭臭花",3,18));
room3_w = new Room("小荷塘",new Monster("大钳蟹",4,16));
room3_e = new Room("密室",new Thing("雷之石"));
room4=new Room("火箭队本部",new Monster("猫老大",10,50));
room1.setExit("east", volcano);
room1.setExit("north", room2);
room1.setExit("west", pool);
volcano.setExit("west",room1);
pool.setExit("east", room1);
room2.setExit("west", room2_w);
room2.setExit("east", room2_e);
room2_w.setExit("east",room2);
room2_e.setExit("west",room2);
room2.setExit("north", room3);
room3.setExit("west", room3_w);
room3.setExit("east", room3_e);
room3_e.setExit("west",room3);
room3_w.setExit("east",room3);
room3_e.setExit("north",room4);
currentRoom = room1; // 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("这这个世界里生活着人类和各种各样的宝可梦精灵,如喷火龙、杰尼龟等");
System.out.println("世界上有许多的野生宝可梦。");
System.out.println("人类通过收服野生的宝可梦精灵,从而成为训练家。");
System.out.println("训练家们指挥自己的宝可梦进行对决,不断变强。");
System.out.println("现在你是训练家小智的精灵:皮卡丘");
System.out.println("不过你被火箭队(坏人)抓走了");
System.out.println("你来到了一个陌生的地方。");
System.out.println("现在你还不能打败火箭队,需要升级提高自己。");
System.out.println("不断与宝可梦战斗,获得经验从而升级,最终去打败火箭队吧!!!");
System.out.println("You are " + 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 PICK:
pickThing();
break;
case LOOK:
lookRoom();
break;
case CHECK:
player.checkBag();
break;
case EAT:
eatThing(command);
break;
}
if(currentRoom.getDescription() == "火箭队本部"&&player.gethealth()>0) {
wantToQuit=true;
System.out.println("恭喜你!成功战胜了火箭队,你回到了训练家小智的身边");
System.out.println("游戏结束!");
}
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(" go quit help look check pick 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());
if(currentRoom.getMonster()!=null)
{
currentRoom.getMonster().Pk(player);
}
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 助手
尝试更多
代码解读
代码找茬
代码优化