Dobrý den,
chtěl bych Vás poprosit, jestli by mi někdo nemohl pomoc vymyslet jak, aby fungovalo stále dokola příkaz 'back'. Pro vysvětlení, hráč se pohybuje světem, vždy začíná v oblasti 'outside' poté může chodit možnými směry (south, north, up, down,....) příkazem 'go' a posouvá se do dalších místností, já jsem do toho vložil příkaz 'back' s tím, že se vrátí do té předchozí místnosti, ale funguje mi to pouze vždy jednou a poté zůstává v té jedné vracené místnosti. Já bych chtěl, aby se vracel cestou, kterou chtěl, třeba až na začátek. Děkuji za odpověď. Doufám, že jste to trochu pochopili.
package zuul_bad_zeman;
import zuul_bad_zeman.Parser;
import zuul_bad_zeman.Command;
public class Game {
private Parser parser;
private Room currentRoom;
private Room previousRoom;
public Game() {
createRooms();
parser = new Parser();
}
private void createRooms() {
Room outside, theater, pub, lab, office, shop, hotel, shopSecondFloor, library, underground, pubRoof;
outside = new Room("outside", "outside the main entrance of the university");
theater = new Room("theater", "in a lecture theater");
pub = new Room("pub", "in the campus pub");
lab = new Room("lab", "in a computing lab");
office = new Room("office", "in the computing admin office");
shop = new Room("shop", "in a shop, we have a good CocaCola");
hotel = new Room("hotel", "in a hotel, WELCOME!");
shopSecondFloor = new Room("shopSecondFloor", "in second floor in the shop");
library = new Room("library", "in library there are awesome books");
underground = new Room("underground","in the underground campus ground");
pubRoof = new Room("pubRoof", "in the pub roof");
outside.setExit("north", shop);
outside.setExit("east", theater);
outside.setExit("south", lab);
outside.setExit("west", pub);
theater.setExit("east", library);
theater.setExit("west", outside);
pub.setExit("east", outside);
pub.setExit("west", hotel);
lab.setExit("north", outside);
lab.setExit("east", office);
office.setExit("west", lab);
shop.setExit("south", outside);
shop.setExit("up", shopSecondFloor);
hotel.setExit("east", pub);
shopSecondFloor.setExit("down", shop);
library.setExit("west", theater);
pub.setExit("up", pubRoof);
outside.setExit("down", underground);
underground.setExit("up", outside);
pubRoof.setExit("down", pub);
currentRoom = outside; //začátek hry
}
public void play() {
printWelcome();
boolean finished = false;
while (!finished) {
Command command = parser.getCommand();
finished = processCommand(command);
}
System.out.println("Thank you for playing! Good bye.");
}
private void printWelcome() {
System.out.println();
System.out.println("Welcome to the World of Zuul!");
System.out.println("World of Zuul is a new, incredibly boring adventure game.");
System.out.println("Type 'help' if you need help.");
System.out.println(currentRoom.whereYouAre());
}
private boolean processCommand(Command command) {
boolean wantToQuit = false;
if (command.isUnknown()) {
System.out.println("I don't know what you mean...");
System.out.println("Please, be careful.");
System.out.println(currentRoom.whereYouAre());
return false;
}
String commandWord = command.getCommandWord();
if (commandWord.equals("help")) {
printHelp();
}
else if (commandWord.equals("go")) {
goRoom(command);
}
else if (commandWord.equals("back")) {
back(command);
}
else if (commandWord.equals("look")) {
doLook();
}
else if (commandWord.equals("quit")) {
wantToQuit = quit(command);
}
return wantToQuit;
}
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 back look help quit");
System.out.println(currentRoom.whereYouAre());
}
private void goRoom(Command command) {
if (!command.hasSecondWord()) {
System.out.println("Go where?");
return;
}
String direction = command.getSecondWord();
Room nextRoom = currentRoom.getExit(direction);
if (nextRoom == null) {
System.out.println("There is no door!");
System.out.println("Please, be careful.");
System.out.println(currentRoom.whereYouAre());
} else {
previousRoom = currentRoom;
currentRoom = nextRoom;
System.out.println(currentRoom.whereYouAre());
}
}
private void doLook(){
System.out.println(currentRoom.lookYouAre());
}
private boolean quit(Command command) {
if (command.hasSecondWord()) {
System.out.println("Quit what?");
return false;
} else {
return true;
}
}
private void back(Command command) {
if (command.hasSecondWord()) {
System.out.print("I don't know what you mean...\n");
System.out.println("Try it again");
System.out.println(currentRoom.whereYouAre());
return;
}
if (previousRoom == null) {
System.out.print("Sorry you can't go back\n");
System.out.println(currentRoom.whereYouAre());
return;
}
currentRoom = previousRoom;
System.out.println(currentRoom.whereYouAre());
}
}