Create your Gitee Account
Explore and code with more than 12 million developers,Free private repositories !:)
Sign up
文件
Clone or Download
Game.java 7.52 KB
Copy Edit Raw Blame History
岳颍辉 authored 2020-06-21 15:22 . 逃出迷宫
/**
* 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, theater, pub, lab, office, bigroom, goodroom;
// create the rooms
outside = new Room("神秘的地方");
theater = new Room("小房间",new Thing("面包",50));
pub = new Room("中房间",new Thing("药丸",-50));
lab = new Room("大房间",new Thing("钥匙",10000));
office = new Room("巨大的房间");
bigroom = new Room("一条死路",new Thing("自杀丸",-100));
goodroom = new Room("另一条死路");
// initialise room exits
//outside.setExits(null, theater, lab, pub,bigroom,null);
outside.setExit("east",theater);
outside.setExit("south",lab);
outside.setExit("west",pub);
//theater.setExits(null, null, null, outside,null,null);
theater.setExit("west",outside);
//pub.setExits(null, outside, null, null,null,goodroom);
pub.setExit("east",outside);
pub.setExit("down",bigroom);
//lab.setExits(outside, office, null, null,null,null);
lab.setExit("north",outside);
lab.setExit("east",office);
//office.setExits(null, null, null, lab,null,null);
office.setExit("west",lab);
//goodroom.setExits(null,null,null,null,pub,null);
//bigroom.setExits(null,null,null,null,null,outside);
bigroom.setExit("east",goodroom);
goodroom.setExit("west",bigroom);
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("你是在 " + 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 LOOK:
looks();
break;
case PICK:
picks();
break;
case EAT:
eats(command);
break;
case CHECK:
check();
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(" "+ CommandWords.allCommandWord());
}
/**
* 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)
{
int a=0;
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.goRoom(direction);
if (nextRoom == null) {
System.out.println("此路不通!");
}
else {
player.step();
currentRoom = nextRoom;
System.out.println("你在 " + currentRoom.getDescription());
currentRoom.printExits();
if(currentRoom.getDescription()=="神秘的地方")
{
a=checks();
if(a==1)
{
System.out.println("恭喜你找到了钥匙并回到了这里");
System.out.println("你打开了门走了出去");
System.out.println("愿你前程似锦");
System.out.println("加油");
System.exit(1);
}
else{
System.out.println("这有扇门,但你打不开");
System.out.println(a);
}
}
}
}
private void eats(Command command){
if(!command.hasSecondWord()) {
// if there is no second word, we don't know where to go...
System.out.println("吃什么????");
return;
}
String foot = command.getSecondWord();
t.eat(foot);
}
private void looks(){
System.out.println(currentRoom.getThing());
}
Player t=new Player();
private void picks(){
t.pick(currentRoom.thing);
currentRoom.removeThing();
}
private int checks(){
return t.checksss();
}
private void check(){
t.checkss();
}
/**
* "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 助手
尝试更多
代码解读
代码找茬
代码优化