Assignment 1
Using classes
Submit before 11:30 Saturday January 20
Overview
For this assignment, you will continue your practice of using a predefined class call FancyDice.
Note: your code must work with the FancyDice class provided for this assignment. You will not receive any credit if your code works with a different version of this class.
Code writing and questions
- Download the zip folder for the dice class.
- Study the code in files dice.py and demo.py. Run the demo functions in demo.py and familiarize yourself with what they do and how they work.
- Using the demo2 function as a guide, write a function called boxcar_count. Instead of counting doubles for a standard pair of dice for the given number of trials, it should count the number of times that 12 is rolled and return that count.
- Imagine a pair of dice that produces a color word (e.g. 'red', 'green') instead of the roll sum (e.g. 2, 3, ... 12). Create a literal dictionary (e.g. color_table = { 2: 'blue', 3: 'red' ... ). Use your dictionary to write a function called color_roll, which returns a color (represented as a string) based on the roll of a pair of dice and your dictionary.
- Create a function called dice_race. It should take three parameters: 2 FancyDice objects and an integer (goal). Example parameter names could be dice1, dice2 and goal. The function should repeatedly roll the dice objects until at least one of them reaches or surpasses a running total of the goal (third parameter). After the goal is reached, the winner is the dice object with the highest total. It should return True if the first dice object wins or ties. If the second dice object wins, it should return False.
- Test your dice_race function by calling it multiple times with two newly created FancyDice objects. After each run, review their histories to ensure that the correct "winner" is being reported.
- Write a function called contest. It should take one parameter, an integer called num_trials. This function should repeatedly call your dice_race function with the following arguments: a FancyDice object consisting of 4 6-sided dice, a FancyDice object consisting of 6 4-sided dice, and a goal of 100. It should run the 'race' the number of times specified by num_trials and count the number of times that the dice in the first parameter wins (the 4 6-sided dice). Have it return the percentage of times that it wins (100 * win_count / num_trials).
- Run your contest function multiple times using different values for the number of trials. Determine how large the number needs to be to obtain reliably consistent percentages. At what point does your function take more more than approximately 10 seconds?
Deliverable
Create a text, word or pdf file that contains the following:
- A statement that summarizes your completion of the assignment. The statement should include any help you received or whether you discussed your assignment with others. Keep in mind that you may not copy code from others (or online resources) or allow others to copy your code.
- Your clearly labeled answers to explanations and questions
- A summary of the files included in the zip folder
- Demonstrations that show that your code works
Put your doc file and your py files in a folder, zip it, and submit it under assn1 on D2L. Check that your submitted zip file is complete.
Grading
The assignment is worth 5 points. Full credit will be awarded to complete, accurate and well presented submissions. Points will be deducted for the following:
- Missing requirements
- Inaccuracies (possibly excepting minor ones)
- Poorly written explanations