Cribbage is an old, but still popular, card game using a standard 52-card deck in which each player has a hand of 4 cards. There is also one card from the remainder of the deck that is flipped and used by all players - this card is called the "starter". There are two phases in each round of the game: the pegging phase and the counting phase. We will focus on the counting phase only. In the counting phase, each player will count the number of points they should receive from their 4 cards plus with the communal starter card. There are a variety of ways to score points in this phase of Cribbage: Pairs, Runs, Fifteen, Flush, and His Knobs. Each of these categories is described with examples below. Scoring can occur from any number of these categories and the total number of points includes points from all these categories. Each card has two properties: its suit and its rank. The suit is the red or black symbol on the card: Hearts, Diamonds, Clubs, or Spades. The rank is the value of the card's number or letter. Most cards have a number from 2 to 10 and their rank is equal to that numeric value. Some cards have a letter instead of a number; i.e. A, J, Q, or K. The A (ace) has a rank of 1,J (jack) has a rank of 11,Q (queen) has a rank of 12 , and K (king) has a rank of 13 . When dealing with Runs (see description below), these are the ranks to be considered to check for consecutive sequences. For example, 10, J, Q is a run of 3 consecutive cards based on these ranks. However, when summing to Fifteen (see description below), the face cards ( J,Q, and K ) have values of 10. It's important to remember that the rank of these cards is different for Runs than they are for summing to Fifteen. Examine the provided Card class to see the two different rank getter methods, getRunRank() and getFifteenRank(). Pairs Two cards with the same number/letter label are considered a pair and it is worth 2 points. If you have more than 2 cards with the same number/letter label, each pair would score. For example, suppose you have 2H (2 of Hearts), 2S (2 of Spades), and 2C (2 of Clubs) among your 5 cards. With these cards, there are three pairs: 2H2S,2H2C, and 2S2C. Each of those three pairs scores 2 points for a total of 6 points. Runs Three or more cards with consecutive ranks (remember that J,Q, and K have ranks of 11,12 , and 13 respectively for this scoring category) are considered to form a run. Only the longest run will count and will score points equal to the length of the run. For example, if there is 7,8,9, and 10 , it would score 4 points as a run of 4 cards. The runs of 3( and ) would not count since the run of 4 is the longest run. If there are multiple runs of the same length, they would all count. For example, if there is 7D, 8H,9C,10D, and 10H, there would be two runs of 4(7D,8H,9C,10D> and 7D,8H,9C, 10H> ) so both runs would score for a total of 8 points for the two runs. Sometimes there are three or four different runs and they would each score in the same manner. For example, if there is 7D,8D,8H,9S, and 9H, there would be four runs of length 3(7D,8D,9S>;;; and ) so the score from these runs would be 12 points. The above set of cards would yield the following 4 runs: Each of these runs would score 3 points for a total of 12 points for all the runs. Since the runs are difficult and complex to determine, we are providing you with a private helper method called isRun(Set set) which you are encouraged to use to help in calculating this scoring category. This method will return true if the given set represents a run of 3 or more consecutive ranked cards, and false otherwise. You still have to check if the given run is the longest and only score it if there is no longer run in the sequence. Hint: keep track of the longest run length and then only score the one(s) whose length is the longest. Fifteen Any combination of cards that add up to 15 will score 2 points. For example, 6 and 9 add up to 15 so that would score 2 points. Queen, Ace, and 4 add up to 15 and would also score 2 points. Remember that J,Q, and K are all worth 10 in this scoring category. A card can be used in multiple additions to fifteen. For example, if there is 2D, 2H, 5D, 8D, 8C, there would be multiple add-ups to fifteen 2D,5D,8D>,2D,5D,8C>,2H,5D,8D>, and ) so it would score 8 for this category of scoring. Flush If all 4 cards in the hand have the same suit, it is a flush worth 4 points. If the starter also has the same suit as the 4 cards in the hand, then it scores 5 points instead of 4. The above set of cards would score 4 points for a flush in the hand. (starter) The above set of cards would score 5 points for a flush in the hand including the starter card. (starter) The above set of cards would score 0 points since the 4 cards in the hand are not all the same suit. His Knobs If there is a Jack within the 4 cards of the hand and its suit matches the suit of the starter card, it is called "His Knobs" and is worth 1 point. (starter) The above set of cards would yield 1 point for "His Knobs" since the starter card's suit is Diamonds and the hand contains a Jack of Diamonds. The above set of cards would not yield a point for "His Knobs" since the starter card's suit is Spades and the hand does not contain a Jack of Spades. Important Reminders: - Aces represent (and have a value of) 1 when summing to Fifteen and for Runs - As explained above, Jacks, Queens, and Kings all have values of 10 when summing to Fifteen, but are distinct ranks when it comes to Runs (for example, 8, 9, Jack is not a run even though Jack has a value of 10 in the counting. 10, Jack, Queen is a valid run). To simplify the ranks of these face cards for runs, we give Jack a value of 11 , Queen a value of 12 , and King a value of 13. For summing to Fifteen, these face cards all have a value of 10. Examples of Scoring Totals 2pts Pair of Queens 3pts Run of 3(10H,JS,QC) 3pts Run of 3(10H,JS,QD) 2pts Fifteen (5S+10H) 2pts Fifteen (5S+JS) 2pts Fifteen (5S+QC) 2pts Fifteen (5S+QD) 1pt His Knobs (JS same suit as starter 5S) Total points: 17 2 pts Fifteen (AD+4H+JC) 2 pts Fifteen (AD+4C+JC) 2 pts Fifteen (AD+4H+KC) 2pts Fifteen (AD+4C+KC) 2pts Pair of Fours 1pt His Knobs (JC same suit as starter 4C) Total points: 11 Some of the categories of scoring are dependent on multiple combinations of cards, so we will use a Power Set to generate all the different possible combinations of cards so that we can examine each set in the Power Set to compute the total score. A Power Set means a series of sets that covers every possible combinations of element(s) from the original set, including an empty set. For example, from the set {1,2,3}, the Power Set would include all of the following sets: {1},{2},{3},{1,2},{1,3},{2,3},{1,2,3}, and \{\} . Note that for any set with n elements, its Power Set will contain 2n sets. In this example, there are 3 elements, so the Power Set contains 23=8 sets. Classes to Implement For this assignment, you must implement three (3) Java classes: Set, PowerSet, and Counter. Follow the guidelines for each one below. In these classes, you may implement more private (helper) methods if you want. However, you may not implement more public methods except public static void main(String[] args) for testing purposes (this is allowed and encouraged). You may not add instance variables other than the ones specified in these instructions nor change the variable types or accessibility (i.e. making a variable public when it should be private). Penalties will be applied if you implement additional instance variables or change the variable types or modifiers from what is described here. Set.java This class represents a simple collection that must be implemented with a singly-linked list. This class must work for the generic type T. The class must have the following private variable: - LinearNode
setStart (the front of the linked list) The class must have the following public methods: - public Set(): constructor - Initialize setStart to null - public void add(T element) - Create a new node containing the given element and add the new node to the linked list. The order does not really matter for a Set, so you can either add to the end or the start of the linked list for simplicity. Make sure you update the links properly and account for different cases (i.e. adding the first node to an empty list, adding a node to a list with other nodes, etc.) - public int getLength() - Return the number of items in the linked list - public T getElement(int i) - Returns the element stored in the ith node of the linked list (NOTE: remember that order does not matter, so this method is not being tested in the autograded tests but it may be very useful for you when implementing the Counter class and you need to extract individual elements from a Set within a loop). - public boolean contains(T element) - Returns true if the given element is found within the linked list; false otherwise - public String toString() - Returns a string containing each of the elements in the Set separated by a space PowerSet.java This class represents the Power Set from a given set. This class must also work with the generic type T. The class must have the following private variable: - Set T>[] set The class must have the following public methods: - public PowerSet(T] elements): constructor - Generate the Power Set from the given T array of elements - See the hints below (Power Set Generation) for help on how to approach this - Store the series of sets for this Power Set in the instance variable, set. - public int getLength() - Return the number of items in the array (the number of sets in the Power Set) - public Set getSet(int i) - Return the Set stored at index i of the array Power Set Generation Creating a Power Set might sound difficult but there is a recommended approach that simplifies the process. - A Power Set for a set of n elements will contain 2n sets. - To generate each of the 2n sets with the proper combination of elements, take the binary representation of each number from 0 to 2n1. Hint you can use Integer.toBinaryString (x) to convert an int, x, to its binary representation as a String. - Ensure that all the binary numbers have the same number of digits (they will need to have n digits) using padded zeroes at the front, i.e. 0001. - Loop through each binary number and through each digit (bit) of the binary number. Whenever the digit (bit) is a 1, add the item from the original set at the corresponding index into the proper set for the Power Set (see the Example table below and the resulting sets (below the table) that would be produced from each binary number) Example: Suppose the original set of elements is ["A", "B", and "C"]. Since there are 3 items, there will be 23=8 sets in the Power Set. Loop through 0 to 7 and take the binary representation and pad each with zeroes so they all have the same length (3). Next, loop through each of the padded binary numbers and look for 1's and add the elements to the corresponding sets. 000 Nothing is added, so this is the empty set. 0 001"C is added because there is a 1 in the 3rd position, so the item at index 3 from the original array is added. {CCm} 010Bn is added. {1B} 011 " B " and " Cn are added because there are 1 s at the 2nd and 3rd position. {B, " C"} 100 "A" is added. { "A" } 101 "A" and "C" are added. {A,,C} 110 "A" and "B" are added. { "A", "B" } 111 All three elements are added. {AA, "B", "C" } Counter.java This class will be used to calculate the number of points from a Cribbage hand. The class must have the following private variables: - PowerSet cardps - Card starter The class must have the following public methods: - public Counter(Card] hand, Card starter): constructor - Initialize the starter and use the PowerSet constructor to generate the Power Set of the cards from the hand (note that the starter card is already included in the hand array so the Power Set will be based on all 5 cards. - public int countPoints() - Calculate the number of points for the hand that was sent into the constructor. Use the Power Set, cardps, to do the calculations so that all the combinations are checked. Refer to the scoring explanations in the Introduction when implementing this method. It is recommended that you implement one or more private helper methods to help keep the code more organized and clean. For example, you may want private helper methods for each of the scoring categories, i.e. one for checking if a given Set sums to 15 , another one to check if the given Set forms a run of 3 or more consecutive cards, etc. public class Card \& private string suit; //s (spade). H (heart), D (diamond), or C(club) private string label; //A(ace),2,3,,16,J,(jack),Q (queen), or k (king) public Card (String suit, String label) \{ this, suit = suit; this. label = label; ? public string getsuit () \{ return suit; ? public string getLabel ( ) \ return label: ? public void setsuit (String suit) \{ this . suit = suit; ) public void setlabol (string label) f this. labeI = labe ; 7 public string tostring () return label + of a+ suit; ) f/ Get the rank value when it comes to runs (J,Q, and K are distinctl) public int getRunRank () \& if ( label. equals (A)) \& return 1; J else if (label.equals (J)}{ return 11; \} else if (label, equals (Q)) \{ return 12; \} else if (1abel.equals (K)) i return 13; b else. return Integer.parseInt ( label); 3 ) 1/ Get the rank value when it comes to fifteens (J,Q, and K are all worth 101) public int getfifteentank () \& if (label.equals (A)) ( return 1: return 19 ; 30150 f. return Integer.parseInt(label); ) 3 vate boolean isfun (SeteCard> set) \& II In this method, we are going through the given set to check if it constitutes a run of 3 or more II consecutive cards. To do this, we are going to create an array of 13 cells to represent the II range of card ranks from 1 to 13. We go through each card and increment the cell corresponding to II each card's rank. For example, an Ace (rark. 1) would cause the first (index 0) cell to increment. If An 8 would cause the 8 th (index 7) cell to increment. When this loop is done, the array will NI contain 5 or less cells with values of 1 or more to represent the number of cards with each rank. If Then we can use this array to search for 3 or more consecutive non-zero values to represent a run. int n m set.getLength(); if (n maxStreak) maxStreak = streak; lolse f streak =0; \} \} if (maxStreak ==n ) f if Check if this is the maximum streak. return true; Jelse \{ return false; \} LinearNode TestSetPowerSet public class TestSetPowerset \{ public static void main(String[] args) f / _._ Test 1 [Set getLength and contains] ___ / try \& Set sset = new Set(1); sSet. add( "spring"); sSet add( "sumner"); sset, add ( "autunn"); sset, add ( "winter"); if (sSet.getLength() =48s sSet. contains("summer") \&s IsSet. contains("fall")) ( System. out. println( "Test 1 Passed"); 3 else \{ System,out, println("Test 1 Failed"); \} \} catch (Exception e) \& System. out, println("Test 1 Failed"); \} try of SeteIntegers iSet = new set>(3; iset. add (14); iset.add (35); iset, add (9); iset.add (12); iSet-add (28); iSet. add (49); iset.add (17); String str = iSet. tostring(); if (iset,getLength() ==7 \&\& str.contains("14") \&s str.contains("12") \&\& str. contains("17") ss str.split(" "). length >=6)( System, out,println("Test 2 Passed"); \} else \{ System.out.println("Test 2 Failed"); ) j catch (Exception e) \& System+out, println("Test 2 Failed"); \} 100% TestSetPowerSet Character[ ] carr = new Character[ ]{ 'a', 'b', " c ' }; Powerset cps= new PonerSet (carr); int[] res = getMinMax(cps); if (cps.getlength ()=8 ss res [8]= ss res [1]==3) f System, out.println("Test 3 Passed"); 7 else \{ System.out.println("Test 3 Failed"); 3 \} catch (Exception e) \& System, out, println("Test 3 Failed"); 3 try Integer ] Larr = new Integer[ ]{5,1,9,3,7}; Powerset> ips = new Powerseteinteger >( iarr ); int [] res = getMinMax (ips); if (ips.getLength ()=328& res [0]==0 res [1]=5) \& System, out.println("Test 4 Passed"); 7 else \{ System.out.println("Test 4 Failed"); catch (Exception e) f Systen. out, println("Test 4 Failed*); 3 l_Test 5 [Powerset getSet] ___ */ try \& String[] sarr = new String[] fTin, "Bob", "Don"); Powersetestrings sps = new Powersetestrings (sarr); sps. getset (7).getLength ()=3) f System.out.printin("Test 5 Passed"); 7 else \& System, out,println("Test 5 Failed"); 3 j catch (Exception e) 1 System. out, println("Test 5 Failed"); b 3 private static int[] getMinMax (Powerset ps) \{ int tmpMin =99, tmpMax =1; Set set; int n; for (int 1=0;1tmpMax) tapMax =n; \} return nea int[\} \{tmpMin, tmpMax\}; 3 LinearNode