Skip to content

Project 1: Chuck-a-luck

Chuck-a-luck score table

Introduction

Chuck-a-luck is a betting game played with three dice. The player puts their money down on one of the possible bets, then the dealer rolls the dice. The player collects a payout if the outcome matches the bet. A playable online version for understanding the problem is located here:

Your goal for this assignment is to create a dice module and develop and test a function for determining payouts in Chuck-a-luck.

Provided Code

Start with the following source files:

dice.py

The dice module will work with a list of 3 integer die values. This module will need to work with not just our Chuck-a-luck number of dice (3) but with any number from 1–10 dice. The module will contain four functions:

  1. roll_dice – This function will take two parameters. The first will be an int for the number of dice in the list. The second parameter will be a random seed integer, with a default value of 0. Note it is possible to pass None as a parameter, which should result in a random seed value. This function will return a list of n dice values (each die has an integer value of 1–6). If the number of dice is out of range 1–10 then a single die of value 6 will be returned as a list of 1 item. If no parameters are provided, then a list of 3 dice values should be returned (having been generated using a seed of 0).

  2. are_valid – This function takes a dice parameter which is a list of from 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 between 1 and 6. Return True if all these conditions are met, False otherwise.

  3. count_values – This function takes a dice parameter which is a list of from 1–10 dice rolls. It will also take a second parameter for the dice value being counted, with a default of None. The function returns a count of the number of times a particular value shows on the dice in the list. For example, if the dice are [1, 3, 3], dice.count_values(dice, 3) will return 2 (since the value 3 occurs twice). If no parameter is passed (i.e., None) then count should return 0. Also, if an invalid list or face value of None is passed the function should return -1.

  4. add_values – This function takes a dice parameter which is a list of from 1–10 dice rolls. It will calculate and return the sum of the face values of the dice in the list. Make sure you use the validation function for dice and return a -1 for an invalid list of dice.

driver.py

This module contains a simple terminal-based Chuck-a-luck game. This code includes all of the game logic except the payout function. Once you have completed chuck_a_luck.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 running your code for this project will be the test_chuck_a_luck.py file described below.

chuck_a_luck.py

Your main task for this assignment is to implement the chuck_a_luck.calculate_payout 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 bet types
  • Appropriate helper functions must be used to break the problem into smaller and more manageable pieces.
  • The add_values and count_values functions from the dice module should be used.
  • Your code must make use of the named constants declared at the top of the chuck_a_luck program. Avoid the use of hard-coded literal values in your solution.
  • Your chuck_a_luck.calculate_payout function must have only one return statement.

The following table represents a summary of the rules for Chuck-a-luck bets:

Roll TypeRulesPayouts
Single Die MatchA specific number will appear(1-6)1 die, 1 to 1; 2 dice, 2 to 1; 3 dice, 10 to 1
Any TripleAll three dice show the same number30 to 1
BigThe total score will be over 10 with the exception of a triple1 to 1
SmallThe total score will be under 11 with the exception of a triple1 to 1
FieldThe total score will be outside the range of 8 to 12 (inclusive)1 to 1

The final column in the table describes the payout if the player wins the corresponding bet. For example, if a player puts $5 down on single 3, and roll is [3, 3, 2], the payout will be $10 because the odds for that outcome are 2 to 1. If the dice rolled are [3, 3, 3], then the payout will be $50, because the odds for that outcome are 10 to 1.

test_chuck_a_luck.py

You must write unit tests for chuck_a_luck.py. Rather than writing one very long test_calculate_payout 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_payout_single_no_match:

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

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

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

  • Test payout single no match
  • Test payout single one match
  • Test payout single two match
  • Test Triples match
  • Test Triples no match
  • Test payout BIG match
  • Test payout BIG no match
  • Test payout SMALL
  • Test payout SMALL no match
  • Test payout field match
  • Test payout field no 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; however, you may submit as many times as needed.

Part B - Code (70 points)

Upload only your chuck_a_luck.py file to Gradescope. You should not include driver.py or test_chuck_a_luck.py in your submission.

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

  • Test your solution carefully.
  • Make sure that driver.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.

Honor Code

This assignment must be completed individually. Your submission must conform to the JMU Honor Code. Authorized help is limited to general discussion on Piazza, the lab assistants assigned to CS 149, and the instructor. Copying work from another student, person or the Internet is an honor code violation and will be grounds for a reduced or failing grade in the course.

Acknowledgments

This assignment is based on the original Chuck-a-luck designed by Nathan Sprague which was loosely based on a similar Yahtzee project developed by Chris Mayfield and others. Some parts of the Chuck-a-luck description above are borrowed from the Chuck-a-luck Wikipedia page. The Wikipedia article, and this assignment page, are licensed under the Creative Commons Attribution-Share-Alike License 3.0.