package mug13; 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 CS152 2004F * @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", "say", "sleep"}; Pen pen; BufferedReader keyboard; World world; AccountManager am; // +--------------+-------------------------------------------- // | Constructors | // +--------------+ /** * Create a new read-and-parser that uses a particular world. */ public ReadAndParse(World _world, AccountManager _am) { this.world = _world; this.am = _am; this.pen = new Pen(); this.keyboard = new BufferedReader(new InputStreamReader(System.in)); } // ReadAndParse(World) // +----------------+------------------------------------------ // | Public Methods | // +----------------+ public void printStuffInRoom(String roomname) throws Exception { // World needs to provide listContents(String) Object[] stuffInRoom = world.listContents(roomname); for (int i = 0; i < stuffInRoom.length; i++) { pen.println(stuffInRoom[i]); } } // printStuffInRoom(String) public void help() { for (int i = 0; i < arrayOfValidActions.length; i++) { pen.println(arrayOfValidActions[i]); } } // help() public void createAccount() throws Exception { 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() throws Exception { pen.print("Please enter your username: "); pen.flush(); username = keyboard.readLine(); pen.print("Please enter your password: "); pen.flush(); password = keyboard.readLine(); while (!am.checkAccount(username, password)) { pen.println("Your username and password were invalid. Please try again."); pen.print("Please enter your username: "); pen.flush(); username = keyboard.readLine(); pen.print("Please enter your password: "); pen.flush(); password = keyboard.readLine(); } } // validateUser(String, String) public void readStuff() throws Exception { pen.print("> Play on. Enter a verb and an object please: "); 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]; } // Note that we need a default object if an object is not provided. // Things like sleep, run, smile etc. are intransitive yet Action expects an object. // so the program throws an exception whenever intransitive verbs are used. /* if (array1.length == 1){ this.object = "none"; } */ if (array1.length == 3) { this.modifier = array1[2]; } if (this.verb.equals("look")) { String room = world.getLocation(this.username); printStuffInRoom(room); } if (this.verb.equals("examine")) { pen.println(world.describe(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! { try { pen.println(world.perform(new Action(this.username, this.verb, this.object))); } catch (Exception e) { pen.println("This program sucks so much that it can't understand your command because: "); pen.println(e.toString()); } 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) public void run() throws Exception { validateUser(); readStuff(); } // run() } // class ReadAndParse