teendev
3/2/2016 - 4:25 AM

YahtzeeScorecard.java


/**
 * Insert name and submission date here
 */
public class YahtzeeScorecard
{
    /* instance data given */
    private int ones;
    private int twos;
    private int threes;
    private int fours;
    private int fives;
    private int sixes;
    private int threeKind;
    private int fourKind;
    private int fiveKind;
    private int chance;
    private int fullHouse;
    private int smStraight;
    private int lgStraight;
    private boolean bonus;

    /* Sets up a new game.  Sets all instance data to incomplete (-1). Sets bonus to false */
    public YahtzeeScorecard()
    {
        // set all values to -1 (incomplete)
        this.ones = this.twos = this.threes = this.fours = this.fives = this.sixes = this.threeKind = this.fourKind = this.fiveKind = this.chance = this.fullHouse = this.smStraight = this.lgStraight = -1;
        this.bonus = false;
    }

    /**
     * For methods 1-6, checks to see if any of the die match the last param (num) and then
     * returns the total score to be added.
     * @param die1
     * @param die2
     * @param die3
     * @param die4
     * @param die5
     * @param num
     * @return int of total score
     */
    private int checkDigit(int die1, int die2, int die3, int die4, int die5, int num) {
        int total = 0;
        if (die1 == num) {
            total += num;
        }
        if (die2 == num) {
            total += num;
        }
        if (die3 == num) {
            total += num;
        }
        if (die4 == num) {
            total += num;
        }
        if (die5 == num) {
            total += num;
        }

        return total;
    }
    /* 1. If the field is already full, return false
       2. Set data value ones equal to number of ones rolled multiplied by one.
       3. Update the bonus (call updateBonus())
       4. Return true   */
    public boolean markOnes(int die1, int die2, int die3, int die4, int die5)
    {
        if (this.ones != -1 ) {
            return false;
        }

        int total = this.checkDigit(die1, die2, die3, die4, die5, 1);

        this.ones = total;
        this.updateBonus();

        return true;
    }

    /* 1. If the field is already full, return false
       2. Set data value twos equal to number of twos rolled multiplied by two.
       3. Update the bonus (call updateBonus())
       4. Return true   */
    public boolean markTwos(int die1, int die2, int die3, int die4, int die5)
    {
        if (this.twos != -1 ) {
            return false;
        }

        int total = this.checkDigit(die1, die2, die3, die4, die5, 2);

        this.twos = total;
        this.updateBonus();

        return true;
    }

    /* 1. If the field is already full, return false
       2. Set data value threes equal to number of threes rolled multiplied by three.
       3. Update the bonus (call updateBonus())
       4. Return true  */
    public boolean markThrees(int die1, int die2, int die3, int die4, int die5)
    {
        if (this.threes != -1 ) {
            return false;
        }

        int total = this.checkDigit(die1, die2, die3, die4, die5, 3);

        this.threes = total;
        this.updateBonus();

        return true;
    }

    /* 1. If the field is already full, the method returns false
       2. Sets data value fours equal to number of fours rolled multiplied by four.
       3. Update the bonus (call updateBonus())
       4. Returns true   */
    public boolean markFours(int die1, int die2, int die3, int die4, int die5)
    {
        if (this.fours != -1 ) {
            return false;
        }

        int total = this.checkDigit(die1, die2, die3, die4, die5, 4);

        this.fours = total;
        this.updateBonus();

        return true;
    }

    /* 1. If the field is already full, the method returns false
       2. Sets data value fives equal to number of fives rolled multiplied by five.
       3. Update the bonus (call updateBonus())
       4. Returns true   */
    public boolean markFives(int die1, int die2, int die3, int die4, int die5)
    {
        if (this.fives != -1 ) {
            return false;
        }

        int total = this.checkDigit(die1, die2, die3, die4, die5, 5);

        this.fives = total;
        this.updateBonus();

        return true;
    }

    /* 1. If the field is already full, the method returns false
       2. Sets data value sixes equal to number of sixes rolled multiplied by six.
       3. Update the bonus (call updateBonus())
       4. Returns true */
    public boolean markSixes(int die1, int die2, int die3, int die4, int die5)
    {
        if (this.sixes != -1 ) {
            return false;
        }

        int total = this.checkDigit(die1, die2, die3, die4, die5, 6);

        this.sixes = total;
        this.updateBonus();

        return true;
    }

    /* 	1. If the field is already full, return false
        2. Check to see if there are actually three of the same value.
           If there are, set the data value threeKind to the value of all five dice.
           If there aren’t set the value to 0.
              (Hint: use YahtzeeSortDice class!)
           4. Return true   */
    public boolean markThreeOfAKind(int die1, int die2, int die3, int die4, int die5)
    {
		if (this.threeKind != -1) {
            return false;
        }

        YahtzeeSortDice sorted = new YahtzeeSortDice(die1, die2, die3, die4, die5);

        int first = sorted.getFirst();

        if (first != sorted.getSecond()) {
            if (sorted.getSecond() != sorted.getThird()) {
                if (sorted.getThird() != sorted.getFourth()) {
                    this.threeKind = 0;
                } else {
                    if (sorted.getFifth() == sorted.getFourth()) {
                        this.threeKind = die1 + die2 + die3 + die4 + die5;
                    }
                }
            } else {
                if (sorted.getSecond() == sorted.getFourth() || sorted.getSecond() == sorted.getFifth()) {
                    this.threeKind = die1 + die2 + die3 + die4 + die5;
                } else {
                    this.threeKind = 0;
                }
            }
        } else {
            if (sorted.getFirst() == sorted.getThird() || sorted.getFirst() == sorted.getFourth() || sorted.getFifth() == sorted.getFifth()) {
                this.threeKind = die1 + die2 + die3 + die4 + die5;
            } else {
                this.threeKind = 0;
            }
        }

        this.updateBonus();
        return true;
    }

    /* 	1. If the field is already full, return false
        2. Check to see if there are actually four of the same value.
           If there are, set the data value fourKind to the value of all five dice.
           If there aren’t set the value to 0;
           (Hint: use YahtzeeSortDice class!)
        4. Return true  */
    public boolean markFourOfAKind(int die1, int die2, int die3, int die4, int die5)
    {
		if (this.fourKind != -1 ) {
            return false;
        }

        YahtzeeSortDice sorted = new YahtzeeSortDice(die1, die2, die3, die4, die5);

        if (sorted.getFirst() != sorted.getSecond()) {
            if (sorted.getThird() != sorted.getSecond()) {
                this.fourKind = 0;
            } else {
                if (sorted.getSecond() == sorted.getFourth() && sorted.getSecond() == sorted.getFifth()) {
                    this.fourKind = die1 + die2 + die3 + die4 + die5;
                }
            }
        } else {
            if (sorted.getFirst() != sorted.getThird()) {
                if (sorted.getFourth() == sorted.getFirst() && sorted.getFifth() == sorted.getFirst()) {
                    this.fourKind = die1 + die2 + die3 + die4 + die5;
                } else {
                    this.fourKind = 0;
                }
            } else {
                if (sorted.getFirst() != sorted.getFourth()) {
                    if (sorted.getFirst() == sorted.getFifth()) {
                        this.fourKind = die1 + die2 + die3 + die4 + die5;
                    } else {
                        this.fourKind = 0;
                    }
                } else {
                    this.fourKind = die1 + die2 + die3 + die4 + die5;
                }
            }
        }

        this.updateBonus();
        return true;
    }

    /* 1. If the field is already full, return false
       2. Check to see if there are actually three die with the same value, and two with another value.
          If there are, set the data value fullHouse to 25.
          If there aren’t set the value to 0.
          (Hint: Use YahtzeeSortDice class!)
       3. Return true   */
    public boolean markFullHouse(int die1, int die2, int die3, int die4, int die5)
    {
		if (this.fullHouse != -1) {
            return false;
        }

        // use basic bubble sort algorithm

        int[] dice = {die1, die2, die3, die4, die5};

        int count = 1;
        boolean found3 = false, found2 = false;

        for (int i =1; i < dice.length; i++) {
            if (dice[i] == dice[i-1]) {
                count++;
            } else {
                if (count == 3) {
                    found3 = true;
                } else if (count == 2) {
                    found2 = true;
                }
                count = 1;
            }
        }

        if (count == 3) {
            found3 = true;
        } else if (count == 2) {
            found2 = true;
        }
        if (found3 && found2) {
            this.fullHouse = 25;
        } else {
            this.fullHouse = 0;
        }


        this.updateBonus();
        return true;
    }

    /* 	1. If the field is already full, return false
        2. Check to see if there are actually four consecutive dice numbers.
           If there are, set the data value smStraight to 30.
           If there aren’t set the value to 0.
           (Hint: Use YahtzeeSortDice class)
        4. Return true  */
    public boolean markSmallStraight(int die1, int die2, int die3, int die4, int die5)
    {
		if (this.smStraight != -1 ) {
            return false;
        }

        int[] dice = {die1, die2, die3, die4, die5};
        // use another bubble sort
        int straight = 1;

        for (int i = 1; i < dice.length; i++) {
            if ((dice[i-1]+1) == dice[i]) straight++;
        }

        if (straight >= 3) {
            this.smStraight = 30;
        } else {
            this.smStraight = 0;
        }

        this.updateBonus();
        return true;
    }

    /* 	1. If the field is already full, return false
        2. Check to see if there are actually five consecutive dice numbers.
           If there are, set the data value lgStraight to 40.
           If there aren’t set the value to 0;
           (Hint: use YahtzeeSortDice class!)
        3. Return true  */
    public boolean markLargeStraight(int die1, int die2, int die3, int die4, int die5)
    {
        if (this.lgStraight != -1 ) {
            return false;
        }

        int[] dice = {die1, die2, die3, die4, die5};
        // use another bubble sort
        int straight = 1;

        for (int i = 1; i < dice.length; i++) {
            if ((dice[i-1]+1) == dice[i]) straight++;
        }

        if (straight >= 5) {
            this.lgStraight = 40;
        } else {
            this.lgStraight = 0;
        }

        this.updateBonus();
        return true;
    }

    /* 1. If the field is already full, return false
       2. Checks to see if there are actually five of the same value.  If there are, set the data value fiveKind to 50.  If there aren’t set the value to 0;
       3. Return true   */
    public boolean markYahtzee(int die1, int die2, int die3, int die4, int die5)
    {
		if (this.fiveKind != -1 ) {
            return false;
        }

        if (die1 == die2 && die2 == die3 && die4 == die3 && die5 == die4) {
            this.fiveKind = 50;
        } else {
            this.fiveKind = 0;
        }

        this.updateBonus();
        return true;
    }

    /* 1. If the field is already full, return false
       2. Set the data value chance to the value of all five dice.
       3. Return true  */
    public boolean markChance(int die1, int die2, int die3, int die4, int die5)
    {
		if (this.chance != -1) {
            return false;
        }

        this.chance = die1 + die2 + die3 + die4 + die5;

        this.updateBonus();
        return true;
    }

    /* 	1. If the bonus has already been assigned, do nothing
        2. If the upper section’s total is 63 or greater, set the data value bonus to true */
    private void updateBonus()
    {
		if (!this.bonus && this.getUpperTotal() >= 63) {
            this.bonus = true;
        }
    }

    /* 	returns the upper total, remember incompletes (-1s) should not be factored in! */
    public int getUpperTotal()
    {
        int total = 0;

        if (this.ones != -1 ) {
            total += this.ones;
        }
        if (this.twos != -1 ) {
            total += this.twos;
        }
        if (this.threes != -1) {
            total += this.threes;
        }
        if (this.fours != -1) {
            total += this.fours;
        }
        if (this.fives != -1) {
            total += this.fives;
        }
        if (this.sixes != -1) {
            total += this.sixes;
        }

        if (this.bonus) {
            total += 35;
        }

        return total;
    }

    /* 	returns the lower total, remember incompletes (-1s) should not be factored in! */
    public int getLowerTotal()
    {
        int total = 0;

        if(this.threeKind != -1) {
            total += this.threeKind;
        }

        if(this.fourKind != -1) {
            total += this.fourKind;
        }

        if(this.fullHouse != -1) {
            total += this.fullHouse;
        }

        if(this.fiveKind != -1) {
            total += this.fiveKind;
        }

        if(this.smStraight != -1) {
            total += this.smStraight;
        }

        if(this.lgStraight != -1) {
            total += this.lgStraight;
        }

        if(this.chance != -1) {
            total += this.chance;
        }

        return total;
    }

    /* 	returns the grand total, remember incompletes (-1s) should not be factored in! */
    public int getGrandTotal()
    {
        return this.getLowerTotal() + this.getUpperTotal();
    }

    /*	Prints a scorecard with the current total, using "__" to mark uncompleted fields and a number to mark filled fields
        Sample:
        **********************************
        *  	    Yahtzee Score Card		 *
        *  					`		  	 *
        * 	Ones:				__		 *
        * 	Twos:				__		 *
        * 	Threes:				__		 *
        * 	Fours:				__		 *
        * 	Fives:				25		 *
        * 	Sixes:				__		 *
        *								 *
        *	Upper Bonus:		 0		 *
        * 	Upper Total:   		25		 *
        *								 *
        *	3 of Kind:			__		 *
        * 	4 of Kind:			__		 *
        * 	Full House:			25		 *
        * 	Sm Straight:		__		 *
        * 	Lg  Straight:		__		 *
        * 	Yahtzee:	  		 0		 *
        * 	Chance:				__		 *
        *								 *
        * 	Lower Total:		25		 *
        *								 *
        * 	Grand Total:		50		 *
        **********************************
        already implemented
    */
    public void printScoreCard()
    {
        System.out.println();
        System.out.println("*********************************");
        System.out.println("*      Yahtzee Score Card       *");
        System.out.println("*                               *");
        System.out.print("*  Ones:\t\t");
        if(ones==-1)System.out.print("__");
        else System.out.print(ones);
        System.out.println("\t*");
        System.out.print("*  Twos:\t\t");
        if(twos==-1)System.out.print("__");
        else System.out.print(twos);
        System.out.println("\t*");
        System.out.print("*  Threes:\t\t");
        if(threes==-1)System.out.print("__");
        else System.out.print(threes);
        System.out.println("\t*");
        System.out.print("*  Fours:\t\t");
        if(fours==-1)System.out.print("__");
        else System.out.print(fours);
        System.out.println("\t*");
        System.out.print("*  Fives:\t\t");
        if(fives==-1)System.out.print("__");
        else System.out.print(fives);
        System.out.println("\t*");
        System.out.print("*  Sixes:\t\t");
        if(sixes==-1)System.out.print("__");
        else System.out.print(sixes);
        System.out.println("\t*");
        System.out.println("*\t\t\t\t*");
        System.out.print("*  Upper Bonus:\t\t");
        if(bonus)System.out.print("35");
        else System.out.print("0");
        System.out.println("\t*");
        System.out.print("*  Upper Total:\t\t");
        System.out.print(this.getUpperTotal());
        System.out.println("\t*");
        System.out.println("*                               *");
        System.out.print("*  3 of Kind:\t\t");
        if(threeKind==-1)System.out.print("__");
        else System.out.print(threeKind);
        System.out.println("\t*");
        System.out.print("*  4 of Kind:\t\t");
        if(fourKind==-1)System.out.print("__");
        else System.out.print(fourKind);
        System.out.println("\t*");
        System.out.print("*  Full House:\t\t");
        if(fullHouse==-1)System.out.print("__");
        else System.out.print(fullHouse);
        System.out.println("\t*");
        System.out.print("*  Sm Straight:\t\t");
        if(smStraight==-1)System.out.print("__");
        else System.out.print(smStraight);
        System.out.println("\t*");
        System.out.print("*  Lg Straight:\t\t");
        if(lgStraight==-1)System.out.print("__");
        else System.out.print(lgStraight);
        System.out.println("\t*");
        System.out.print("*  Yahtzee:\t\t");
        if(fiveKind==-1)System.out.print("__");
        else System.out.print(fiveKind);
        System.out.println("\t*");
        System.out.print("*  Chance:\t\t");
        if(chance==-1)System.out.print("__");
        else System.out.print(chance);
        System.out.println("\t*");
        System.out.println("*                               *");
        System.out.print("*  Lower Total:\t\t");
        System.out.print(this.getLowerTotal());
        System.out.println("\t*");
        System.out.println("*                               *");
        System.out.print("*  Grand Total:\t\t");
        System.out.print(this.getGrandTotal());
        System.out.println("\t*");
        System.out.println("**********************************");
        System.out.println();
    }

}