r/dailyprogrammer • u/jnazario 2 0 • Oct 11 '17
[2017-10-11] Challenge #335 [Intermediate] Scoring a Cribbage Hand
Description
Cribbage is a game played with a standard deck of 52 cards. There are several phases or rounds to playing cribbage: deal, discard, play and show. Players can earn points during the play and show rounds. This challenge is specific to the show phase of gameplay only. Each player's hand consists of 4 cards and an additional face up card. During the show round, each player scores points based on the content in their hand (plus the face up card). Points are awarded for the following:
- Any number of cards that add up to 15 (regardless of suit) – 2 points
- Runs of 3, 4, or 5 cards – 3, 4 and 5 points respectively
- Pairs: 2, 3, or 4 of a kind – 2, 6 and 12 points respectively
- Flushes: 4 or 5 cards of the same suit (note, the additional face up card is not counted for a 4 card flush) – 4 and 5 points respectively
- Nobs: A Jack of the same suit as the additional face up card – 1 point
Note: cards can be used more than once, for each combo
Input Description
Your program should take an array of 5 cards, each card will be designated by a rank: 1 (Ace) – 10 and Q-K as well as a suit: Hearts, Clubs, Spades and Diamonds. The first 4 cards are the cards in your hand and the final card is the additional face up card.
Output Description
Your program should output the score of the hand
Sample Input data
- 5D,QS,JC,KH,AC
- 8C,AD,10C,6H,7S
- AC,6D,5C,10C,8C
Sample Output data
- 10 points (3 fifteens - 6, a run of 3 - 3, and a nob – 1)
- 7 points (2 fifteens – 4, a run of 3 – 3)
- 4 points (2 fifteens – 4)
Notes and Further Reading
Credit
This challenge was suggested by user /u/codeman869, many thanks! If you have a challenge idea, please share it in /r/dailyprogrammer_ideas and there's a good chance we'll use it
7
u/ironboy_ Oct 11 '17
Does an ace count as 1 (lowest) as well as highest?
5
u/lgastako Oct 11 '17 edited Oct 11 '17
Haskell
Edited to add test results for a bunch of hands from this thread:
4
u/ironboy_ Oct 11 '17 edited Oct 12 '17
JavaScript
I thought it made things easier to make real card objects and calculate all combos... After that the rules for scoring points were rather easy to implement.
Edited: After reading the challenge instructions I understand "cards can be used more than once, for each combo" as "if you have a run of 4 you automatically have two runs of 3 as well" etc. But not so according to the rules - only the longest possible run counts... So I changed that.
Edited again: Now handles double runs, triple runs and double double runs. This page explains it nicely: http://www.rubl.com/rules/cribbage-scoring-chart.html
Output
4
u/quantik64 Oct 12 '17 edited Oct 13 '17
C++11
card.h
cribbage.cpp
3
u/gandalfx Oct 11 '17
Python 3 using standard input/output
I mostly went for brevity over readability but I wanted to at least split it into functions.
3
u/Gprime5 Oct 11 '17 edited Oct 11 '17
Python 3.5
3
u/Working-M4n Oct 11 '17 edited Oct 11 '17
VB.NET
I'm a noob and couldn't figure out how to check combinations for 15's so I used /u/i3aizey 's solution; very slick, many thanks!
Code:
Output:
3
u/thestoicattack Oct 11 '17 edited Oct 11 '17
C++17. Since cribbage hands are 5 cards, I figured it was easiest to enumerate the 32 possible sub-hands, calculate the points for each, and add them up. There's a subtlety in that some runs and flushes get double-counted that way, which explains the somewhat strange choices for values in the score function.
The power set implementation is the classic recursive style. Also, there's like zero error-checking throughout, especially on inputs, and to make card representations uniformly two characters, I subbed 'T' for "10" as the shorthand for ten.
3
u/[deleted] Oct 11 '17 edited Oct 11 '17
Ruby
Feedback always appreciated. Creating a Cribbage object starts an input loop which will accept new cribbage hands until return is pressed on an empty line.
Update: Fixed an issue where runs where not being calculated correctly. Now handles all runs (doubles etc.) according to the rules here (runs can be 3,4,5,6,8,9 or 12 points)
Output (from irb)
3
u/gabyjunior 1 2 Oct 11 '17 edited Oct 12 '17
C
Gives the expected result for this post samples and the rules samples.
EDIT
Fixed issue computing adds up, detected checking the hand that gives the highest score possible, which is an interesting test case:
5H,5C,5S,JD,5D (29 points)
Results
3
u/mn-haskell-guy 1 0 Oct 12 '17 edited Oct 12 '17
A program which announces the score the way I do... if pairs are part of a double or triple run they are included in the points for the run and not announced separately as pairs.
E.g., for the card ranks:
2 2 3 3 4you would simply announce a double-double run and peg 16 points since a double-double run implies there are (exactly) two pair. Likewise a triple run is automatically 15 points (3x 3 for the runs + 6 for the three of a kind.)The guts of the logic is in the
score'function.Sample output:
Code:
3
u/[deleted] Oct 12 '17
C# This is my first time submitting. I need to work on my problem solving so any feedback is very welcome!
3
u/mn-haskell-guy 1 0 Oct 12 '17
Here's a Javascript solution:
Output:
3
u/lennyboreal Oct 12 '17
XPL0 on the RPi
Card hands from lgastako's Haskell program using command line: crib <crib.dat There are two discrepancies in the results. Some minor changes were made to XPL0 to accommodate this challenge (which are available upon request).
Example output:
3
u/JD7896 Oct 12 '17
Python 3.5
I've been looking for a place to use itertools.chain, so this was a pretty fun challenge.
3
u/[deleted] Oct 13 '17 edited Oct 17 '17
JavaScript GitHub feedback welcome
Firstly I just wanted to say a big hello to everyone on r/dailyprogrammer. I've only just become aware of this subs existence and wanted to jump straight in. So congratulations challenge #335 you're my first challenge!
Oh also TIL the array functions map, reduce, filter... and thoroughly abused them :)
u/between2spaces Inputs -> Results
I also ran u/gabyjunior's inputs as I note a few others have (u/ironboy_).
u/gabyjunior Inputs -> Results
Feedback is welcome.
UPDATE Thanks to u/mn-haskell-guy who picked up a scoring issue. Now corrected.
2
u/chunes 1 2 Oct 13 '17 edited Oct 15 '17
How about some Factor
Edit: it comes to my attention that double and triple runs are a thing, thanks to /u/mn-haskell-guy. Hence, my use of
membersto set-ify the hand leads to missing those extra potential runs. I'll try to fix it when I get around to it.2
u/Herpuzderpuz Oct 13 '17
Python 3.6, everything seems to be working except for runs as I dont really understand how they work. I'll look into it when I have some more time.
1
u/Qwazy_Wabbit Oct 14 '17 edited Oct 15 '17
C++
My solution. I chose to break the scoring out into individual functions, this means that the hand must get read multiple times and the code is slightly longer. I did this because I think it is more readable and frankly don't care about reading 5 cards multiple times.
Feedback welcome!
1
u/__dict__ Oct 14 '17
Just learning Ruby.
1
u/[deleted] Oct 14 '17 edited Oct 14 '17
Does the sample output data correspond to the sample input data? Because I can only see one fifteen and no runs in the first example, but apparently there are three of each.
Also, which card is the face up card for nobs?
Edit: If the face-up card is a jack, does that make it a nob?
1
u/[deleted] Oct 14 '17 edited Oct 15 '17
Java
Output
1
u/speedster217 Oct 14 '17 edited Oct 19 '17
Clojure
dp-335.core.clj
dp-335.core-test.clj
1
u/hobotbobot Oct 15 '17 edited Oct 16 '17
Scala.
Edit: updated to match the rules explained by mn-haskell-guy below.
1
u/LegendK95 Oct 16 '17 edited Oct 16 '17
Rust - new to the language, would love some advice :)
Edit 1: ran against gabyjunior's input - fixed small thing - added output as total score
1
u/PoetOfShadows Oct 16 '17
Kotlin:
1
u/Working-M4n Oct 17 '17
Reworked my submission to JavaScript
Live link on CodePen
1
u/gardeimasei Oct 21 '17 edited Oct 21 '17
C++11
ScoringACribbageHand.cpp
Tried a different spin on the solution design :)
1
u/lackhead Oct 30 '17
Python 3.6, using classes.
1
u/The_Acronym_Scribe Dec 05 '17
python
Sorry to everyone who reads this, I wrote this a little later than I usually am writing code and decided against going back and optimising. Any feedback would be greatly appreciated, thanks.
1
u/zatoichi49 Mar 14 '18 edited Mar 14 '18
Method:
Split the hand into two lists of values and suits, and create a separate list replacing A, J, Q, K with their numerical values. For nobs: concatenate 'J' with the last suit in the hand, and check if this string appears anywhere in the first four cards. For fifteens: take all combinations of 2, 3, 4, and 5 cards and count all those that add to 15, applying the multiplier (x2) to the final count. For pairs: count each instance of the values, and apply the multiplier (x count-1) to each instance. Return the total. For flush: take the set of all suits in the hand. If the set = 1, then it's a five-card flush. If not, check the set of the first four cards for a four-card flush. If it's neither, there's no flush. For runs: sort the hand by the values (A-K), adding any duplicates to a dictionary that will be used to calculate the multipliers. Check for any 3, 4 or 5 card runs. Take the length of the longest run, and multiply it by the count of any values that appear in the dictionary to get the total run score.
Python 3:
Output:
0
u/ironboy_ Oct 11 '17
Hm, after reading the rules I think the challenge is down right wrong and lacking in its description "cards can be used more than once, for each combo" (they can't for runs - only for 15:ths according to the rules). Also the rules have different scoring for "double runs" which aren't mentioned at all in the challenge.
0
u/[deleted] Oct 11 '17
[deleted] — view deleted comment