package Spork.Spork; import java.util.Random; /** * This class is based upon Zuul by Michael Kolling and David J. Barnes. * This is the main Spork class. Game objects are defined here and the * user input is parsed. * * @author Andrew Cronk * @version 0.1 */ public class Game { private Console io; // Declare Rooms // You must have a currentRoom private Room currentRoom; private Room bedroom, darkCave, katie2MazeEntrance, strawberryRoom, volleyballCourt, tennisCourt, cupcakeRoom, nerdlyRoom, sprinklesRoom, playland, lollipopRoom, hedgehogRoom, room1, room2, room3, room4, room5, room6, room7, home; //use commas to separate Rooms // Declare items //You must have a Character private Character you; private Item flashlight, tennisRacket, cupcake, smarties, licorice, snickers, donut, nerds, reeses, volleyball, skittles, candyCane, mAndMs, hersheyBar, milkyWay, gummyBear, sourPatch, lollipop, jellybeans, ribbon; // Declare Characters // You must have a player Character private Character player; private Character person; private Random rnd; public static void main (String[] args) { Game newGame = new Game(); newGame.play(); } /** * Create the game and initialise its internal map. */ public Game() { io = new Console(); rnd = new Random(); // Uncomment line below when finished with Character class player = new Character(); configureGameObjects(); } /** * Create and configure all the game objects */ private void configureGameObjects() { // Create Rooms // outside = new Room("outside"); bedroom = new Room("your familiar bedroom"); darkCave = new Room("a small cave"); katie2MazeEntrance = new Room("the Katie Squared Maze"); strawberryRoom = new Room("the Strawberry Room"); volleyballCourt = new Room("the Volleyball Court"); tennisCourt = new Room("the Tennis Court"); room1 = new Room("a room"); room2 = new Room("a room"); room3 = new Room("a room"); room4 = new Room("a room"); room5 = new Room("a room"); room6 = new Room("a room"); room7 = new Room("a room"); sprinklesRoom = new Room("the Sprinkles Room"); lollipopRoom = new Room("the Lollipop Room"); playland = new Room("Playland"); home = new Room("YOUR HOUSE!!!"); cupcakeRoom = new Room("the Cupcake Room"); nerdlyRoom = new Room("the Nerdly Room"); hedgehogRoom = new Room("the Hedgehog Room"); // Set Room descriptions // outside.setDescription("You are outside Sunset High School."); bedroom.setDescription("But a large dark hole has opened on your wall... dare you enter?!?"); darkCave.setDescription("there are 4 exits, a big opening behind you\nand small holes in the wall to the south, southeast, and east"); katie2MazeEntrance.setDescription("This is the entrance to a MAZE... good luck!!!"); strawberryRoom.setDescription("This strawberry-filled room is filled with strawberry flavored things!!"); tennisCourt.setDescription("You can play tennis...if you want to,\nbut entering here without the tennis racket is DEADLY"); volleyballCourt.setDescription("You can play volleyball...if you want to,\nbut entering here without the volleyball is DEADLY"); cupcakeRoom.setDescription("This is a room full of cupcakes!!!"); nerdlyRoom.setDescription("This is a room full of nerdly candy!!!"); playland.setDescription("This is a room full of toys!!!"); lollipopRoom.setDescription("This is a room full of lollipops!!!"); home.setDescription("You have gotten back to you house before your parents got home! Good job :D"); hedgehogRoom.setDescription("This is a room full of hedgehogs!!!"); sprinklesRoom.setDescription("This is a room full of sprinkles!!!"); room1.setDescription("This is a room of the maze..."); room2.setDescription("This is a room of the maze..."); room3.setDescription("This is a room of the maze..."); room4.setDescription("This is a room of the maze..."); room5.setDescription("This is a room of the maze..."); room6.setDescription("This is a room of the maze..."); room7.setDescription("This is a room of the maze..."); // initialise room exits // outside.setExit("north", oneHallS); bedroom.setExit("south", darkCave); darkCave.setExit("north", bedroom); darkCave.setExit("east", volleyballCourt); darkCave.setExit("south", katie2MazeEntrance); darkCave.setExit("southeast", cupcakeRoom); cupcakeRoom.setExit("northwest", darkCave); cupcakeRoom.setExit("east", nerdlyRoom); nerdlyRoom.setExit("west", darkCave); nerdlyRoom.setExit("south", cupcakeRoom); katie2MazeEntrance.setExit("west", darkCave); katie2MazeEntrance.setExit("southeast", room1); room1.setExit("north", katie2MazeEntrance); room1.setExit("west", katie2MazeEntrance); room1.setExit("east", room2); room2.setExit("south", room1); room2.setExit("west", katie2MazeEntrance); room2.setExit("northeast", room3); room3.setExit("southwest", room2); room3.setExit("west", katie2MazeEntrance); room3.setExit("southeast", room4); room4.setExit("northwest", room3); room4.setExit("west", room5); room4.setExit("south", katie2MazeEntrance); room5.setExit("west", room4); room5.setExit("north", katie2MazeEntrance); room5.setExit("northeast", room6); room6.setExit("south", room5); room6.setExit("west", katie2MazeEntrance); room6.setExit("northwest", room7); room7.setExit("southeast", room6); room7.setExit("west", tennisCourt); room7.setExit("northwest", darkCave); tennisCourt.setExit("north", room7); strawberryRoom.setExit("south", hedgehogRoom); cupcakeRoom.setExit("south", sprinklesRoom); sprinklesRoom.setExit("northeast", cupcakeRoom); sprinklesRoom.setExit("south", playland); playland.setExit("west", sprinklesRoom); playland.setExit("southeast", lollipopRoom); lollipopRoom.setExit("south", playland); lollipopRoom.setExit("west", tennisCourt); lollipopRoom.setExit("north", hedgehogRoom); sprinklesRoom.setExit("southeast", tennisCourt); hedgehogRoom.setExit("northwest", lollipopRoom); hedgehogRoom.setExit("southwest", home); volleyballCourt.setExit("south", strawberryRoom); tennisCourt.setExit("northwest", sprinklesRoom); // Create Items // key = new Item("key"); flashlight = new Item ("flashlight"); tennisRacket = new Item ("tennis racket"); cupcake = new Item ("cupcake"); smarties = new Item ("smarties"); licorice = new Item ("licorice"); snickers = new Item ("snickers"); donut = new Item ("donut"); nerds = new Item ("nerds"); reeses = new Item ("reeses"); volleyball = new Item ("volleyball"); skittles = new Item ("skittles"); candyCane = new Item ("candy cane"); mAndMs = new Item ("m&ms"); hersheyBar = new Item ("hershey bar"); milkyWay = new Item ("milky way"); gummyBear = new Item ("gummy bear"); sourPatch = new Item ("sour patch"); lollipop = new Item ("lollipop"); jellybeans = new Item ("jellybeans"); ribbon = new Item ("ribbon"); // Set Item descriptions // key.setDescription("A master key"); flashlight.setDescription("This is a flashlight, its used for light"); tennisRacket.setDescription("This a tennis racket, its used for playing tennis..."); cupcake.setDescription("a piece of candy!"); smarties.setDescription("a piece of candy!"); licorice.setDescription("a piece of candy!"); snickers.setDescription("a piece of candy!"); donut.setDescription("a piece of candy!"); nerds.setDescription("a piece of candy!"); reeses.setDescription("a piece of candy!"); volleyball.setDescription("This a volleyball, its used for playing volleyball..."); skittles.setDescription("a piece of candy!"); candyCane.setDescription("a piece of candy!"); mAndMs.setDescription("a piece of candy!"); hersheyBar.setDescription("a piece of candy!"); milkyWay.setDescription("a piece of candy!"); gummyBear.setDescription("a piece of candy!"); sourPatch.setDescription("a piece of candy!"); lollipop.setDescription("a piece of candy!"); jellybeans.setDescription("a piece of candy!"); ribbon.setDescription("A pretty ribbon!!!"); // Add Items to Rooms // t4.setItem(key.getKey(), key); katie2MazeEntrance.setItem(donut.getKey(), donut); bedroom.setItem(flashlight.getKey(), flashlight); darkCave.setItem(tennisRacket.getKey(), tennisRacket); strawberryRoom.setItem(gummyBear.getKey(), gummyBear); volleyballCourt.setItem(lollipop.getKey(), lollipop); tennisCourt.setItem(snickers.getKey(), snickers); cupcakeRoom.setItem(cupcake.getKey(), cupcake); nerdlyRoom.setItem(smarties.getKey(), smarties); sprinklesRoom.setItem(licorice.getKey(), licorice); playland.setItem(jellybeans.getKey(), jellybeans); lollipopRoom.setItem(milkyWay.getKey(), milkyWay); hedgehogRoom.setItem(sourPatch.getKey(), sourPatch); room1.setItem(nerds.getKey(), nerds); room2.setItem(reeses.getKey(), reeses); room3.setItem(volleyball.getKey(), volleyball); room4.setItem(skittles.getKey(), skittles); room5.setItem(candyCane.getKey(), candyCane); room6.setItem(mAndMs.getKey(), mAndMs); room7.setItem(hersheyBar.getKey(), hersheyBar); home.setItem(ribbon.getKey(), ribbon); //use commas to separate Rooms // Create Characters // cronk = new Character("cronk"); person = new Character("Bob"); // Set Character descriptions // cronk.setDescription("Mr. Cronk is bald."); person.setDescription("Person in the House"); // Put Characters in Rooms // f2.setNPC(cronk); katie2MazeEntrance.setNPC(person); // Set Character Stats // cronk.setStat(20); person.setStat(20); // Set the player stat // player.setStat(20); player.setStat(20); // Set the currentRoom // currentRoom = outside; // start game outside currentRoom = bedroom; //start game inside katie2MazeEntrance } /** * Main play routine. Loops until end of play. */ public void play() { printWelcome(); updateOutput(); // Enter the main command loop. Here we repeatedly read commands and // execute them until the game is over. boolean finished = false; while (! finished) { String inputLine = io.getInput(); finished = processCommand(inputLine); } System.out.println("Now get to work on your own version!"); } /** * Print out the opening message for the player. */ private void printWelcome() { io.clear(); //can be delete io.showOutput(""); io.showOutput("Hiya! Welcome to my world..."); io.showOutput(""); io.showOutput("If you hurry up, you could get through my whole world before your parents get home!"); io.showOutput("and even get some candy along the way!!!"); io.showOutput(""); io.showOutput("..."); io.showOutput(""); io.showOutput(""); io.showOutput("..."); io.showOutput(""); io.showOutput(""); io.showOutput("..."); io.showOutput(""); io.showOutput(""); io.showOutput("..."); io.showOutput(""); io.showOutput(""); } /** * 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(String inputLine) { boolean wantToQuit = false; String words[], word1, word2; word1 = "no"; word2 = null; // corrected word2 = "command"; words = inputLine.split(" "); if (words.length > 0) word1 = words[0]; if (words.length > 1) word2 = words[1]; if (word1.equals("help")) doHelp(); else if (word1.equals("go")) doGo(word2); else if (word1.equals("get")) doGet(word2); else if (word1.equals("drop")) doDrop(word2); else if (word1.equals("examine")) doExamine(word2); else if (word1.equals("show")) doShow(word2); else if (word1.equals("quit")) wantToQuit = doQuit(); // add more commands else io.showOutput("Um yeah... I don't know how to do that!"); return wantToQuit; } // implementations of user commands: /** * Print out some help information. * Here we print a list of the recognized commands. */ private void doHelp() { io.showOutput("You can use the commands: get, drop, examine, go, help, and quit"); } /** * "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 doQuit() { io.showOutput("Thanks for playing Spork, a combination of spam and pork!"); return true; // signal that we want to quit } /** * Move into the room in the given direction and update the output */ private void doGo(String direction) { if(direction == null) { // if there is no second word, we don't know where to go... System.out.println("Go where?"); return; } // Try to leave current room. Room nextRoom = currentRoom.getExit(direction); if (nextRoom == null) io.showOutput("I can't go that way."); else { String items = player.getItemString(); for(int i = 0; i < items.length() - 10; i++) { if (currentRoom == volleyballCourt && !(items.substring(i, i+10).equalsIgnoreCase("volleyball"))) return; } for(int i = 0; i < items.length() - 13; i++) { if (currentRoom == tennisCourt && !(items.substring(i, i+13).equalsIgnoreCase("tennis racket"))) return; } currentRoom = nextRoom; //io.clear(); io.showOutput(""); io.showOutput("--------------------------------------------------------------"); io.showOutput(""); updateOutput(); } } /** * Pick up an item */ private void doGet(String item) { if(item == null) { // if there is no second word, we don't know what to get... System.out.println("Get what?"); return; } // Try to leave current room. boolean itemFound = currentRoom.containsItem(item); if (!itemFound) io.showOutput("I can't find an item."); else { Item hand = currentRoom.getItem(item); player.setItem(item, hand); currentRoom.removeItem(item); //io.clear(); io.showOutput(""); io.showOutput("--------------------------------------------------------------"); io.showOutput(""); updateOutput(); } } private void doDrop(String item) { if(item == null) { // if there is no second word, we don't know what to get... System.out.println("Drop what?"); return; } // Try to leave current room. boolean itemFound = player.hasItem(item); if (!itemFound) io.showOutput("You can't drop the " + item + " if you don't have it!"); else if (currentRoom == darkCave) { io.showOutput ("Can't you read the sign? "); io.showOutput(""); io.showOutput("Sign: Don't drop things on the sidewalk"); io.showOutput(""); } else { Item hand = player.getItem(item); currentRoom.setItem(item, hand); player.removeItem(item); //io.clear(); io.showOutput(""); io.showOutput("--------------------------------------------------------------"); io.showOutput(""); updateOutput(); } } private void doExamine(String item) { if(item == null) { // if there is no second word, we don't know what to get... System.out.println("Drop what?"); return; } // Try to leave current room. if (player.hasItem(item)) { io.showOutput(player.getItem(item).getDescription()); } else if (currentRoom.hasItem(item)) { io.showOutput("You need to pick up the " + item + " before you can examine it."); } else { io.showOutput("I can't find an item."); } } /** * Will try to make program display room contents */ private void doShow(String command) { //System.out.println(command); if(command == null) { // if there is no second word, we don't know where to go... System.out.println("Show what?"); return; }else { if(command.equals("exit")) //corrected if(command == ("exit")); { io.showOutput("Exits: " + currentRoom.getExitString()); }else { io.showOutput("I don't know that instruction"); } } } /** * Update the display with current information */ private void updateOutput() { if(currentRoom == darkCave && currentRoom.getItemString() != "flashlight") io.showOutput("Oh look, its dark...\n\n\nhmmmmmmm\n\nmaybe we should try to be able to SEE"); else { io.showOutput("You are in " + currentRoom.getKey()); io.showOutput(currentRoom.getDescription()); if(currentRoom.getItemString() != "") io.showOutput("There is a" + currentRoom.getItemString() + " in the room!"); io.showOutput("Exits: " + currentRoom.getExitString()); io.showOutput("Characters: " + currentRoom.getNPCName()); } } } package Spork.Spork; import java.io.BufferedReader; import java.io.InputStreamReader; import java.util.StringTokenizer; /* * This class is based upon Zuul by Michael Kolling and David J. Barnes * Returns a line of input as a String * * @author Andrew Cronk * @version 08.01.2005 */ public class Console { private String inputLine; // holds the user input /** * Constructor just sets inputLine to "" */ public Console() { inputLine = ""; } /** * Get input from keyboard, making sure it is all lower case */ public String getInput() { System.out.print("> "); // print prompt BufferedReader reader = new BufferedReader(new InputStreamReader(System.in)); try { inputLine = reader.readLine(); } catch(java.io.IOException exc) { System.out.println ("There was an error during reading: " + exc.getMessage()); } return inputLine.toLowerCase(); } /** * Clear the console window */ public void clear() { System.out.print('\f'); } /** * Show the output string */ public void showOutput(String s) { System.out.println(s); } } package Spork.Spork; /** * Write a description of class Item here. * * @author (your name) * @version (a version number or a date) */ public class Item { // instance variables - replace the example below with your own private String key; private String description; /** * Constructor for objects of class Item */ public Item() { // initialise instance variables key = "unsetItem"; description = "a generic item"; } /** * Constructor for objects of class Item */ public Item(String item) { // initialise instance variables key = item; description = "a generic item"; } /** * A setter method for key variable */ public void setKey(String keyName) { key = keyName; } /** * A setter method for description variable */ public void setDescription(String keyDescription) { description = keyDescription; } /** * A getter method for key variable */ public String getKey() { return key; } /** * A getter method for description variable */ public String getDescription() { return description; } } package Spork.Spork; //import java.util.HashMap; import java.util.*; /** * Write a description of class Character here. * * @author (your name) * @version (a version number or a date) */ public class Character { // instance variables - replace the example below with your own private String key; private String description; private HashMap items; private int stat; /** * Constructor for objects of class Character */ public Character() { // initialise instance variables key = "unsetItem"; description = "a generic item"; items = new HashMap(); stat = 0; } /** * Constructor for objects of class Character */ public Character(String npc) { // initialise instance variables key = npc; description = "a generic item"; items = new HashMap(); stat = 0; } /** * A setter method for key variable */ public void setKey(String keyName) { key = keyName; } /** * A setter method for description variable */ public void setDescription(String keyDescription) { description = keyDescription; } /** * A setter method for stat variable */ public void setStat(int bob) { stat = bob; } /** * A getter method for key variable */ public String getKey() { return key; } /** * A getter method for description variable */ public String getDescription() { return description; } /** * A getter method for stat variable */ public int getStat() { return stat; } /** * A setter method for item variable */ public void setItem(String name, Item object) { items.put(name, object); } /** * A getter method for item variable */ public Item getItem(String name) { return (Item) items.get(name); } /** * A remove method for item variable */ public void removeItem(String name) { items.remove(name); } /** * A getter method for multiple items variables */ public String getItemString() { String returnString = ""; Set keys = items.keySet();//Why is keys with an s used for (Iterator iter = keys.iterator(); iter.hasNext(); ) returnString += " " + iter.next(); return returnString; } /** * A getter method to see if character has item (true/false) */ public boolean hasItem(String key) { return items.containsKey (key); } /** * GetCharacterString */ public String getCharacterString() { // Capitalize the first letter char[] firstLetter; char[] lastLetters; firstLetter = new char[1]; lastLetters = new char[key.length() -1]; key.getChars(0,1,firstLetter,0); key.getChars(1,key.length(),lastLetters,0); String first = new String(firstLetter); String last = new String(lastLetters); return first.toUpperCase() + last; } /** * Drop all items * * @param Room the Room to drop all Items in (usually currentRoom) */ public void dropAllItems(Room room) { Set keys = items.keySet(); String keyName; for (Iterator iter = keys.iterator(); iter.hasNext(); ) { keyName = (String) iter.next(); room.setItem(keyName, (Item) items.get(keyName)); items.remove(keyName); } } } package Spork.Spork; //import java.util.HashMap; import java.util.*; /** * Write a description of class Room here. * * @author (your name) * @version (a version number or a date) */ public class Room { // instance variables - replace the example below with your own private HashMap items; private HashMap exits; //private HashMap npc; private Character npc; //private puzzle puzzle; private String key; private String description; /** * Constructor for objects of class Room */ public Room() { // initialise instance variables items = new HashMap(); exits = new HashMap(); //npc = new HashMap(); npc = null; key = "unsetItem"; description = "a generic item"; } public Room(String room) { // initialise instance variables items = new HashMap(); exits = new HashMap(); //npc = new HashMap(); npc = null; key = room; description = "a generic item"; } /** * A setter method for setNPC variable */ public void setNPC(Character buford) { npc = buford; } /** * A getter method for NPC variable */ public Character getNPC () { return npc; } /** * A setter method for NPC */ public void removeNPC (Character buford) { npc = null; } /** * A setter method for Exits */ /** * A setter method for key variable */ public void setKey(String keyName) { key = keyName; } /** * A setter method for description variable */ public void setDescription(String keyDescription) { description = keyDescription; } /** * A getter method for key variable */ public String getKey() { return key; } /** * A getter method for description variable */ public String getDescription() { return description; } /** * A setter method for item variable */ public void setItem(String name, Item object) { items.put(name, object); } /** * A getter method for item variable */ public Item getItem(String name) //This was Item as the type { return (Item) items.get(name); } /** * A remove method for item variable */ public void removeItem(String name) { items.remove(name); } /** * A getter method for multiple items variables */ public String getItemString() { String returnString = ""; Set keys = items.keySet();//Why is keys with an s used for (Iterator iter = keys.iterator(); iter.hasNext(); ) returnString += " " + iter.next(); return returnString; } /** * A getter method for multiple items variables */ public String getExitString() { String returnString = ""; Set keys = exits.keySet();//Why is keys with an s used for (Iterator iter = keys.iterator(); iter.hasNext(); ) returnString += " " + iter.next(); return returnString; } /** * A getter method to see if room contains item (true/false) */ public boolean containsItem(String key) { return items.containsKey (key); } /** * A setter method for exit variable */ public void setExit(String name, Room object) { exits.put(name, object); } /** * A getter method for item variable */ public Room getExit(String name) //was Item { return (Room) exits.get(name); } /** * A remove method for item variable */ public void removeExit(String name) { exits.remove(name); } /** * Determine if room contain Item */ public boolean hasItem(String key) { return items.containsKey (key); } /** * Determine if room contain None Player Character */ public boolean hasNPC(String npc) { return (npc != null); //return npc.containsKey (key); } /** * A getter method for multiple npc variables * *public String getNPCString() *{ * String returnString = ""; * Set keys = npc.keySet();//Why is keys with an s used * for (Iterator iter = keys.iterator(); iter.hasNext(); ) * returnString += " " + iter.next(); * return returnString; *} */ /** * Get NPC Name */ public String getNPCName() { if (npc != null) { return npc.getCharacterString(); } else { return ""; } } }