Cribbage hand counter

Add a reply

Posted by gnuvince 281 days ago:
INTRODUCTION

Cribbage is a card game where you need to score points to move your pegs forward on the board. Each player holds four (4) cards and a fifth one, called the starter, is a card that can be used by all players to score points.

There are a variety of ways to score points:

* Fifteens: Every combination of cards that adds up to 15 is worth 2 points.

* Pairs: Every pair -- two cards of the same value -- are worth 2 points. Three of a kind is thus worth 6 points, and four of a kind is worth 12 points.

* Straights: A sequence of 3 or more cards is worth one point per card.

* Flush: A player has a flush when the cards in his hands are of the same suit. A flush is worth one point per card. A flush scores 4 or 5 points (if the starter card suit matches the one in the hand.)

* "His nobs": If a player holds a Jack and that the suit of that Jack and the suit of the starter are the same, this is worth one point.

In cribbage, aces are low, they have the numerical value 1. All figure cards (Jacks, Queens and Kings) have the numerical value 10. Numbered cards have their own values, obviously. Numerical value is considered only when counting fifteens.

EXAMPLES

Hand: 5C, 6D, 7D, QH -- Starter: 6H

This hand is worth 10 points:
* Fifteens: 2 points (5C + QH)
* Pairs: 2 points (6D and 6H)
* Straights: 6 points (5C-6D-7D and 5C-6H-7D)
* Flush: 0 points
* His nobs: 0 points


Hand: AC, 2C, 3C, KC -- Starter: 2S

This hand is worth 20 points:
* Fifteens: 6 points (KC + 2C + 3C, KC + 2S + 3C, KC + AC + 2C + 2S)
* Pairs: 2 points (2C and 2S)
* Straights: 6 points (AC-2C-3C and AC-2S-3C)
* Flush: 4 points (the cards in the hand are all clubs)
* His nobs: 0 points


Hand: 2C, 4S, 6H, JD -- Starter: 8D

This hand is worth 1 point:
* Fifteens: 0 points
* Pairs: 0 points
* Straights: 0 points
* Flush: 0 points
* His nobs: 1 point (JD has the same suit as the starter card)


INPUT

A series of 5 cards separated by spaces is given on stdin and the program returns the value of the hand. The first four cards are the player's hand and the fifth card is the starter.

The card values are: A, 2, 3, 4, 5, 6, 7, 8, 9, T, J, Q, K
The suits are: C, D, H, S


Input : 5C 6D 7D QH 6H
Output: 10
Input : AC 2C 3C KC 2S
Output: 20
Input : 2C 4S 6H JD 8D
Output: 1
Input : 2C 4S 6H 8D TS
Output: 0


REFERENCES

http://en.wikipedia.org/wiki/Cribbage
http://www.cribbage.org/rules/rule1.asp
Posted by o0lit3 253 days ago:
This suggestion gets a thumbs up from me.
Posted by ddipaolo 241 days ago:
Looks nice and well-specified. Could have some interesting solutions, too. Thumbs up.
Posted by gnuvince 241 days ago:
Quick note, the second example is worth 18 points, not 20. Apparently, I can't add :-/
Posted by Dilb 241 days ago:
Sounds promising. Thumbs up.
Posted by mcarrier 241 days ago:
This makes for a great unconventional challenge. Card games have subtilities that most other games don't. A lot of cases to check out for while programming! :) I would definitely enter this one just to see how bad I can do. ;)

Add a reply