Cribbage Scoring

4

My brothers and I like to play Cribbage. Now, the scoring rules for Cribbage are somewhat complicated, and I often worry that I'm not counting my score correctly. Therefore, your job is to write a program that will correctly score my hand.

Scoring

There are two ways to score points in Cribbage: during pegging, and while counting the hands. For the purpose of this challenge, we will only be counting a hand. However, there is also another card which is cut from the deck, and contributes to both players' scores; it is known as the "starter". The card combinations which score are:

Fifteens: Any combination of cards which adds to 15 is worth 2 points. All face cards are valued at 10, while aces are 1.

Pairs: A pair of two cards is worth 2 points. Three and four of a kinds are scored as multiple pairs; for example, since there are three ways to choose 2 from 3, a three of a kind scores 6.

Runs: A run of three or more cards is worth the number of cards in the run. Note that queen-king-ace is not a run, as aces are low.

Flush: A flush happens when all four cards in a hand are of the same suit, and is worth 4. If this also matches the suit of the starter, it is worth 5. Note that having, say, three hearts in hand and starter of hearts is not a flush: all four cards must be in the hand.

His Nobbs: A jack of the same suit as the starter is worth 1 point. Don't ask about the name.

For more information on scoring, see here: http://www.bicyclecards.com/how-to-play/cribbage/

I/O:

Cards can be represented as either a string or list/tuple, consisting of their value followed by their suit. Suits are "H" for hearts, "D" for diamonds, "C" for clubs, and "S" for spades. Face cards are "J", "Q", "K", "A", for jack, queen, king, and ace respectively. Therefore, two ways an ace of spades could be represented are "AS" and ['A', 'S'].

Your program or function should take two values: The hand and the starter. These can be any sane data type. It should return a single number, which can also be in any legible representation.

Examples:

 Input                            ==>   Output (notes)
["8H", "7D", "7H", "6H"], "2H"    ==>   16 (4 ways to make 15 makes 8, a pair makes 10, and two runs of three makes 16)
["JD", "5H", "5C", "5S"], "5D"    ==>   29 (Highest possible score: four of a kind makes 12, 8 ways to make 15 makes 28, and His Nobbs makes 29)
["9S", "10C", "5C", "8C"], "KH"   ==>    7 (two ways to make 15 makes 4, and a run of three makes 7)
["AS", "2H", "3C", "8S"], "AD"    ==>    7 (pair for 2, run of three makes 5, and one way to make fifteen (all five cards) makes 7)

Scoring:

This is code golf, shortest answer in bytes wins.

Standard loopholes are forbidden.

Bolce Bussiere

Posted 2018-01-05T19:39:29.587

Reputation: 970

Question was closed 2018-01-05T19:47:31.197

Can we replace 10 with T on the cards for the purpose of making each exactly 2 characters long? – ETHproductions – 2018-01-05T19:46:52.103

4This is a very nice first question! But unfortunately, this question has been asked before, so I'm voting to close it. – James – 2018-01-05T19:48:01.160

@ETHproductions Sure, go ahead. – Bolce Bussiere – 2018-01-05T19:48:46.643

I really love the game and thought about this question before but found it was a duplicate. I then thought about taking 2 hands and finding the winning hand from the pegging. Not had time to follow up but if you like the idea then put something together in the sandbox and I'll be happy to contribute. – ElPedro – 2018-01-05T21:42:27.460

Btw, very well written challenge with a good description of the scoring. – ElPedro – 2018-01-05T21:55:34.060

No answers