Problem Statement
Cribbage is a card game played with a standard 52-card deck. A hand in cribbage contains five cards, one of which is designated the starter card. A hand earns points as follows:
- Fifteens: Every distinct combination of cards that sums to fifteen contributes two points. For the purposes of summing, aces are worth one and face cards (jacks, queens, and kings) are each worth ten. All other cards are worth their numeric value.
- Pairs: Every distinct pair of cards of the same rank contributes two points.
- Runs: Every distinct run of at least three cards of consecutive ranks contributes a number of points equal to the length of the run. Aces are ranked below twos, and jacks, queens, and kings are ranked above tens, in that order. Kings and aces are not consecutive, so runs that wrap around are not permitted. Do not count a run if a longer run including the same cards is possible.
- Flush: A five-card flush (all five cards the same suit) contributes five points. A four-card flush (four cards of one suit and the fifth card a different suit) contributes four points if and only if the card with the non-matching suit is the starter card.
- His Nobs: A jack in the same suit as the starter card contributes one point, and is called "his nobs". Do not count the point if the jack is the starter card.
A hand is written as a
For example, the hand "4H,6D,5S,JH,6C" (quotes for clarity only) is worth 15 points.
- Three fifteens: 4H+5S+6D, 4H+5S+6C, and 5S+JH.
- One pair: 6D+6C.
- Two runs: 4H+5S+6D and 4H+5S+6C.
- His nobs: JH (starter card is 4H).
Create a class Cribbage containing a method score that takes a
Definition
- Class:
- Cribbage
- Method:
- score
- Parameters:
- String
- Returns:
- int
- Method signature:
- int score(String hand)
- (be sure your method is public)
Notes
- The letter 'A' means "ace", 'T' means "ten", 'J' means "jack", 'Q' means "queen", and 'K' means "king".
Constraints
- hand contains exactly fourteen characters, and has the format "
,..., ", where each is one of the characters {'A','2','3','4','5','6','7','8','9','T','J','Q','K'} and each is one of the characters {'C','D','H','S'}. - No card appears twice in the same hand.
Examples
"4H,6D,5S,JH,6C"
Returns: 15
The example above.
"5H,5S,JH,5C,5D"
Returns: 29
8 fifteens, 6 pairs, and his nobs for 16+12+1 = 29 points.
"JS,TH,JH,9H,QH"
Returns: 14
1 pair, 2 four-card runs, and a four-card flush for 2+8+4 = 14 points.
"7S,6H,7D,8C,8S"
Returns: 24
4 fifteens, 2 pairs, and 4 three-card runs for 8+4+12 = 24 points.
"AH,2H,6C,QH,KH"
Returns: 0
Runs may not wrap around and four-card flushes may not include the starter card.
"AC,5C,JC,9C,7C"
Returns: 10
A five-card flush.
"2D,6H,4C,5C,3C"
Returns: 9
A five-card run.
"3H,4H,QH,KH,7S"
Returns: 0
2-card runs do not count.
"JS,QD,KH,AS,2C"
Returns: 3
Runs do not wrap around.
"2H,3C,2C,3H,2S"
Returns: 8
3 of a kind.
"2H,2C,AH,2S,3C"
Returns: 15
Runs involving an ace.
"AH,4H,6H,3H,7C"
Returns: 2
A four-card flush does not count if it includes the starter card.
"TH,JS,JC,JD,9S"
Returns: 15
A jack of a different suit does not count as his nobs.
"4H,TH,AH,QS,KH"
Returns: 6
"AS,2S,3S,4S,5S"
Returns: 12
"7C,7H,AD,7S,7D"
Returns: 24
"3H,5S,4C,2D,3D"
Returns: 12
"KC,9D,3H,QC,8S"
Returns: 0
"6D,2H,AD,TH,JD"
Returns: 1
"4S,9H,6C,6S,5D"
Returns: 16
"TH,9D,7S,8D,6C"
Returns: 9
"4H,3H,2H,AH,5H"
Returns: 12
"9H,8S,TS,JC,8D"
Returns: 10
"AH,2H,3H,4H,5H"
Returns: 12
"3H,4S,5S,6S,7D"
Returns: 9
"5S,TH,JH,9H,QH"
Returns: 14
"8D,9D,TD,JD,QD"
Returns: 11
"2C,2D,JC,QC,KC"
Returns: 6
"2H,3H,4H,5D,6S"
Returns: 9
"AH,3H,4H,5H,7H"
Returns: 12
"AH,8H,9H,QH,KH"
Returns: 5
"TH,JH,QH,KH,9H"
Returns: 11
"AH,2H,2D,3H,4H"
Returns: 10
"AD,2D,JH,JS,JC"
Returns: 6
"AH,2H,2D,2S,3H"
Returns: 15
"AH,AD,AC,2H,3S"
Returns: 15
"KH,9H,TS,JS,QC"
Returns: 5
"3H,4H,5H,2H,9H"
Returns: 11