//import utils.SavitchIn; class House { public static int location; /////////////////// public static void main(String[] agrs) { String instruction = "nothing"; location = 0; int theTime = 1730; char verb, noun; String rmLights[] = {"off", "off", "off", "off"};//Sets inital state of lights String names[] = {"book", "comb", "glass", "key"}; int status[] = {0, 1, 2, 3}; //Sets up items and initial locations welcome();//Prints out a welcome message and brief instructions while(instruction.charAt(0) != 'q'){ if((theTime > 1744)&&(theTime < 1800)){ System.out.println("\n---The sun has gone down---"); } if((theTime > 1800)&&(rmLights[location]=="off")){ System.out.println("---It's dark in here---"); } if((theTime > 1900)&&(theTime < 1910)){ System.out.println("---You're sleepy---"); } if(theTime >= 1910){ System.out.println("---You have falen asleep, good night!"); System.exit(0); } System.out.print("\nWhat would you like to do?"); switch(location){ case 0: System.out.print("[Bed Room - "+theTime+" ]: "); break; case 1: System.out.print("[Bathroom - "+theTime+" ]: "); break; case 2: System.out.print("[Kitchen - "+theTime+" ]: "); break; case 3: System.out.print("[Living Room - "+theTime+" ]: "); break; } theTime++; if((theTime-60)%100 == 0){ theTime = ((theTime + 100)-60); } instruction = SavitchIn.readLine(); instruction = instruction.toLowerCase();//Makes all input lowecase /* Section seperates the verb from the noun for sorting */ verb = instruction.charAt(0); if(instruction.indexOf(" ", 0)>0){ noun = instruction.charAt(instruction.indexOf(" ", 0)+1); }else{ noun = 'x'; } /* This section takes the entered commands and determines which methods to activate and what to send to them */ if(noun == 'x'){ switch(verb){ case '!': commands(); break; case 'q': System.out.print("Ok, quitting...");break; case 'm': map(location); break; case 'h': help(); break; case 'i': System.out.println("You are holding: "); inv(names, status, 5); break; case 'l': look(location, rmLights); System.out.println("You see: "); inv(names, status, location); break; case 'n': location = move(location, verb); break; case 's': location = move(location, verb); break; case 'w': location = move(location, verb); break; case 'e': location = move(location, verb); break; case 'c': System.out.println("The time is: "+theTime); break; default: System.out.println("Don't know \""+instruction+"\", try help");break; }//end switch }else{ if(instruction.indexOf("turnon") > -1){ switch(noun){ case 'l': rmLights[location] = turnOn(); break; default: System.out.println("huh?"); break; } }//end if if(instruction.indexOf("turnoff") > -1){ switch(noun){ case 'l': rmLights[location] = turnOff(); break; default: System.out.println("huh?"); break; } }//end if if(instruction.indexOf("get") > -1){ switch(noun){ case 'b': if(true == isHolding("book", names, status, location)){ status[0] = 5; }else{ System.out.println("It's not here"); } break; case 'c': if(true == isHolding("comb", names, status, location)){ status[1] = 5; }else{ System.out.println("It's not here"); } break; case 'g': if(true == isHolding("glass", names, status, location)){ status[2] = 5; }else{ System.out.println("It's not here"); } break; case 'k': if(true == isHolding("key", names, status, location)){ status[3] = 5; }else{ System.out.println("It's not here"); } break; default: System.out.println("Huh?"); } }//end if if(instruction.indexOf("drop") > -1){ switch(noun){ case 'b': if(true == isHolding("book", names, status, 5)){ status[0] = location; }else{ System.out.println("You're not holding it"); } break; case 'c': if(true == isHolding("comb", names, status, 5)){ status[1] = location; }else{ System.out.println("You're not holding it"); } break; case 'g': if(true == isHolding("glass", names, status, 5)){ status[2] = location; }else{ System.out.println("You're not holding it"); } break; case 'k': if(true == isHolding("key", names, status, 5)){ status[3] = location; }else{ System.out.println("You're not holding it"); } break; default: System.out.println("Huh?"); }//end switch }//end if } }//end while - exits program }//End Main /////////////////////////////////////////// //Methods /////////////////////////////////////////// ////////////// //Move method ////////////// /* Takes the current location and direction input and determines if the move is possible, then returns the new location if the move is sucessful */ static int move(int location, char direction){ switch(location){ case 0: switch(direction){ case 's': System.out.println("Ok");location = 2;break; case 'e': System.out.println("Ok");location = 1;break; default: System.out.println("Can't go that way, look at the map.");break; };break; case 1: switch(direction){ case 's': System.out.println("Ok");location = 3;break; case 'w': System.out.println("Ok");location = 0;break; default: System.out.println("Can't go that way, look at the map.");break; };break; case 2: switch(direction){ case 'n': System.out.println("Ok");location = 0;break; case 'e': System.out.println("Ok");location = 3;break; default: System.out.println("Can't go that way, look at the map.");break; };break; case 3: switch(direction){ case 'n': System.out.println("Ok");location = 1;break; case 'w': System.out.println("Ok");location = 2;break; default: System.out.println("Can't go that way, look at the map.");break; };break; default: System.out.println("Can't go that way!");break; }//end main switch return(location); } //////////// //Map Method //////////// static void map(int location){ System.out.println("Map of house\n"); System.out.println(" ______________N_______________ "); System.out.println(" |Bed Room | Bathroom|"); System.out.println(" | | |"); if(location == 0){ System.out.println(" | You're here | |"); } if(location == 1){ System.out.println(" | | You're here |"); } System.out.println(" | | |"); System.out.println(" | | |"); System.out.println(" | | |"); System.out.println(" | | |"); System.out.println(" | |"); System.out.println(" |________ ________|"); System.out.println("W| | E"); System.out.println(" | |"); System.out.println(" | |"); System.out.println(" | | |"); System.out.println(" | | |"); System.out.println(" | | |"); if(location == 2){ System.out.println(" | You're here | |"); } if(location == 3){ System.out.println(" | | You're here |"); } System.out.println(" | | |"); System.out.println(" | | |"); System.out.println(" | | |"); System.out.println(" | Kitchen | Living Room |"); System.out.println(" |______________|_______________|"); System.out.println(" S"); System.out.println(); }//End map method ///////////// //Help method ///////////// /* Prints out help information for using the program */ static void help(){ System.out.println(); System.out.println("--Help-----------------------------------"); System.out.println("There are several actions you may take:\n"); System.out.println("*Indicate a direciton to move about the house"); System.out.println("use north, east, west or south"); System.out.println("*Manipulate objects by saying \'get\' or \'drop\' \"object\""); System.out.println("*map - Shows a map of the house"); System.out.println("*look - Describes the room you're in"); System.out.println("*quit - exits program"); System.out.println("*! - lists all the commands"); System.out.println(); System.out.println("This is what your prompt looks like: "); System.out.println(" What would you like to do?[Kitchen - 1730]: "); System.out.println(" the name in the brackets ^ tells you where you are"); } ///////////////// //Commands method ///////////////// /* Prints out a list of commands and how to use them */ static void commands(){ System.out.println(); System.out.println("--Commands---------------------------------"); System.out.println("help - brings up help"); System.out.println("! - lists commands"); System.out.println("inventory - tells you what you have"); System.out.println("drop - drops an object, ex: \"drop key\""); System.out.println("get - picks up an object, ex: \"get book\""); System.out.println("look - looks around the room"); System.out.println("map - show a map of the house"); System.out.println("turnon lights - turns on lights in a room"); System.out.println("turnoff lights - turns off lights in a room"); System.out.println("quit - exits program"); System.out.println("north - goes north"); System.out.println("west - goes west"); System.out.println("east - goes east"); System.out.println("south - goes south"); System.out.println("clock - tells the time"); } ////////////// //Look method ////////////// /* Receives location and rmLights, rmLights is sent to a sub-method depending on what location is sent */ static void look(int location, String rmLights[]){ switch(location){ case 0: System.out.print("\nYou are in the Bed Room, ");bdrm(rmLights); break; case 1: System.out.print("\nYou are in the Bathroom, ");bathrm(rmLights); break; case 2: System.out.print("\nYou are in the Kitchen, ");kitch(rmLights); break; case 3: System.out.print("\nYou are in the Living Room, ");lvgrm(rmLights); break; } } /////////////////// //Look sub methods /////////////////// /* All these methods are basically the same, they take rmLights and print out the value */ /////////////////// //Bedroom state /////////////////// static void bdrm(String rmLights[]){ System.out.print(" the lights are "+rmLights[location]+"\n"); } /////////////////// //Bathroom state /////////////////// static void bathrm(String rmLights[]){ System.out.print(" the lights are "+rmLights[location]+"\n"); } /////////////////// //Kitchen state /////////////////// static void kitch(String rmLights[]){ System.out.print(" the lights are "+rmLights[location]+"\n"); } /////////////////// //Livingroom state /////////////////// static void lvgrm(String rmLights[]){ System.out.print(" the lights are "+rmLights[location]+"\n"); } ///////////////////////////// //Manipulate objects methods ///////////////////////////// /* These two methods are similar, one returns "on" the other "off" */ ///////////////// //Turn on method ///////////////// static String turnOn(){ System.out.println("Ok"); return("on"); } ///////////////// //Turn off method ///////////////// static String turnOff(){ System.out.println("Ok"); return("off"); } ///////////////// /* These two methods alow objects to be pickup and dropped by checking to see where they are */ //////////////// //Inventory //////////////// /* Goes through the list of objects and determines which ones the user is holding */ static void inv(String names[], int status[], int location){ int numOfItems = 0; for(int i = 0; i < names.length; i++){ if(status[i] == location){ System.out.print("a "+names[i]+"\n"); numOfItems++; } } if(numOfItems > 0){ System.out.println("Total items: "+numOfItems); }else{ System.out.println("Nothing special"); } } /////////////// //Is holding /////////////// /* Sees if an object is in a particular location, either in a room or being carried by the user. A for loop goes through the two object arrays and finds the item name passed to the method. When it does, it matches the object location with the user or room locaiton. It then reports back a boolean. */ static boolean isHolding(String item, String names[], int status[], int location){ boolean retval = false; for(int i = 0; i < names.length; i++){ if((item == names[i])&&(status[i] == location)){ System.out.println("Ok"); retval = true; } } return(retval); } ///////////////// //Welcome method //////////////// static void welcome(){ System.out.println("\nWelcome to the house interface."); System.out.println("There are four rooms to move through and\nfour objects to pick up and move to different rooms."); System.out.println("The time starts at 1730(military time), \nat 1745 the sun goes down and by 1800 the house is dark."); System.out.println("You may turn the lights on to avoid being in the dark."); System.out.println("Because it is so boring, at 1900, you get tired and fall asleep"); System.out.println("Other than that, the game is pretty silly."); System.out.println("Type \'help\' for help a or \'!\' for a list of commands."); } }//End class