Program 5

Cribbage Scoring

due Thursday, April 28, 2005 by the start of your lab period

Lab Partners

Everyone is encouraged to work with one lab partner on this assignment. If you do work with a partner, submit one solution with both of your names on it. Also, please look at the class collaboration policy so that you know what is and what isn't allowed.

Data File Used For Testing

4 4686       // pair - 2 points
5 6T66J      // triple - 6 points
6 K66Q66     // four of a kind - 12 points
4 TQJ8       // run of 3 - 3 points
5 TQJ8T      // 2 runs of 3, pair - 8 points
6 TQJ8TT     // 3 runs of 3, triple - 15 points
7 TQJ8TTQ    // 6 runs of 3, triple, pair - 26 points
2 69         // 1 15 - 2 points
8 66669999   // 2 four of a kinds, 16 15s, 56 points
13 789654TJQK32A       // ??
1 A          // 0 points

Purpose

The purpose of this assignment is to give you experience with both sorting and recursion.

Introduction

Cribbage is an old card game, usually played by 2 people. Points are scored by having various card combinations in a hand. In Cribbage, many of these combinations are similar to other card games, two-of-a-kind, three-of-a-kind, four-of-a-kind and runs (numerically sequential cards of 3 or more). But also in Cribbage, combinations of cards that add up to 15 are important. (Face cards are counted as 10 points, and aces are worth 1 point).

In our version of Cribbage, we will be "dealt" n cards from a standard 52 card deck. The number n will be at least 1 and no greater than 13. It is the job of your program to calculate the score for the n cards.

For our purposes, we will not use card suits, and we will represent the cards as single characters, as follows: Ace=1, Two=2, Three=3, Four=4, Five=5, Six=6, Seven=7, Eight=8, Nine=9, Ten=T, Jack=J, Queen=Q, King=K

Scoring
Two-of-a-kind (a pair): 2 points
Three-of-a-kind (3 pair): 6 points
Four-of-a-kind (6 pair): 12 points
Runs (sequentially numbered cards of length greater than 2): 1 point per card in the run
Fifteens: 2 points for any distinct combination of cards that sum to 15

Example #1 (n = 5)
Hand dealt: 595T5
14 points (3 pair for 6 points, plus 4 15s for 8 points(10+5, 10+5, 10+5, 5+5+5))

Example #2 (n = 5)
Hand dealt: 62947
6 points (3 15s (9+6, 2+4+9, 2+6+7))

Example #3 (n = 5)
Hand dealt: 56788
14 points (2 runs of 4 cards for 8 points, a pair of 8s for 2 points, and two 15s (8+7,8+7) for 4 points)

Example #4 (n = 5)
Hand dealt: 45666
21 points (3 runs of 3 cards for 9 cards, 3 pair for 6 points, 3 15s for 6 points)

Your task is to write a program that reads a file of "dealt" cards, and returns the correct score for each dealing.

Input File Format

The name of the input file is cribbage.dat. The input file will consist of an unknown number of lines. Each line will contain the number n (an integer between 1 and 13 inclusive), a space and then the n "cards". All input characters will be of a valid card type, as explained above. Cards represented by alphabetic characters will be capitalized. There will be no spaces between card characters.

Sample Input File

1 K
2 K5
3 KJQ
4 1132

Required Output Format

Deal #1:
Hand: K = 0 points

Deal #2:
Hand: 5K = 2 points

etc.

Notes:

Grading

After the programs are submitted, Mike and John will make up a comprehensive cribbage.dat file in order to test for correctness.

What to Submit

E-mail your code to Mike Thiesen at mthiesen@gmail.com in one message before the due date. Zip your relevant BlueJ folder up so that you only need to send Mike one file. The subject of the message should be: CS221-xx program5 your-names. The xx is the number of your lab section. Late programs will not be accepted for credit. If you aren't finished by the deadline, submit whatever you have - partial credit is possible.