package mug12; import java.io.BufferedReader; import java.io.InputStreamReader; import rebelsky.io.Pen; import java.util.Comparator; /** * Something that reads the user's input and parses it (figures out what the input means), * accepts reasonable synonyms and prints error messages if it can't figure out the meaning. * * @author Angeline Namai * @author Kyle Gonnerman * @author Michael Claveria * @version 1.0 of November 2004 */ public class ReadAndParse { // +--------+-------------------------------------------------- // | Fields | // +--------+ String verb; String object; String modifier; String username; String password; String[] arrayOfValidActions = new String[] {"drink", "eat", "examine", "help", "look", "quit", "sleep"}; // +--------------+-------------------------------------------- // | Constructors | // +--------------+ // +----------------+------------------------------------------ // | Public Methods | // +----------------+ // World needs to implement getLocation, listContents, printStuffInRoom, describeThing. // Our implementations are just stubs for testing. public String getLocation(String username) { return "Haines"; } // getLocation(String) public String[] listContents(String room) { return new String[] {"computers", "beer", "beds", "pizza"}; } // listContents(String) public void printStuffInRoom(String roomname) throws Exception { Pen pen = new Pen(); // World needs to provide listContents(String) Object[] stuffInRoom = listContents(roomname); for (int i = 0; i < stuffInRoom.length; i++) { pen.println(stuffInRoom[i]); } } // printStuffInRoom(String) public String describeThing(String thing) { return "Damn ugly!"; } public void help() { Pen pen = new Pen(); for (int i = 0; i < arrayOfValidActions.length; i++) { pen.println(arrayOfValidActions[i]); } } // help() public void createAccount() throws Exception { Pen pen = new Pen(); BufferedReader keyboard = new BufferedReader(new InputStreamReader(System.in)); pen.print("Create a username: "); pen.flush(); String username = keyboard.readLine(); pen.print("Create a password of more than six characters long: "); pen.flush(); String password = keyboard.readLine(); pen.print("Verify your password: "); pen.flush(); String verifiedpass = keyboard.readLine(); if (password.equals(verifiedpass)) { readStuff(); // newAccount(username, password) } else { pen.print("Your password and verified password do not match. Would you like to try again? [Y/N] "); pen.flush(); String option = keyboard.readLine(); if ((option.equals("Y")) || (option.equals("y"))) { createAccount(); // newAccount(username, password) } else pen.println("Goodbye"); } } // createAccount() public void validateUser(String username, String password) throws Exception { Pen pen = new Pen(); BufferedReader keyboard = new BufferedReader(new InputStreamReader(System.in)); if (username.equals("angeline") && password.equals("grinnell")) { ReadAndParse test = new ReadAndParse(); test.readStuff(); } else { pen.println("Your username and password were invalid. Please try again."); pen.print("Please enter your username: "); pen.flush(); String username2 = keyboard.readLine(); pen.print("Please enter your password: "); pen.flush(); String password2 = keyboard.readLine(); validateUser(username2, password2); } } // validateUser(String, String) public void readStuff() throws Exception { Pen pen = new Pen(); BufferedReader keyboard = new BufferedReader(new InputStreamReader(System.in)); pen.print("What's your quest? "); pen.flush(); String input = keyboard.readLine(); String[] array1 = input.split(" "); this.verb = array1[0]; if (this.verb.equals("quit")) pen.println("Goodbye"); else { if (array1.length >= 2) { this.object = array1[1]; } if (array1.length == 3) { this.modifier = array1[2]; } if (this.verb.equals("look")) { String room = getLocation(this.username); printStuffInRoom(room); } if (this.verb.equals("examine")) { pen.println(describeThing(this.object)); } if (this.verb.equals("help")) { help(); } // check to see if the verb ie. array1[0], is in the array of valid actions int lb = 0; // The lower-bound of the portion of interest int ub = arrayOfValidActions.length - 1; // The upper-bound int counter = 0; while (lb <= ub) { int mid = (lb + ub)/2; int c = this.verb.compareTo(arrayOfValidActions[mid]); if (c == 0) // Found it! { StubInteract p = new StubInteract(); pen.println(p.interact(this.username, this.verb, this.object)); lb = ub + 1; counter++; } else if (c < 0) // Middle element too big! ub = mid-1; else // Middle element too small! lb = mid+1; } // while // Whoops, doesn't seem to be there if (counter == 0) { pen.println(this.verb + " is an invalid action! Please enter a valid action."); } readStuff(); } } // readStuff() public void printMessage(String printMe) throws Exception { Pen pen = new Pen(); pen.println(printMe); readStuff(); } // printMessage(String) } // class ReadAndParse