SHARE
TWEET

Untitled

a guest Mar 26th, 2019 56 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package model;
  2.  
  3. import java.util.Arrays;
  4.  
  5. public class Yatzy {
  6.     // Face values of the 5 dice.
  7.     // 1 <= values[i] <= 6.
  8.     private int[] values = new int[5];
  9.     // private int[] frequencies = new int[7]
  10.     // Number of times the 5 dice have been thrown.
  11.     // 0 <= throwCount <= 3.
  12.     private int throwCount = 0;
  13.  
  14.     public Yatzy() {
  15.         //
  16.     }
  17.  
  18.     /**
  19.      * Returns the 5 face values of the dice.
  20.      */
  21.     public int[] getValues() {
  22.         return values;
  23.     }
  24.  
  25.     /**
  26.      * Sets the 5 face values of the dice. Req: values contains 5 face values in
  27.      * [1..6]. Note: This method is only meant to be used for test, and therefore
  28.      * has package visibility.
  29.      */
  30.     void setValues(int[] values) {
  31.         this.values = values;
  32.     }
  33.  
  34.     /**
  35.      * Returns the number of times the 5 dice has been thrown.
  36.      */
  37.     public int getThrowCount() {
  38.         return throwCount;
  39.     }
  40.  
  41.     /**
  42.      * Resets the throw count.
  43.      */
  44.     public void resetThrowCount() {
  45.         throwCount = 0;
  46.     }
  47.  
  48.     /**
  49.      * Rolls the 5 dice. Only roll dice that are not hold. Req: holds contain 5
  50.      * boolean values.
  51.      */
  52.     public void throwDice(boolean[] holds) {
  53.  
  54.         // Iterates each item in values, and updates the element if the same index is
  55.         // false in the holds array
  56.         for (int i = 0; i < values.length; i++) {
  57.             if (!holds[i])
  58.                 values[i] = (int) (Math.random() * 6 + 1);
  59.         }
  60.  
  61.         throwCount++;
  62.  
  63.     }
  64.  
  65.     // -------------------------------------------------------------------------
  66.  
  67.     /**
  68.      * Returns all results possible with the current face values. The order of the
  69.      * results is the same as on the score board. Note: This is an optional method.
  70.      * Comment this method out, if you don't want use it.
  71.      */
  72.  
  73.     public int[] getResults() {
  74.         int[] results = new int[15];
  75.         for (int i = 0; i <= 5; i++) {
  76.             results[i] = this.sameValuePoints(i + 1);
  77.         }
  78.         results[6] = this.onePairPoints();
  79.         results[7] = this.twoPairPoints();
  80.         results[8] = this.threeSamePoints();
  81.         results[9] = this.fourSamePoints();
  82.         results[10] = this.fullHousePoints();
  83.         results[11] = this.smallStraightPoints();
  84.         results[12] = this.largeStraightPoints();
  85.         results[13] = this.chancePoints();
  86.         results[14] = this.yatzyPoints();
  87.  
  88.         return results;
  89.     }
  90.  
  91.     // -------------------------------------------------------------------------
  92.  
  93.     // Returns an int[7] containing the frequency of face values.
  94.     // Frequency at index v is the number of dice with the face value v, 1 <= v
  95.     // <= 6.
  96.     // Index 0 is not used.
  97.     public int[] calcCounts() {
  98.  
  99.         int[] frequencies = new int[7];
  100.  
  101.         Arrays.fill(frequencies, 0);
  102.  
  103.         for (int i = 0; i < values.length; i++) {
  104.             frequencies[values[i]]++; // For each element n in values, index n in frequencies gets incremented by one
  105.         }
  106.  
  107.         return frequencies;
  108.  
  109.     }
  110.  
  111.     /**
  112.      * Returns same-value points for the given face value. Returns 0, if no dice has
  113.      * the given face value. Requires: 1 <= value <= 6;
  114.      */
  115.     public int sameValuePoints(int value) {
  116.  
  117.         int[] frequencies = calcCounts();
  118.    
  119.         // Finds the amount of occurrences for 'value' and multiplies it by itself
  120.         // in order to calculate the sum
  121.            
  122.    
  123.         return frequencies[value] * value;
  124.     }
  125.     /**
  126.      * Returns points for one pair (for the face value giving highest points).
  127.      * Returns 0, if there aren't 2 dice with the same face value.
  128.      */
  129.     public int onePairPoints() {
  130.  
  131.         int[] frequencies = calcCounts();
  132.  
  133.         int maxPoints = 0;
  134.         // Calculates the pair sum if a elements in frequencies is equal or greater than 2
  135.         // If a new pair is found the sum is reset and summed again
  136.         for (int i = 1; i < frequencies.length; i++)
  137.             if (frequencies[i] >= 2) {
  138.                 maxPoints = 2 * i;
  139.             }
  140.  
  141.         return maxPoints;
  142.     }
  143.  
  144.     /**
  145.      * Returns points for two pairs (for the 2 face values giving highest points).
  146.      * Returns 0, if there aren't 2 dice with one face value and 2 dice with a
  147.      * different face value.
  148.      */
  149.  
  150.     public int twoPairPoints() {
  151.         int sum = 0;
  152.         int[] frequencies = calcCounts();
  153.         int pairsFound = 0;
  154.        
  155.         // For each pair found increase the sum by the given pair sum
  156.         // only return 'sum' if two pairs are found
  157.        
  158.         for (int i = 1; i < frequencies.length; i++) {
  159.            
  160.             if (frequencies[i] >= 4) // minor performance tweak, should be commented if values array's length is <5
  161.                 return 0;
  162.            
  163.             if (frequencies[i] >= 2) {
  164.                 sum += 2 * i;
  165.                 pairsFound++;
  166.             }
  167.            
  168.             if (pairsFound >= 2)
  169.                 return sum;
  170.         }
  171.  
  172.  
  173.         return 0;
  174.  
  175.     }
  176.  
  177.     /**
  178.      * Returns points for 3 of a kind. Returns 0, if there aren't 3 dice with the
  179.      * same face value.
  180.      */
  181.     public int threeSamePoints() {
  182.        
  183.         // If three of kind is found; return the sum of them
  184.         int[] frequencies = calcCounts();
  185.  
  186.         for (int i = 1; i < frequencies.length; i++) {
  187.             if (frequencies[i] >= 3)
  188.                 return i * 3;
  189.         }
  190.         return 0;
  191.     }
  192.  
  193.     /**
  194.      * Returns points for 4 of a kind. Returns 0, if there aren't 4 dice with the
  195.      * same face value.
  196.      */
  197.     public int fourSamePoints() {
  198.        
  199.         // If four of kind is found; return the sum of them
  200.         int[] frequencies = calcCounts();
  201.  
  202.         for (int i = 1; i < frequencies.length; i++) {
  203.             if (frequencies[i] >= 4)
  204.                 return i * 4;
  205.         }
  206.         return 0;
  207.     }
  208.  
  209.     /**
  210.      * Returns points for full house. Returns 0, if there aren't 3 dice with one
  211.      * face value and 2 dice a different face value.
  212.      */
  213.  
  214.     public int fullHousePoints() {
  215.  
  216.         int[] frequencies = calcCounts();
  217.        
  218.         boolean twoOfKind = false;
  219.         boolean threeofKind = false;
  220.        
  221.         // Iterates for each element in frequencies expect index 0, if frequencies
  222.         // if either 0,2 or 3 doesn't exist in any given element return 0 immediately
  223.         // if two of kind and three of kind is found return the dice sum
  224.        
  225.         for (int i = 1; i < frequencies.length; i++) {
  226.            
  227.        
  228.             if (frequencies[i] == 3)
  229.                 twoOfKind = true;
  230.             else if (frequencies[i] == 2) {
  231.                 if (threeofKind)
  232.                     break; // Very minor performance tweak
  233.                 threeofKind = true;
  234.             } else if (frequencies[i] != 0)
  235.                 break;
  236.            
  237.             if (twoOfKind && threeofKind) {
  238.                 int sum = 0;
  239.                 for (int j = 0; j < 5; j++)
  240.                     sum += this.values[j];
  241.                 return sum;
  242.             }
  243.  
  244.         }
  245.  
  246.        
  247.         return 0;
  248.     }
  249.  
  250.     /**
  251.      * Returns points for small straight. Returns 0, if the dice are not showing
  252.      * 1,2,3,4,5 (sorted).
  253.      */
  254.     public int smallStraightPoints() {
  255.  
  256.         int[] frequencies = calcCounts();
  257.         // If values is equal to 1,2,3,4,5 (sorted), frequencies must be equal to {0,1,1,1,1,1,0}
  258.         int[] smallStraight = new int[] { 0, 1, 1, 1, 1, 1, 0 };
  259.         if (Arrays.equals(frequencies, smallStraight))
  260.             return 15;
  261.  
  262.         return 0;
  263.     }
  264.  
  265.     /**
  266.      * Returns points for large straight. Returns 0, if the dice is not showing
  267.      * 2,3,4,5,6.
  268.      */
  269.     public int largeStraightPoints() {
  270.         int[] frequencies = calcCounts();
  271.         // If values is equal to 2,3,4,5,6 (sorted), frequencies must be equal to {0,0,1,1,1,1,1}
  272.         int[] largeStraight = new int[] { 0, 0, 1, 1, 1, 1, 1 };
  273.         if (Arrays.equals(frequencies, largeStraight))
  274.             return 20;
  275.  
  276.         return 0;
  277.     }
  278.  
  279.     /**
  280.      * Returns points for chance.
  281.      */
  282.     public int chancePoints() {
  283.         // Return the sum for all elements in values
  284.         int sum = 0;
  285.         for (int i = 0; i < 5; i++)
  286.             sum += this.values[i];
  287.         return sum;
  288.     }
  289.  
  290.     /**
  291.      * Returns points for yatzy. Returns 0, if there aren't 5 dice with the same
  292.      * face value.
  293.      */
  294.  
  295.     public int yatzyPoints() {
  296.  
  297.         int[] frequencies = calcCounts();
  298.        
  299.         // Iterates for each element in frequencies expect index 0, if frequencies
  300.         // if 5 exists in frequencies a yatzy has occurred
  301.        
  302.         for (int i = 1; i < frequencies.length; i++) {
  303.             if (frequencies[i] == 5)
  304.                 return 50;
  305.             if (frequencies[i] != 0)
  306.                 return 0;
  307.         }
  308.        
  309.         return -1; // In theory should't be necessary given 'values' is valid according to the Yatzy rules
  310.        
  311.  
  312.     }
  313.  
  314. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top