我正在为这个类的 Zuul Game 工作,我必须将 currentRoom 变量从 Game 类重构为 Player 类。我对大部分代码都很有信心,但是当我尝试编译时,Cannot Find Symbol - Method 错误不断出现。请帮忙。
这是游戏类:
class Game {
private Parser parser;
private Scenario scenario;
/**
* Create the game and initialise its internal map.
*/
public Game() {
scenario = new Scenario();
parser = new Parser();
}
/**
* 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("Welcome to the world of Zuul!");
System.out.println("Zuul is a new, incredibly boring adventure game.");
System.out.println("Type 'help' if you need help.");
System.out.println();
System.out.println(getCurrentRoom().getLongDescription());
}
/**
* Given a command, process (that is: execute) the command.
* If this command ends the game, true is returned, otherwise false is
* returned.
*/
private boolean processCommand(Command command) {
boolean wantToQuit = false;
if (command.isUnknown()) {
System.out.println("I don't know what you mean...");
return false;
}
String commandWord = command.getCommandWord();
if (commandWord.equals("help"))
printHelp();
else if (commandWord.equals("go"))
goRoom(command);
else if (commandWord.equals("quit")) {
wantToQuit = quit(command);
}
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:");
parser.showCommands();
}
/**
* Try to go to 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 = getCurrentRoom().getExit(direction);
if (nextRoom == null)
System.out.println("There is no door!");
else {
getCurrentRoom() = nextRoom;
System.out.println(getCurrentRoom().getLongDescription());
}
}
/**
* "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
}
}
这是播放器类:
public class Player {
private Room currentRoom;
/**
* Constructor for objects of class Player
*/
public Player() {
currentRoom = getBeginRoom();
}
/**
* Sets the current room that the player is in.
*/
public void setCurrentRoom(Room room) {
currentRoom = room;
}
/**
* Returns the current room that the player is in.
*/
public Room getCurrentRoom() {
return currentRoom;
}
}
这是场景类:
public class Scenario {
private List rooms; // List of all rooms in the game
private Room beginRoom;
private Random random;
/**
* Create a complete scenario (with all rooms).
*/
public Scenario() {
Room outside, theatre, pub, lab, office, transporter;
// create the rooms
outside = new Room("outside the main entrance of the university");
theatre = new Room("in a lecture theatre");
pub = new Room("in the campus pub");
lab = new Room("in a computing lab");
office = new Room("in the computing admin office");
transporter = new TransporterRoom("in a dark place", this);
// initialise room exits
outside.setExit("east", theatre);
outside.setExit("south", lab);
outside.setExit("west", pub);
theatre.setExit("west", outside);
theatre.setExit("north", transporter);
transporter.setExit("south", theatre);
pub.setExit("east", outside);
lab.setExit("north", outside);
lab.setExit("east", office);
office.setExit("west", lab);
beginRoom = outside; // start game outside
rooms = new ArrayList();
rooms.add(outside);
rooms.add(theatre);
rooms.add(pub);
rooms.add(lab);
rooms.add(office);
rooms.add(transporter);
random = new Random();
}
/**
* Return the room where players start the game.
*/
public Room getBeginRoom() {
return beginRoom;
}
/**
* Return a random room in this game.
*/
public Room getRandomRoom() {
return (Room) rooms.get(random.nextInt(rooms.size()));
}
}
谢谢!