/*******************************************************************************
Program:			Triple Roll

Author:				Rebecca Hasti, hasti@cs.wisc.edu
				copyright 2000, 2001, all rights reserved
Course:				CS 302

Compiler:			Metrowerks CodeWarrior (JDK 1.2)
Platform:			Windows (NT 4.0 or 95 or 2000)
*******************************************************************************/

import javabook.*;

/**
 * Represents a game of Triple Roll.  A game consists of two players.  The
 * players take turns rolling three dice, trying to get the highest score.
 *
 * Bugs:	none known
 **/
class TripleRoll {

	////////////////////////////////////////
	// Data Members
	////////////////////////////////////////
	
	// Public Constants
	public static final int HIGH_DIE = 1;
	public static final int TWO_OF_A_KIND = 2;
	public static final int THREE_OF_A_KIND = 3;
	public static final int STRAIGHT = 4;
	
	private Die die1, die2, die3;				// the three dice
	private TripleRollPlayer player1, player2;	// the two players
	private MainWindow mainWindow;				// the main frame for game
	private OutputBox outputBox;				// for output
	
	////////////////////////////////////////
	// Constructors
	////////////////////////////////////////
	
	/**
	 * Constructs a game of TripleRoll given a main window.  A game has
	 * two players and three dice.
	 * @param mw the MainWindow object for the game
	 **/
	public TripleRoll(MainWindow mw) {
		InputBox inputBox = new InputBox(mw);
		String name;
	
		die1 = new Die(6);
		die2 = new Die(6);
		die3 = new Die(6);
		
		name = inputBox.getString("Please enter player 1's name:");
		player1 = new TripleRollPlayer(name);
		name = inputBox.getString("Please enter player 2's name:");
		player2 = new TripleRollPlayer(name);
		
		mainWindow = mw;
		outputBox = new OutputBox(mainWindow);
		outputBox.show();
	}
	
	////////////////////////////////////////
	// Public Methods
	//		void	declareWinner ( )
	//		void	playerTurn    ( TripleRollPlayer player )
	//		void	playRound     ( )
	////////////////////////////////////////
	
	/**
	 * Declares the winner of the game.  The winner is the player with
	 * the highest total score.
	 **/
	public void declareWinner() {
		int score1 = player1.getTotalScore();	// player 1's total score
		int score2 = player2.getTotalScore();	// player 2's total score
	
		outputBox.printLine("%%%%%%%% End Of Game %%%%%%%%");
		outputBox.printLine("Score for " + player1.getName() + 
							": " + score1);
		outputBox.printLine("Score for " + player2.getName() + 
							": " + score2);

		if (score1 > score2) 					// player 1 won
			outputBox.printLine(player1.getName() + " is the winner!");
		
		else if (score2 > score1) 				// player 2 won
			outputBox.printLine(player2.getName() + " is the winner!");
		
		else 									// tie game
			outputBox.printLine("Tie game!");
	} // end declareWinner

	/**
	 * Completes one turn of Triple Roll for the given player.  A turn 
	 * starts by having the player roll the dice.  The player is then
	 * asked if he or she wishes to re-roll one die and if the answer is
	 * yes, the player is allowed to choose which die to re-roll.  The
	 * current score for the roll as well as what it is (e.g. two of a
	 * kind) is displayed and at the end of the turn, the player's total
	 * score so far is also displayed.
	 * 
	 * @param player the player whose turn it is
	 **/
	public void playerTurn(TripleRollPlayer player) {
		ResponseBox responseBox;
		ListBox listBox;
		int response,				// holds answer from responseBox
		    choice;					// holds answer from listBox
		boolean noChoiceMade;
		
		// create javabook objects
		responseBox = new ResponseBox(mainWindow);
		listBox = new ListBox(mainWindow, "Which die?");
		listBox.addItem("Die 1");
		listBox.addItem("Die 2");
		listBox.addItem("Die 3");
	
		// print out info
		outputBox.printLine("****** Begin Turn for " + player.getName() +
		                    " ******");
	
		// roll the dice and report the values
		player.rollDice(die1, die2, die3);
		outputBox.printLine("    Die 1: " + die1.getValue() + 
		                	"    Die 2: " + die2.getValue() + 
		                	"    Die 3: " + die3.getValue());
		                
		// have the player evaluate the roll
		player.evaluateRoll(die1, die2, die3);
		outputBox.printLine("    " + translateKind(player.getRollKind()) +
							"   Score: " + player.getRollScore());
	
		// ask if player wants to re-roll a die
		response = responseBox.prompt("Do you want to re-roll a die?");
		
		// if so, prompt for the die
		if (response == ResponseBox.YES) {
			choice = listBox.getSelectedIndex();
			noChoiceMade = false;
			
			switch (choice) {
				case 0:		die1.rollDie();
							break;
							
				case 1:		die2.rollDie();
							break;
							
				case 2:		die3.rollDie();
							break;
							
				default:	noChoiceMade = true;
			
			}
			
			if (!noChoiceMade) {
				player.evaluateRoll(die1, die2, die3);
				outputBox.printLine("    Re-roll die...");
				outputBox.printLine("    Die 1: " + die1.getValue() + 
		                			"    Die 2: " + die2.getValue() + 
		                			"    Die 3: " + die3.getValue());
				outputBox.printLine("    " + 
									translateKind(player.getRollKind()) +
									"   Score: " + player.getRollScore());
			} // end if chose to re-roll a die
		} // end if response was yes
		
		// add the score to the total score
		player.addRollToTotal();
		outputBox.printLine("****** Score so far for " + player.getName() +
							": " + player.getTotalScore() + " ******");
	} // end playerTurn
	
	/**
	 * Plays one round of Triple Roll by giving each player a turn.
	 **/
	public void playRound() {
		playerTurn(player1);
		playerTurn(player2);
	} 

	////////////////////////////////////////
	// Public Static Methods
	//		void	main          ( String args[] )
	//		String	translateKind ( int kind )
	////////////////////////////////////////
	
	/**
	 * The main method.  Plays three rounds of Triple Roll and declares
	 * the winner.
	 **/
	public static void main(String args[]) {
		// declare and create a main window for the program
		MainWindow mainWindow = new MainWindow("Triple Roll");
		mainWindow.show();
		
		// declare and create the game
		TripleRoll game = new TripleRoll(mainWindow);
		
		// play three rounds of the game and declare a winner
		game.playRound();
		game.playRound();
		game.playRound();
		game.declareWinner();
	} // end main

	/**
	 * Translates the kind to a String (for printing).
	 * @param kind one of the class constants representing the kind
	 * @return a String representation of the kind
	 **/
	public static String translateKind(int kind) {
		String translation;		// the kind translated to a String
		
		switch (kind) {

			// Fill in in class



















				
		} // end switch
			
		return translation;
	} // end translateKind
	
} // end class TripleRoll
