加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
Game.java 9.64 KB
一键复制 编辑 原始数据 按行查看 历史
方子聪 提交于 2020-06-20 16:21 . 迷宫
/**
* 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;
Room R0_0,R1_1, R1_2, R1_3, R1_5, R2_1, R2_2, R2_3, R2_4, R2_5,R3_1,R3_2,R3_3,R3_4,R3_5,R3_6,R4_1,R4_3,R4_5,R4_6,R5_1,R5_3,R5_4,R5_5,R5_6;
/**
* 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()
{
// create the rooms
R1_1 = new Room("R1_1");
R1_2 = new Room("R1_2", new Thing("小药剂", 20));
R1_3 = new Room("R1_3");
R1_5 = new Room("R1_5", new Trap("剧毒蝙蝠", 50));
R2_1 = new Room("R2_1");
R2_2 = new Room("R2_2");
R2_3 = new Room("R2_3", new Thing("药剂", 30));
R2_4 = new Room("R2_4", new Trap("小蝙蝠", 10));
R2_5 = new Room("R2_5");
R3_1 = new Room("R3_1", new Trap("蝙蝠群", 30));
R3_2 = new Room("R3_2", new Trap("老鼠", 20));
R3_3 = new Room("R3_3");
R3_4 = new Room("R3_4", new Trap("蛇", 10));
R3_5 = new Room("R3_5");
R3_6 = new Room("R3_6", new Thing("金钥匙"));
R4_1 = new Room("R4_1", new Thing("恢复药", 100));
R4_3 = new Room("R4_3", new Thing("小药剂", 20));
R4_5 = new Room("R4_5", new Thing("药剂", 30));
R4_6 = new Room("R4_6");
R5_1 = new Room("R5_1", new Thing("铜钥匙"));
R5_3 = new Room("R5_3", new Trap("蝙蝠群", 30));
R5_4 = new Room("R5_4", new Thing("银钥匙"));
R5_5 = new Room("R5_5", new Trap("蝎子", 20));
R5_6 = new Room("R5_6", new Trap("蝎子群", 30));
R1_1.setExit("north", R2_1);
R1_1.setExit("east", R1_2);
R1_2.setExit("west", R1_1);
R1_2.setExit("east", R1_3);
R1_3.setExit("north", R2_3);
R1_3.setExit("west", R1_2);
R1_5.setExit("north", R2_5);
R2_5.setExit("south", R1_5);
R2_5.setExit("north", R3_5);
R2_1.setExit("north", R3_1);
R2_1.setExit("south", R1_1);
R2_1.setExit("west", R2_5);
R2_1.setExit("east", R2_2);
R2_2.setExit("north", R3_2);
R2_2.setExit("west", R2_1);
R2_3.setExit("south", R1_3);
R2_3.setExit("east", R2_4);
R2_4.setExit("west", R2_3);
R2_4.setExit("north", R3_4);
R3_5.setExit("north", R4_5);
R3_5.setExit("south", R2_5);
R3_1.setExit("south", R2_1);
R3_1.setExit("north", R4_1);
R3_2.setExit("south", R2_2);
R3_2.setExit("east", R3_3);
R3_3.setExit("north", R4_3);
R3_3.setExit("west", R3_2);
R3_3.setExit("east", R3_4);
R3_4.setExit("west", R3_3);
R3_4.setExit("south", R2_4);
R4_6.setExit("north", R5_6);
R4_6.setExit("east", R4_5);
R4_5.setExit("north", R5_5);
R4_5.setExit("west", R4_6);
R4_5.setExit("south", R3_5);
R4_1.setExit("north", R5_1);
R4_1.setExit("south", R3_1);
R4_3.setExit("south", R3_3);
R5_6.setExit("east", R5_5);
R5_6.setExit("south", R4_6);
R5_5.setExit("west", R5_6);
R5_5.setExit("south", R4_5);
R5_1.setExit("south", R4_1);
R5_3.setExit("east", R5_4);
R5_4.setExit("west", R5_3);
currentRoom = R1_1; // 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("输入 '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("I don't know what you mean...");
return false;
}
Word commandWord = command.getCommandWord();
switch(commandWord) {
case HELP:
printHelp();
break;
case GO:
goRoom(command);
break;
case QUIT:
quit(command) ;
break;
case PICK:
pickThing();
break;
case EAT:
eatThing(command);
break;
case CHECK:
player.checkBag();
break;
case LOOK:
lookRoom();
break;
}
if(player.isDead()){
wantToQuit = true;
System.out.println("你死了");
}
if(currentRoom == R0_0){
wantToQuit = true;
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("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 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("Go where?");
return;
}
String direction = command.getSecondWord();
// Try to leave current room.
Room nextRoom = currentRoom.goNext(direction);
if (nextRoom == null) {
System.out.println("There is no door!");
}
else {
currentRoom = nextRoom;
System.out.println("你在 " + currentRoom.getDescription());
currentRoom.printExits();
if(currentRoom.getTrap()!=null){
player.monster(currentRoom.getTrap().getEnergy());
System.out.println("你遇到了"+currentRoom.getTrap().getName()+"并扣除:"+currentRoom.getTrap().getEnergy());
}
}
}
private void lookRoom()
{
if(currentRoom.getThing()==null)
System.out.println("nothing in there!");
else System.out.println(currentRoom.getThing().getName());
}
private void pickThing()
{
player.pick(currentRoom.getThing());
if(currentRoom.getThing().getName() == "铜钥匙")
{
R5_3.setExit("south", R4_3);
R4_3.setExit("north", R5_3);
System.out.println("你打开了R4_3通往R5_3路");
}
if(currentRoom.getThing().getName() == "银钥匙")
{
R3_6.setExit("north", R4_6);
R4_6.setExit("south", R3_6);
System.out.println("你打开了R4_6通往R3_6路");
}
if(currentRoom.getThing().getName() == "金钥匙")
{
R0_0.setExit("north", R1_1);
R1_1.setExit("south", R0_0);
System.out.println("你打开了通往终点的门,但是门在哪里呢?也许你可以回去看看!");
}
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("Eat something?");
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("Quit what?");
return false;
}
else {
return true; // signal that we want to quit
}
}
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化