--- Game --- --- --- --- --- --- --- --- --- --- --- import java.util.Scanner; public class Game { // // Public // // Globals public static final boolean DEBUGGING = false; // Debugging flag. public static final int MAX_LOCALES = 2; // Total number of rooms/locations we have in the game. public static int currentLocale = 0; // Player starts in locale 0. public static String command; // What the player types as he or she plays the game. public static boolean stillPlaying = true; // Controls the game loop. public static Locale[] locations; // An uninitialized array of type Locale. See init() for initialization. public static int[][] nav; // An uninitialized array of type int int. public static int moves = 0; // Counter of the player's moves. public static int score = 0; // Tracker of the player's score. public static void main(String[] args) { if (DEBUGGING) { // Display the command line args. System.out.println("Starting with args:"); for (int i = 0; i < args.length; i++) { System.out.println(i + ":" + args[i]); } } // Set starting locale, if it was provided as a command line parameter. if (args.length > 0) { try { int startLocation = Integer.parseInt(args[0]); // Check that the passed-in value for startLocation is within the range of actual locations. if ( startLocation >= 0 && startLocation <= MAX_LOCALES) { currentLocale = startLocation; } else { System.out.println("WARNING: passed-in starting location (" + args[0] + ") is out of range."); } } catch(NumberFormatException ex) { System.out.println("WARNING: Invalid command line arg: " + args[0]); if (DEBUGGING) { System.out.println(ex.toString()); } } } // Get the game started. init(); updateDisplay(); // Game Loop while (stillPlaying) { getCommand(); navigate(); updateDisplay(); } // We're done. Thank the player and exit. System.out.println("Thank you for playing."); } // // Private // private static void init() { // Initialize any uninitialized globals. command = new String(); stillPlaying = true; // TODO: Do we need this? // Set up the location instances of the Locale class. Locale loc0 = new Locale(0); loc0.setName("The Lab"); loc0.setDesc("You are in a shiny new lab."); Locale loc1 = new Locale(1); loc1.setName("Dungeon"); loc1.setDesc("This is a dark and smelly place."); Space loc2 = new Space(2); loc2.setName("TARDIS"); loc2.setDesc("It seems larger on the inside."); loc2.setNearestPlanet("Altair 6"); // Set up the location array. locations = new Locale[3]; locations[2] = loc2; // "TARDIS"; // ^ locations[0] = loc0; // "The Lab"; // N locations[1] = loc1; // "Dungeon"; // | if (DEBUGGING) { System.out.println("All game locations:"); for (int i = 0; i < locations.length; ++i) { System.out.println(i + ":" + locations[i].toString()); } } // Set up the navigation matrix. nav = new int[][] { /* N S E W */ /* 0 1 2 3 */ /* nav[0] for loc 0 */ { 2, 1, -1, -1 }, /* nav[1] for loc 1 */ { 0, -1, -1, -1 }, /* nav[2] for loc 2 */ { -1, 0, -1, -1 } }; createMagicItems(); } private static void updateDisplay() { System.out.println(locations[currentLocale].getText()); } private static void getCommand() { System.out.print("[" + moves + " moves, score " + score + "] "); Scanner inputReader = new Scanner(System.in); command = inputReader.nextLine(); // command is global. } private static void navigate() { final int INVALID = -1; int dir = INVALID; // This will get set to a value > 0 if a direction command was entered. if ( command.equalsIgnoreCase("north") || command.equalsIgnoreCase("n") ) { dir = 0; } else if ( command.equalsIgnoreCase("south") || command.equalsIgnoreCase("s") ) { dir = 1; } else if ( command.equalsIgnoreCase("east") || command.equalsIgnoreCase("e") ) { dir = 2; } else if ( command.equalsIgnoreCase("west") || command.equalsIgnoreCase("w") ) { dir = 3; } else if ( command.equalsIgnoreCase("quit") || command.equalsIgnoreCase("q")) { quit(); } else if ( command.equalsIgnoreCase("help") || command.equalsIgnoreCase("h")) { help(); }; if (dir > -1) { // This means a dir was set. int newLocation = nav[currentLocale][dir]; if (newLocation == INVALID) { System.out.println("You cannot go that way."); } else { currentLocale = newLocation; moves = moves + 1; // TODO: Deal with hasVisited and the score here. } } } private static void help() { System.out.println("The commands are as follows:"); System.out.println(" n/north"); System.out.println(" s/south"); System.out.println(" q/quit"); } private static void quit() { stillPlaying = false; } private static void createMagicItems() { // Create the list manager for our magic items. List0 magicItems = new List0(); magicItems.setName("Magic Items"); magicItems.setDesc("These are the magic items."); magicItems.setHead(null); // Create some magic items and put them in the list. ListItem i1 = new ListItem(); i1.setName("+2 ring"); ListItem i2 = new ListItem(); i2.setName("vorpal sword"); ListItem i3 = new ListItem(); i3.setName("cursed cloak of DOOM"); // Link it all up. magicItems.setHead(i1); i1.setNext(i2); i2.setNext(i3); i3.setNext(null); System.out.println(magicItems.toString()); } } --- Game --- --- --- --- --- --- --- --- --- --- --- --- List0 --- --- --- --- --- --- --- --- --- --- --- public class List0 { // // Public // // Constructor public List0() { } // Getters and Setters public String getName() { return name; } public void setName(String name) { this.name = name; } public String getDesc() { return desc; } public void setDesc(String desc) { this.desc = desc; } public ListItem getHead() { return head; } public void setHead(ListItem head) { this.head = head; } // Other methods @Override public String toString() { String retVal = new String(); retVal = super.toString() + " name=" + this.name + " desc=" + this.desc + "\n"; ListItem currentItem = this.head; while (currentItem != null) { retVal = retVal + " " + currentItem.toString() + "\n"; currentItem = currentItem.getNext(); } return retVal; } // // Private // private String name; private String desc; private ListItem head; } --- List0 --- --- --- --- --- --- --- --- --- --- --- --- ListItem --- --- --- --- --- --- --- --- --- --- --- public class ListItem { // // Public // // Constructor public ListItem(){ } // Getters and Setters public String getName() { return name; } public void setName(String name) { this.name = name; } public String getDesc() { return desc; } public void setDesc(String desc) { this.desc = desc; } public ListItem getNext() { return next; } public void setNext(ListItem next) { this.next = next; } // Other methods @Override public String toString() { return super.toString() + " name=" + this.name + " desc=" + this.desc; } // // Private // private String name; private String desc; private ListItem next = null; } --- ListItem --- --- --- --- --- --- --- --- --- --- --- --- Locale --- --- --- --- --- --- --- --- --- --- --- public class Locale { // // Public // // Constructor public Locale(int id) { this.id = id; } // Getters and Setters public int getId() { return this.id; } public String getText() { return this.name + "\n" + this.desc; } public String getName() { return this.name; } public void setName(String value) { this.name = value; } public String getDesc() { return this.desc; } public void setDesc(String value) { this.desc = value; } public boolean getHasVisited() { return hasVisited; } public void setHasVisited(boolean hasVisited) { this.hasVisited = hasVisited; } // Other methods @Override public String toString(){ return "[Locale id=" + this.id + " name=" + this.name + " desc=" + this.desc + " hasVisited=" + this.hasVisited + "]"; } // // Private // private int id; private String name; private String desc; private boolean hasVisited = false; } --- Locale --- --- --- --- --- --- --- --- --- --- --- --- Space --- --- --- --- --- --- --- --- --- --- --- public class Space extends Locale { // Space IS-A Locale. // // Public // // Constructor public Space(int id){ super(id); } // Getters and Setters public String getNearestPlanet() { return nearestPlanet; } public void setNearestPlanet(String nearestPlanet) { this.nearestPlanet = nearestPlanet; } @Override public String toString() { return "Space..." + super.toString() + " nearestPlanet=" + this.nearestPlanet; } // // Private // private String nearestPlanet; } --- Space --- --- --- --- --- --- --- --- --- --- ---