/*********************************************************************
 * Program:  The Vacation Game
 * Author:   Mark Rich,  richm@cs.wisc.edu
 * Date:     3/23/2000
 ********************************************************************/
import javabook.*;

/**
 * The Vacation game is a wonderful way to amuse your friends on a 
 * long car trip or at a boring party.  You're going on a vacation
 * and you're allowed to bring certain things and not others.  The 
 * object of the game it to determine what it legal and what is not.
 * For this particular game, legal objects will have double letters
 * in their name, and all other objects are illegal.  "apple" will be
 * accpeted, as well as "penny", "mississippi", and "bookkeeper", while
 * "fruit", "nickle", and "river" will be illegal.  
 *
 * This program is an example of the use of Strings, using the
 * .length() and .charAt() methods.  It also shows the benefits 
 * of error checking on your code to combat evil users.
 */
class Vacation {
    
    public static void main(String[] args) {

	// Declare and Create javabook objects
	MainWindow mw = new MainWindow();
	InputBox in = new InputBox(mw);
	OutputBox out = new OutputBox(mw);
	mw.show();
	out.show();

	// Describe the game to the user
	out.printLine("We're going on vacation!  You tell me what you");
	out.printLine("would like to bring, and I'll tell you if you");
	out.printLine("can bring it along.  See if you can find a pattern");
	out.printLine("to my answers!");
	out.skipLine(1);
	out.printLine("If you wish to end the game, type STOP or just");
	out.printLine("hit enter again.");
	out.skipLine(1);

	// We need a sentinel for our loop, called playmore.
	boolean playmore = true;

	// Our outside loop, which plays the game until the user decides
	// to stop.
	do {

	    // Get the name of the object the user wants to bring.
	    // We shift it to UpperCase to ignore capitalization
	    String thing = in.getString("What would you like to bring?");
	    thing = thing.toUpperCase();

	    // Check for the stopping conditions to quit the loop
	    if (thing.equals("STOP") || thing.length() == 0) {
		playmore = false;
	    }

	    // Otherwise, we now need to detect for double letters
	    else {

		// we assume the word has no double letters and 
		// initialize prev to be the first character of the String
		boolean cool = false;
		char prev = thing.charAt(0);

		// now loop through the rest of the String, comparing
		// neighboring characters as we go.
		for (int i = 1; i < thing.length() && !cool; i++) {
		    char cur = thing.charAt(i);

		    // if we find a match between cur and prev,
		    // the word is cool, and kick ourselves out of the loop
		    if (cur == prev) {
			cool = true;
		    }

		    // otherwise move prev over one and try again
		    prev = cur;
		}

		// If the word is cool, say so, otherwise deny them
		// the ability to bring the uncool object.
		if (cool) {
		    out.printLine("Yes!  Bring " + thing + " along!");
		}
		else {
		    out.printLine("Sorry, you can't bring " + thing + ".");
		}
		out.skipLine(1);
	    }
	} while (playmore);

	// The user has decided to quit the game, so we thank them.
	out.printLine("Thanks for playing the vacation game!");
    }
}
