Danmark vil mindske afhængigheden af globale techgiganter, men det kræver mere end politiske formuleringer og strategier, understreger PROSA’s formand Niels Bertelsen.
Dette virker fint og står i samme klasse som det ovenfor:
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.getExit(direction);
if (nextRoom == null) System.out.println("There is no door!"); else { currentRoom = nextRoom; System.out.println(currentRoom.getLongDescription()); } }
class Game { private Parser parser; private Room currentRoom;
/** * Create the game and initialise its internal map. */ public Game() { createRooms(); parser = new Parser(); }
/** * Create all the rooms and link their exits together. */ private void createRooms() { Room plaza, pub, kirke, motel, huset, hall, spisestue, bibliotek, trapperum, indgang, rotte, fange, nøgle, indgang1, trappetiltårn, jagtrum, balsal, balkon, tower;
// UDENFOR plaza = new Room("Du står nu ude på pladsen"); pub = new Room("Velkommen til Vodka De Kovaltsenko"); kirke = new Room("Du er nu i kirke"); motel = new Room("Velkommen til Mies Sleepover"); huset = new Room("Du står nu udenfor huset");
// HAUNTED HOUSE //STUE
hall = new Room("Du er nu i Hall'en"); spisestue = new Room("Du er nu i spisestuen"); bibliotek = new Room("Velkommen til det støvede bibliotek"); trapperum = new Room("Du står nu ved to trapper - en op og en ned");
// KÆLDER
indgang = new Room("Du er nu kommet ned i den mørke kælder"); rotte = new Room("Du er nu blevet spist af en mutantrotte"); fange = new Room("Du er nu gået ind i fangekælderen"); nøgle = new Room("Du er nu kommet ind i rummet med nøglen");
// 1. SAL
indgang1 = new Room("Du er nu på 1. sal"); trappetiltårn = new Room("Du er nu i et rum hvor der går en trappe op til tårnet"); jagtrum = new Room("Du er nu i husets jagtrum"); balsal = new Room("Du er nu i balsalen"); balkon = new Room("Du står på balkonnen og kigger på den flotte have");
// TÅRN
tower = new Room("Du er nu i tårnet.... udsigten er bare for fed");
// PLAZA plaza.setExit("east", pub); plaza.setLook("east", "Der er noget"); plaza.setExit("south", motel); plaza.setExit("west", kirke); plaza.setExit("north", huset);
/** * 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 Adventure!"); System.out.println("Adventure is a new, incredibly boring adventure game."); System.out.println("Type 'help' if you need help."); System.out.println(); System.out.println(currentRoom.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("look")) lookAround(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 = currentRoom.getExit(direction);
if (nextRoom == null) System.out.println("There is no door!"); else { currentRoom = nextRoom; System.out.println(currentRoom.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 } }
class Room { private String description; private HashMap look; private HashMap exits; // stores exits of this room.
/** * Create a room described "description". Initially, it has no exits. * "description" is something like "in a kitchen" or "in an open court * yard". */ public Room(String description) { this.description = description; look = new HashMap(); exits = new HashMap(); }
/** * Define an exit from this room. */ public void setExit(String direction, Room neighbor) { exits.put(direction, neighbor); }
public void setLook(String direction, String rummetsUdseende) { look.put(direction, rummetsUdseende); }
/** * Return the description of the room (the one that was defined in the * constructor). */ public String getShortDescription() { return description; }
/** * Return a long description of this room, in the form: * You are in the kitchen. * Exits: north west */ public String getLongDescription() { return "You are " + description + ".\n" + getExitString(); }
/** * Return a string describing the room's exits, for example * "Exits: north west". */
/** * Return the room that is reached if we go from this room in direction * "direction". If there is no room in that direction, return null. */ public Room getExit(String direction) { return (Room)exits.get(direction); }
public Room getLook(String direction) { return (Room)look.get(direction); } }
men hvis du skapar en getLooks() metode i Room kan du anropa den fra Game, med Room room = new Room(); room.getLooks();
Synes godt om
Ny brugerNybegynder
Din løsning...
Tilladte BB-code-tags: [b]fed[/b] [i]kursiv[/i] [u]understreget[/u] Web- og emailadresser omdannes automatisk til links. Der sættes "nofollow" på alle links.