// Text Adventure Game // WTF, CS206: Data Structures // Bryn Mawr College, Spring 2016 // D.S. Blank import java.io.*; public class Game { LinkedList places; Stack> backpack; String current_place_name; public Game(String fileName) { places = new LinkedList(); backpack = new Stack>(); current_place_name = null; try { FileReader fileReader = new FileReader(fileName); BufferedReader bufferedReader = new BufferedReader(fileReader); String line; while((line = bufferedReader.readLine()) != null) { System.out.println("line: " + line); String[] parts = line.split("::"); if (parts[0].equals("place")) { places.append(new Place(parts[1], parts[2])); if (current_place_name == null) { current_place_name = parts[1]; } } else if (parts[0].equals("object")) { Thing thing = new Thing(parts[1], parts[2]); // first, find the place that is mentioned by parts[3] String place_name = parts[3]; Node current = places.head; while (current != null) { if (current.data.name.equals(place_name)) { System.out.println("Put the " + parts[1] + " into " + place_name); break; } current = current.next; } if (current == null) { System.out.println("Can't find place: " + place_name); return; } else { current.data.things.append(thing); } } else if (parts[0].equals("command")) { String[] needthings = parts[3].split(","); LinkedList lls = new LinkedList(); for (String item: needthings) { lls.append(item); } Action action = new Action(parts[1], parts[2], lls); // Add code here! // Put the action in the associated room } else { System.out.println("Ignored: " + line); } } bufferedReader.close(); } catch(FileNotFoundException ex) { System.out.println("Unable to open file '" + fileName + "'"); } catch(IOException ex) { System.out.println("Error reading file '" + fileName + "'"); } } public String getInput(String prompt) { System.out.print(prompt); String input = null; try { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); input = br.readLine(); } catch (IOException io) { io.printStackTrace(); } return input; } public void play() { // get description of current_place_name while (true) { Node current = places.head; while (current != null) { if (current.data.name == current_place_name) { System.out.println(current.data.description); break; } current = current.next; } String action = getInput("> "); // Add code here! // look for action in the actions of this room // change current_place_name, but only if you have the necessary objects // Special cases: "pick up OBJECT" // "drop" } } public static void main(String[] args) { Game game = new Game(args[0]); game.play(); } }