/*******************************************************************************
Program:			TripleRoll

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)
*******************************************************************************/

/**
 * Represents one player in a Triple Roll game.  A player has a name and keeps
 * track of the current values of the three dice, the current kind of roll the
 * dice represent (e.g. two of a kind), the current score for the roll, and the
 * total score for the player.
 *
 * Bugs:	none known
 **/
class TripleRollPlayer {

	////////////////////////////////////////
	// Data Members
	////////////////////////////////////////
	
	private String name;	// player's name
	private int dieValue1, dieValue2, dieValue3;	// values of dice
	private int rollKind;	// kind of current roll (a constant from TripleRoll)
	private int rollScore;	// score of current roll (>= 0)
	private int totalScore;	// total score for player (>= 0)
	
	
	////////////////////////////////////////
	// Constructors
	////////////////////////////////////////
	
	/**
	 * Constructs a TripleRollPlayer with the given name.
	 * @param playerName the name of the player
	 **/
	public TripleRollPlayer(String playerName) {
		name = playerName;
		dieValue1 = 0;
		dieValue2 = 0;
		dieValue3 = 0;
		rollKind = 0;
		rollScore = 0;
		totalScore = 0;
	}

	////////////////////////////////////////
	// Accessor Methods
	//		String	getName       ( )
	//		int		getRollKind   ( )
	//		int		getRollScore  ( )
	//		int		getTotalScore ( )
	////////////////////////////////////////

	/**
	 * Returns the name of the player.
	 * @return a String representing the name of the player
	 **/
	public String getName() {
		return name;
	}
	
	/**
	 * Returns the kind of the current roll.
	 * @return the kind of the roll (== one of the constants from TripleRoll)
	 **/
	public int getRollKind() {
		return rollKind;
	}
	
	/**
	 * Returns the score of the current roll.
	 * @return the current roll score (>= 0)
	 **/
	public int getRollScore() {
		return rollScore;
	}
	
	/**
	 * Returns the total score for the player.
	 * @return the total score (>= 0)
	 **/
	public int getTotalScore() {
		return totalScore;
	}
	
	////////////////////////////////////////
	// Other Public Methods
	//		void	addRollToTotal ( )
	//		void	evaluateRoll   ( Die die1, Die die2, Die die3 )
	//		void	rollDice       ( Die die1, Die die2, Die die3 )
	////////////////////////////////////////
	
	/**
	 * Adds the current roll score to the total score.
	 **/
	public void addRollToTotal() {
		totalScore += rollScore;
	}
	
	/**
	 * Evaluates the values on the three dice by determining what kind of roll
	 * it is (e.g. two of a kind) and what the score for the roll is.  Sets the
	 * data members dieValue1, dieValue2, dieValue3, rollKind, and rollScore.
	 *
	 * @param die1 one of the dice
	 * @param die2 one of the dice
	 * @param die3 one of the dice
	 **/
	public void evaluateRoll(Die die1, Die die2, Die die3) {
		int dieValue;	// holds the die value needed to compute the score
	
		// get the values from the dice and put them in order
		dieValue1 = die1.getValue();
		dieValue2 = die2.getValue();
		dieValue3 = die3.getValue();
		orderDieValues();
		
		// determine what was rolled
		if ((dieValue2 == dieValue1 + 1) && (dieValue3 == dieValue2 + 1)) {
			rollKind = TripleRoll.STRAIGHT;
			dieValue = dieValue2;
		}
		
		else if ((dieValue1 == dieValue2) && (dieValue2 == dieValue3)) {
			rollKind = TripleRoll.THREE_OF_A_KIND;
			dieValue = dieValue1;		
		}
		
		else if ((dieValue1 == dieValue2) || (dieValue2 == dieValue3)) {
			rollKind = TripleRoll.TWO_OF_A_KIND;
			dieValue = dieValue2;		
		}
		
		else {
			rollKind = TripleRoll.HIGH_DIE;
			dieValue = dieValue3;				
		}	
		
		// compute the score for the roll
		rollScore = rollKind * dieValue;
	} // end evaluateRoll
	
	/**
	 * Rolls the three dice given.
	 *
	 * @param die1 one of the dice
	 * @param die2 one of the dice
	 * @param die3 one of the dice
	 **/
	public void rollDice(Die die1, Die die2, Die die3) {
		die1.rollDie();
		die2.rollDie();
		die3.rollDie();
	}
	
	////////////////////////////////////////
	// Private Methods
	//		void	orderDieValues ( )
	////////////////////////////////////////
	
	/**
	 * Puts the die values in numerical order from smallest to largest.
	 **/
	private void orderDieValues() {
		int temp; 		// holds value temporarily while swapping
		
		if (dieValue3 < dieValue2) {
			temp = dieValue3;
			dieValue3 = dieValue2;
			dieValue2 = temp;
		}
		
		if (dieValue2 < dieValue1) {
			temp = dieValue2;
			dieValue2 = dieValue1;
			dieValue1 = temp;		
		}
		
		if (dieValue3 < dieValue2) {
			temp = dieValue3;
			dieValue3 = dieValue2;
			dieValue2 = temp;
		}	
	} // end orderDieValues
	
} // end class TripleRollPlayer
