Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package edu.pcc.cis.zuul;
- import java.util.ArrayList;
- public class Game
- {
- private Parser parser;
- private Room currentRoom;
- private ArrayList<Item> item1;
- /**
- * 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 entrance, policestation, club, lab, cityhall, hospital, center, cellar, watchtower;
- // create the rooms
- entrance = new Room("at the main entrance of town, a thick blanket fog covers the entirety of town");
- entrance.addItem("and found a dirty Journal", 5);
- cellar = new Room("down into the cellar the smell of decomposing matter is strong");
- cellar.addItem("and found a folder", 2);
- watchtower = new Room("up in the watchtower, it is cold and you can't see anything thanks to the fog");
- watchtower.addItem("and found a pair of binoculars", 4);
- policestation = new Room("in the police station, you find find evidence of a gunfight that left bullet holes scattered throughout the place");
- policestation.addItem("and found a gun", 6);
- club = new Room("in the local club, where you find broken bottles scattered around the premises covered in strange black goo.");
- club.addItem("and found a water flask", 3);
- lab = new Room("in the science lab, you find broken glass and the signs of an explosion");
- lab.addItem("and found a vial of strange liquid", 1);
- cityhall = new Room("in the mayor's office, you find papers strewn all over the place");
- cityhall.addItem("and found a notebook full of notes", 2);
- hospital = new Room("in the hospital, you find signs of a struggle along with black goo splattered all over the place along with the sound of something scratching against one of the doors.");
- hospital.addItem("and found a small first-aid kit alogn with a stash of granolar bars", 7);
- center = new Room("in the center of town, you find signs of a huge accident along with cars that are either burnt out are have been broken into. You find black goo on some of the broken windows.");
- center.addItem("and found a flashlight", 2);
- // initialise room exits
- entrance.setExit("east", policestation);
- entrance.setExit("south", center);
- policestation.setExit("up", watchtower);
- policestation.setExit("west", entrance);
- club.setExit("north", center);
- lab.setExit("north", hospital);
- lab.setExit("down", cellar);
- cityhall.setExit("east", center);
- hospital.setExit("west", center);
- hospital.setExit("south", lab);
- center.setExit("north", entrance);
- center.setExit("east", hospital);
- center.setExit("south", club);
- center.setExit("north", entrance);
- cellar.setExit("up", lab);
- watchtower.setExit("down", policestation);
- currentRoom = entrance; // start game outside
- }
- /**
- * edu.pcc.cis.zuul.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 Town of Silent Hill!");
- System.out.println("Town of Silent Hill is a adventure game where You, a detective, must find clues as to happened to the inhabitants of the town of Silent Hill.");
- System.out.println("Type 'help' if you need help.");
- System.out.println();
- printLocationInfo();
- }
- /**
- * 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;
- }
- String commandWord = command.getCommandWord();
- if (commandWord.equals("help")) {
- printHelp();
- }
- else if (commandWord.equals("go")) {
- goRoom(command);
- }
- else if (commandWord.equals("look")) {
- look();
- }
- else if (commandWord.equals("eat")) {
- eat();
- }
- else if (commandWord.equals("quit")) {
- wantToQuit = quit(command);
- }
- return wantToQuit;
- }
- /**
- * Look command
- * @return output of Look()
- */
- private void look()
- {
- System.out.println(currentRoom.getLongDescription());
- }
- /**
- * Eat command
- * @return description if you've eaten food.
- */
- private void eat()
- {
- System.out.println("You have eaten now and you are not hungry any more");
- }
- // implementations of user commands:
- /**
- * Print out some help information.
- * Here we print some 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 the abandoned town.");
- System.out.println();
- System.out.println("Your command words are:");
- parser.showCommands();
- }
- /**
- * 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.getExit(direction);
- if (nextRoom == null) {
- System.out.println("That is not a valid direction!");
- }
- else {
- currentRoom = nextRoom;
- printLocationInfo();
- }
- }
- /**
- * Print Location Info.
- * @return directions available and the description of the current room.
- */
- private void printLocationInfo()
- {
- System.out.print("Directions: ");
- System.out.println();
- 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
- }
- }
- }
- ////////////////////////////////////////////////////////////////////////////////////////////////////////////////
- Main class:
- package edu.pcc.cis.zuul;
- //Zuul transporter project
- public class Main {
- public static void main(String[] args) {
- new Game().start();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement