
/**
 * <p>
 * For this problem, you will write some helper methods which will be used to play the game pig.
 * </p>
 * <h2>Playing Pig</h2>
 * Pig is a dice game where the goal is to score 100 points. Players take turns repeatedly rolling a die
 * to accumulate points (their turn total). If they ever roll a 1, their turn total is 0, and it becomes 
 * the other player's turn. If they choose to stop rolling before rolling a 1, they add their turn total 
 * to their score and it is the other player's turn.
 * 
 * <h3>An example round</h3>
 * <pre>
 The score starts at 0-0.
 Player one rolls: 5 4 6 4 3 1 so receives no points.
 Player two rolls: 3 2 5 3 2 2 4 and then holds adding the total, 21, to their score.
 The score is now 0-21.
 </pre>
 <h2>The Pig GUI</h2>
 <ol>
 <li>Create project for the Pig Game.
 <li>Download PigSwing.java and PigHelper.java from the assignment. PigSwing contains the main program to run. 
 PigHelper is where you will put your code
 <li>Run PigSwing. It should display a window with working buttons.
 <li>Write the body of the roll method. Test the use of the roll button. You should be able to accumulate points by rolling
 and holding. The computer will get 0 every turn.
 <li>Write the body of isGameOver. Test to make sure the game ends when you get 100 points.
 <li>Write the cpuRollAgain method. Start by returning turnTotal &lt; 20 (a simple strategy).
 <li>Write cpuTurn as described in its documentation.
 </ol>
 */
public class PigHelper {

	/**
	 * Use Math.random to produce a roll of a six-sided die.
	 * This method is called any time the die needs to be rolled. 
	 * @return a random value between 1 and 6.
	 */
	public static int roll() {
		//roll the die - return a random value between 1 and 6
		return (int)(Math.random()*6) + 1;
	}
	
	
	/**
	 * The game is over when someone's score is 100 or more.
	 * This method is called any time we need to check if the game is over.
	 * @param score The score to be checked.
	 * @return true if the game is over, false otherwise.
	 */
	public static boolean isGameOver(int score) {
		//method stub
		if (score >= 100) {
			return true;
		}
		return false;
		
		//return score >= 100;
	}
	
	/**
	 * Determine if the cpu should roll again based on its score, your score, and its turn total.
	 * One simple method, although sub-optimal, is for the computer to roll if it hasn't
	 * gotten enough points this round, or it's score is lower than the player's, or the player 
	 * is close to winning. You can decide what "enough points" are and what it means for a player
	 * to be close to winning.
	 * <p>
	 * This method is called to determine if the computer player should roll or hold.
	 * </p>
	 * @param myScore the computer's score.
	 * @param yourScore the player's score.
	 * @param myTurnTotal the computer's current turn total
	 * @return true if the Computer should roll again and false otherwise.
	 */
	public static boolean cpuRollAgain(int myScore, int yourScore, int myTurnTotal) {
		//hold at 20
		return myTurnTotal < 20 ||   //did I get enough?
				myScore + myTurnTotal < yourScore ||  //am I behind?
				yourScore >= 94; //is the other player close to winning
		
	}
	
	/**
	 * Run the entire turn for the computer player, adding each dice roll to a String as you go.
	 * At the end concatenate an equal sign and the turn total. 
	 * <p>
      2 3 4 =9 indicates the computer rolled a 2 then a 3 then a 4 and then decided to hold with a turn total of 9.
      </p><p>
      6 4 2 1 =0 indicates the computer rolled a 6, 4, 2, and finally a 1 which ended its turn with no points.
      </p>
      <p>
      Repeatedly roll the dice until one of the following conditions hold
      <ul>
      <li> 1 is rolled, or 
      <li>the computer can hold and win the game, or 
      <li>the computer chooses to hold (as defined by the cpuRollAgain method). 
      </ul>
      <p>
      Update the turn total as you go. Concatenate each roll onto the 
      result String.
      </p>
      <p>If the loop ends from a dice roll of 1, set turn total to 0.
	 * <p>
	 * This method should call the other methods you wrote whenever possible.
	 * </p>
	 * <p>
	 * This method is called to simulate the computer player's turn and provide the resulting turn total.
	 * </p>
	 * @param myScore the computer's score.
	 * @param yourScore the player's score.
	 * @return the String as described above. Be sure it ends with = followed by the turn total.
	 */
	public static String cpuTurn(int myScore, int yourScore) {
		String result = "";  // start with ""
		int turnTotal = 0;
		int roll = 0;
		
		//keep rolling until we need to stop
		do {
			turnTotal += roll;
			//roll the dice
			roll = roll();
			result += roll + " ";
			
		}while(roll != 1 && //not rolled a 1
				!isGameOver(myScore + turnTotal) && //not end of game
				cpuRollAgain(myScore, yourScore, turnTotal)); //should I roll?
		
		if(roll == 1) {
			turnTotal = 0;
		}
		
		result += "=" + turnTotal;
		return result;
	}
	
	
	private PigHelper() {}
	
}
