/*******************************************************************************
Main Class:			TwentyOneFiveHundred

Authors:			Rebecca Hasti, hasti@cs.wisc.edu
                    Patrick Votruba, votruba@cs.wisc.edu
					copyright 2001, all rights reserved
Completion Date:	7/11/2001
Course:				CS 302, Summer 2001

Compiler:			javac (JDK 1.2)
Platform:			Solaris
*******************************************************************************/
/**
 * A simulation of the card game, 21-500
 *
 * Bugs:	none known
 **/

import javabook2.*;

public class TwentyOneFiveHundred {

	public static final int MAX_GAME_SCORE = 500;
	public static final int MAX_TURN_SCORE = 21;

	/**
	 * Prompts the user for a yes or no answer
	 * @param in the InputBox for user input
	 * @return the answer (true for yes, false for no)
	 */
	private static boolean getYesNoAnswer(InputBox in) {
		int intAnswer;
		boolean answer;
		do {
			intAnswer = in.getInteger("Enter 1(yes) or 0(no)");
		} while (intAnswer != 1 && intAnswer != 0);
		if (intAnswer == 0)	
			return false;
		return true;
	}

	/**
	 * Executes one turn for a player
	 * @param in the InputBox for user input
	 * @param out the OutputBox to display info to user
	 * @return the player's score for the turn
	 */
	private static int oneTurn(InputBox in, OutputBox out) {
		int score = 0;
		boolean goAgain = true;
		Die die = new Die(10);
		while (score < MAX_TURN_SCORE && goAgain) {
			die.roll();
			score += die.getValue();
			
			out.printLine("Your score is: " + score);
			if (score > MAX_TURN_SCORE){
				out.printLine("Busted!");
				goAgain = false;
			}
			else	
				goAgain = getYesNoAnswer(in); // see example above
		}
		if (score > MAX_TURN_SCORE) score = 0; 
		// no points if player breaks 21
		
		else if (score == 21) score *= 5;
		else if (score >= 19) score *= 4;
		else if (score >= 16) score *= 3;
		else if (score >= 11) score *= 2;
		return score;
	}

	/**
	 * The main method drives the game simulation
	 */
	public static void main (String [] args) {
		// Create an initialize mainWindow, in, out
		MainWindow mw  = new MainWindow();
		InputBox   in  = new InputBox(mw);
		OutputBox  out = new OutputBox(mw);
		out.setVisible(true);

		int player1 = 0, player2 = 0;
		boolean gameOver = false;
		while (!gameOver) {
			out.printLine("Player 1's turn:");
			player1 += oneTurn(in, out);
			out.printLine("Player 2's turn:");
			player2 += oneTurn(in, out);
			if (player1 >= MAX_GAME_SCORE || player2 >= MAX_GAME_SCORE)
				gameOver = true;
		}

		/* The winner is the player who scores closest to 500
		 * without going over.
		 */
		if ((player1 > MAX_GAME_SCORE && player2 <= MAX_GAME_SCORE) || 
			(player1 < MAX_GAME_SCORE && player2 == MAX_GAME_SCORE))
			out.printLine("Player 2 won");
		
		else if ((player2 > MAX_GAME_SCORE && 
				  player1 <= MAX_GAME_SCORE) ||
				 (player2 < MAX_GAME_SCORE && 
				  player1 == MAX_GAME_SCORE))
			out.printLine("Player 1 won");
		else 
			out.printLine("Tie game");
	}

} // end TwentyOneFiveHundred class
