import java.util.Scanner;
/**
 * Lulz
 * @author Eric Joyner
 *
 */

public class Game1 {

	/**
	 * This is basically what runs the program...
	 * It really doesn't need an explanation.
	 * @param args
	 */
	public static void main(String[] args) {
		
		// initialize important variables
		Scanner console = new Scanner(System.in);
		String input = "";
		
		boolean done = false;
		String name = "";
		int choice = -2;
		int hp = 5;
		int maxhp = 5;
		
		// game start
		System.out.println("/****************************\\");
		System.out.println("*Gam1                        *");
		System.out.println("*A (never?)ending while loop.*");
		System.out.println("\\****************************/\n");
		System.out.println("Hello, strapping young adventurer!");
		System.out.println("Comment t'appelles-tu?");
		
		input = console.nextLine();
		// say something needy if the player doesn't enter their name
		while( input.trim().equals("") ) {
			System.out.println("You can't be nameless. Everyone has a name...right?");
			System.out.println("Tell me I'm right by entering your name on the next line.");
			input = console.nextLine();
		}
		// store the player's name and clear input
		name = input; input = "";
		
		// game loop
		game: while(!done) {
			hp = maxhp;
			System.out.println("\n*****************************************");
			System.out.println("You, " + name + ", are on an adventure!");
			System.out.println("Delving into the depths of bits and bytes,");
			System.out.println("You look for what you're trying to find.\n");
			
			System.out.println("There are two directions that you can move in.");
			System.out.println("Do you go EAST, WEST, or GIVE UP (it's always an option at any time in the game)?");
			
			// input checking
			do {
				if( choice == -1 ) {
					System.out.println(name + " only has three choices here. It shouldn't be too hard to pick one.");
				}
				input = console.nextLine();
				choice = choiceParse(input, new String[] {"GIVE UP", "EAST", "WEST" } );
			} while(choice == -1);
			
			//*** the outcome of your first choice! ***
			switch( choice ) {
			// GIVE UP
			case 0:
				break game; // lulz
			// EAST
			case 1:
				System.out.println("Considering that right makes might, you saunter in the easternly direction.");
				System.out.println("If only other people on adventures had " + name + "'s foresight!");
				System.out.println("You are out of the place you were in! Pressing forward...");
				break;
			// WEST
			case 2:
				System.out.println("Thinking it's always good to go from left to right, " +
						"you make your way west.\n");
				
				int enemy = (int)Math.round(Math.random() * 0xffff);
				if( enemy < 0xffff / 2 )
					System.out.println("Unfortunately for " + name + " it is the case such that it is not a good idea.");
				else
					System.out.println("Unfortunately for " + name + " it is not the case such that it is a good idea.");
				
				System.out.println("You encounter a hexadecimal number!");
				System.out.println("The leading 0x is very scary looking.");
				
				input = "";
				boolean monster1IsDead = false;
				
				// fight-the-monster1 loop!
				while(!monster1IsDead) {
					System.out.printf("\nThe number %#x is" + ((input.isEmpty()) ? "" : " still") + " all up in your grill!\n", enemy);
					System.out.println((hp <= 0.4 * maxhp) ? "You're probably not going to make it, but are you going to..." : "What do you plan on doing?");
					System.out.println("SLASH it, DEFEND against it, or RUN AWAY?");
					
					// input checking
					do {
						if( choice == -1 ) {
							System.out.println(name + " tried to " + input + "! But you can't do that!");
						}
						input = console.nextLine();
						choice = choiceParse(input, new String[] {"GIVE UP", "SLASH", "DEFEND", "RUN AWAY" } );
					} while(choice == -1);
					
					switch(choice) {
					// GIVE UP
					case 0: break game;
					// SLASH
					case 1:
						System.out.println("You slash at the hexadecimal!");
						double toHit = Math.random();
						if( toHit > 0.5 ) {
							System.out.println("You land a sundering blow!");
							enemy = enemy >> 4;
							if( enemy <= 0 ) {
								monster1IsDead = true;
							}
						}
						else {
							System.out.println("You flail around wildly and miss!");
							if (toHit < 0.2 ) {
								System.out.printf("\nThe %#x takes advantage of your ineptitude and hits you!\n", enemy);
								hp--;
								System.out.println("...leaving you with only " + hp + " out of " + maxhp + " health.");
							}
							if( hp <= 0 ) {
								System.out.println("...and that means you die an ignoble death.");
								System.out.println("If only " + name + " had taken a non-remedial CS course...");
								break game;
							}
						}
						break;
					// DEFEND
					case 2:
						System.out.println("Using tactics, you prepare for the hexadecimal's attack!");
						double toDefend = Math.random();
						if( toDefend < 0.1 ) {
							System.out.println("Unfortunately, they failed miserably.");
							System.out.printf("\nThe %#x takes advantage of your ineptitude and hits you!\n", enemy);
							hp--;
							System.out.println("...leaving you with only " + hp + " out of " + maxhp + " health.");
						}
						else if( toDefend < 0.9 ) {
							System.out.println("The hexadecimal cannot hit you, and you avoid taking a hit.");
							System.out.println("However...");
						}
						else {
							System.out.println("That motherf**king freak of odd-number bases doesn't stand a chance!");
							System.out.println("You slash at the hexadecimal and chop off its rear end!");
							enemy = enemy >> 4;
							if( enemy <= 0 ) {
								monster1IsDead = true;
							}
							else
								System.out.println("However...");
						}
						break;
					// RUN AWAY
					case 3:
						System.out.println("Being the candy-ass that you are, you believe its time to leave.");
						System.out.println("Attempting to run to the nearest exit...");
						double toRun = Math.random();
						if( toRun > 1 /* 0.8 */ ) {
							System.out.println("...your fast legging makes it there in time!");
							System.out.printf("Cya later, %#x!\n", enemy);
							monster1IsDead = true;
						}
						else {
							System.out.printf("...you determine that being a wimp doesn't pay as %#x attacks you.\n", enemy);
							hp--;
							System.out.println("...leaving you with only " + hp + " out of " + maxhp + " health.");
						}
					}
					
					if( monster1IsDead ) {
						/*
						System.out.println("The hexadecimal has been chopped up into its component bits!");
						System.out.println("Don't get too cocky though...next time it might say to you:");
						*/
						System.out.println("The evil hexadecimal is defeated!");
						System.out.println("You press on stoically...");
						/*
						System.out.println("But for now, I'll only say,");
							// and life goes on!
						break game;
						*/
					}
					
				} // end of fight-the-monster1 loop
				break;
				
			}
			
			// now to continue after choice 2...
			/*
			System.out.println("\nYou soon approach and are in the outside.");
			System.out.println("What do you think you're going to do?");
			System.out.println("Head NORTH, SOUTH, or GIVE UP (pansy)?");
			*/
			break game;
		}
		// end of game
		System.out.println("CIAO!");
		console.nextLine();
	}

	/**
	 * This method checks a string against a list of strings, and then returns the position if it matches.
	 * If it doesn't, it returns -1.
	 * @param entry the string to be compared
	 * @param choices the list of strings to compare entry with
	 * @return The position of the string, or -1 if there is no match.
	 */
	static int choiceParse(String entry, String... choices ) {
		entry = entry.trim().toUpperCase();
		for( int i = 0; i < choices.length; i++ ) {
			if( entry.equals(choices[i]) )
			{
				return i;
			}
		}
		return -1;
	}
	
	static boolean fiftyfifty() {
		return Math.random() > 0.5;
	}

}

