/* * ZorkJ - By Alan G. Labouseur */ import java.util.Scanner; public class ZorkJcentralControlWithEnums { // Declare (global) data type for locations. private static enum Location { Backyard, House, Shrubs }; // Global Variables private static Location currentLocale = Location.Backyard; private static Scanner input = new Scanner(System.in); private static boolean stillPlaying = true; public static void main(String [] args) { intro(); gameLoop(); quitGame(); } private static void gameLoop() { while (stillPlaying) { switch (currentLocale) { case Backyard : localeBackyard(); break; case House : localeHouse(); break; case Shrubs : localeShrubs(); break; default: System.out.println("Error. Invalid locale:" + currentLocale + " This should never happen."); } } } private static void intro() { System.out.println("Welcome to ZorkJ."); System.out.println(" Written by Alan G. Labouseur"); System.out.println(" (c) 2009 - No Rights Reserved. Some rights obscured."); System.out.println(" Play at your own risk."); blankLine(); } private static void blankLine() { System.out.println(); } private static void localeBackyard() { // Display description. System.out.println("--Back yard--"); System.out.println("It's a lazy summer morning as you sit under a tree"); System.out.println("next to your \"owners\", Phineas and Ferb, wondering"); System.out.println("what they're going to do that day."); // Interact String command = new String(""); command = getCommand(); if (command.equalsIgnoreCase("e")) { currentLocale = Location.House; } else { if (command.equalsIgnoreCase("s")) { currentLocale = Location.Shrubs; } else { System.out.println("You can't go that way."); // Leave currentLocale unchanged. } } } private static void localeHouse() { // Display description. System.out.println("--House--"); System.out.println("(house)"); // Interact String command = new String(""); command = getCommand(); if (command.equalsIgnoreCase("s")) { currentLocale = Location.Shrubs; } else { System.out.println("You can't go that way."); // Leave currentLocale unchanged. } } private static void localeShrubs() { // Display description. System.out.println("--Shrubs--"); System.out.println("(shrubs)"); // Interact String command = new String(""); command = getCommand(); if (command.equalsIgnoreCase("e")) { currentLocale = Location.House; } else { System.out.println("You can't go that way."); // Leave currentLocale unchanged. } } private static String getCommand() { final String PROMPT = "-]"; System.out.print(PROMPT); // Prompt the user for a command. String command = new String(""); command = input.nextLine(); // Check to see if they want to quit ... if (command.equalsIgnoreCase("q")) { // ... which they do, so let's quit. quitGame(); } return command; } private static void quitGame() { // Close down the input Scanner. input.close(); // TODO: 1. Report Score // 2. Report number of turns // Thanks the player. System.out.println("Thank you for playing."); // Finally, just exit. System.exit(0); // TODO: implement (stillPlaying = false;) } }