Skip to content

Project 1: Poker Dice

Poker dice

Introduction

Poker Dice is a dice game similar to Yahtzee played with five 6-sided dice with faces '9', '10', 'J', 'Q', 'K', and 'A'. A playable online version for learning the game is here: Poker Dice Calculator.

Each possible face has an associated numeric value:

Face Value
'9' 9
'10' 10
'J' 10
'Q' 10
'K' 10
'A' 11

The following table represents a summary of the rules for Poker Dice scoring:

Type# Category Description Scoring
1 One Pair A pair of matching faces:
9, 9, 10, J, K
Sum of highest matched values
2 Two Pair Two distinct pairs of matching faces:
9, 9, 10, 10, K
Sum of both pair values
3 Three of a Kind At least three faces are the same Sum of three matched values + 10
4 Four of a Kind At least four faces are the same Sum of four matched values + 20
5 Five of a Kind All five faces are the same 100 points
6 Full House 3 of a kind and a pair(distinct faces):
9, 9, 9, A, A
50 points + sum of dice values
7 Small Straight 4 or more faces in a row:
9, 10, J, Q
10, J, Q, K
J, Q, K, A
70 points
8 Large Straight All 5 dice sequential
9, 10, J, Q, K or
10, J, Q, K, A
95 points
9 Chance Any roll Sum of dice values

The final column in the table describes the score if the player chooses the corresponding score type. For example, if a player selects One Pair and the roll is ['9', '9', 'J', 'A', 'Q'], the score will be 18, because score is twice the pair value. Note: For pair scoring highest pairs should be counted. If the dice rolled are ['9', '9', 'Q', 'A', '9'], then the score will still be 18, because no extra points are given for a three of a kind since One Pair was the selected score type. Failed scoring matches should return a 0

Your goals for this assignment are to create a dice module and to develop and test a function for determining scoring in Poker Dice.

Provided Code

Start with the following source files:

dice.py

The dice module will work with lists of die faces represented as strings. This module will need to work with not just our Poker Dice number of dice (5) but with any number of dice between 1 and 10. The module will contain a globally defined list, a globally defined dictionary, and four functions.

  1. FACES – This list must contain the possible faces in increasing order from '9' to 'A'.
  2. FACE_VALUES – This dictionary must map from the strings representing faces to the corresponding integer values. For example, 'A' must be mapped to 11.
  3. roll_dice – This function will take two parameters. The first will be an int for the number of dice to roll. The second parameter will be a random seed integer, with a default value of 0. (Note that if None is provided as the second argument, the current system time will be used.) This function will return a list containing n die faces. For example:

    >>> roll_dice(3, 10001)
    ['J', '9', '9']
    
    If the number of dice is out of range 1–10 then a single die of value '9' will be returned as a list of 1 item. If no parameters are provided, then a list of 5 dice values should be returned (having been generated using a seed of 0).

  4. are_valid(Updated 10/16) This function takes a dice parameter which is a list of 1–10 dice rolls. This function will check the length of the list to make sure it is between 1 and 10 and check if the values of all dice in the list are valid faces (as defined by the FACES list). Return True if all these conditions are met, False otherwise. If the dice parameter is None, consider it invalid and return False.

  5. add_values(Updated 10/16) This function takes a dice parameter which is a list of dice rolls. It will calculate and return the sum of the values of the dice in the list. If the dice list is invalid (see are_valid), return -1.

  6. num_faces(Updated 10/16) This function takes a dice_list parameter which is a list of dice rolls. It will also take a second parameter for the face being counted. The function returns a count of the number of times the indicated face shows on the dice in the list. For example:

    >>> num_faces(['Q', 'K', '9', '10', 'Q'], 'Q')
    2
    
    If the dice list is invalid (see are_valid), return -1.

    Don't use .count()

    You should write a for loop to accomplish this function. The autograder will disallow use of the list.count() method.

poker_dice.py

This module contains a simple terminal-based Poker Dice game. Once you have completed score_dice.py you can use this driver to try out your code.

This driver will call the dice.py module and use the roll_dice function to return dice rolls. It may not be very useful for systematically testing your solution. The main mechanism for testing your code for this project will be the test_score_dice.py file described below.

score_dice.py

Your main task for this assignment is to implement the score_dice.calculate_score function according to the provided documentation string. For full credit, your finished function must satisfy the following requirements:

  • A nested conditional should be used to distinguish between the different scoring types
  • Appropriate helper functions must be used to break the problem into smaller and more manageable pieces.
  • The add_values and num_faces functions from the dice module must be used.
  • Your code must make use of the named constants (FACES, FACE_VALUES) declared at the top of dice.py. Avoid the use of hard-coded literal values in your solution.
  • Your calculate_score function must have only one return statement.

test_score_dice.py

You must write unit tests for score_dice.py. Rather than writing one very long test_score_dice function, you should write several smaller tests to handle distinct cases. The provided file contains two testing functions to get you started.

Notice that the names of these functions describe the situations they are designed to test. For example, the name test_calculate_score_one_pair_no_match:

  • test - This makes it clear that this is a testing function
  • calculate_score - this is the function being tested
  • one_pair_no_match - this is the situation being tested.

You should properly document your tests and your score_dice.py file. Docstrings for test functions are generally not required.

Categories that you should test include (this is a template for your test_score_dice.py file):

  • Test score one pair no match
  • Test score one pair one match
  • Test score two pair no match
  • Test score two pair one match
  • Test score three of a kind match
  • Test score three of a kind no match
  • Test score four of a kind match
  • Test score four of a kind no match
  • Test score five of a kind match
  • Test score five of a kind no match
  • Test score small straight match
  • Test score small straight no match
  • Test score large straight match
  • Test score large straight no match
  • Test score chance match

Part A - Readiness Quiz and dice.py (30 points)

1) Readiness Quiz (10 points) in Canvas.

Before the deadline for Part A you should read this document carefully, then look at the starter code provided above. Once you have a clear understanding of the expectations for this assignment, complete the readiness quiz in Canvas (10 points). The grading for this quiz will be all or nothing: your score on the quiz will be 0 if you miss any questions.

If you do not successfully complete the readiness quiz, you cannot receive any credit for Part A or Part B.

2) You should also complete the dice.py file (20 pts) and submit this to Gradescope by the deadline for your section.

Be sure to test your dice module carefully for correctness AND style before submitting into gradescope; You will be limited to 10 submissions for Part A code.

Part B - Code (60 points score_dice.py and 10 points test_score_dice.py)

Upload your score_dice.py and test_score_dice.py file to Gradescope. You should not include poker_dice.py in your submission.

Before uploading your submission to Gradescope, be sure to complete the following steps:

  • Test your solution carefully.
  • Make sure that poker_dice.py works as expected with your finished code.
  • Review the course style guide to ensure that your code meets the style requirements.
  • Run flake8 and eliminate all warnings.
  • Review and update comments as needed.

You are limited to a maximum of 10 submissions into Gradescope for Part B.

PA1 Attribution

Provide a short statement describing any assistance that you received on this assignment, either from another student, an online source, an AI-enabled tool, or any other source.

Acknowledgments

This assignment was created by Alvin Chao. Some parts of the Poker dice description above are borrowed from the Poker Dice Wikipedia page. The Wikipedia article, and this assignment page, are licensed under the Creative Commons Attribution-Share-Alike License 3.0.