Assignment : Dice Simulation

Assignment Setup

To create your repository go here. Then follow the same accept/import process described in Assignment 0.

Dice Simulation

This assignment asks you to simulate throwing Dice.

Follow each step of the instructions carefully to develop your code by iterative refinement. After each concept is described implement it (create code for it) and test it before moving on to the next concept.

Procedure

The description below uses pictures to illustrate the concepts involved in this assignment, but you will not be displaying pictures. Instead your program will simulate throwing dice, display the corresponding numbers, and collect statistics of interest.

  1. You need two pieces of information from the user:

    1. How many dice will be used in this simulation?. For example, if three dice are being rolled a single roll could look like:
      Example Throw:

    2. How many times will the dice be rolled? For example, if we’ll do 5 rolls of our three dice, the sequence of rolls may look like:
      Figure 1

  2. Setup:

    • Create a class Dice in the assignment3 package.
    • Decide on meaningful variable names for the two concepts introduced so far and collect values from the user.
    • Test your code by printing out the values you have received.
  3. Now simulate a single roll of a single die
    • Assume we are working with 6-sided die. Each individual die will be represented by a number 1-6, which indicates the number of pips (dots) that the die would show.
    • Update your code so it simulates a single roll of a single die and prints the result.
    • Test your work.
  4. Now simulate rolling all the dice once.
    • Update your code and again print the results. For example, if you were trying to emulate the experiment shown in the first picture above and happened to roll the same values in your simulation, you’d see 3, 5, and 3 printed.
  5. Advice: For the rest of the assignment you need to collect some statistics (listed in the next parts). Try to do the following steps for each part:
    1. Think about how to collect the given information. It may help to think about how you’d solve this problem using paper and pencil. Imagine how you’d do each task if you were rolling 50 dice 10,000 times. (Thinking about big problems like this forces you to think carefully about the details of each step).
    2. Plan “test cases” you’ll use to confirm that each step works as expected.
    3. Create code to collect and display the given information.
    4. Test your work and revise if needed.
    5. Remove or comment out any code used purely for testing, like print statements that you used to help you follow what was going on, but that aren’t needed in the final program.
  6. You’ll want to print the sum shown on the die for each throw. The set of 5 rolls of 3 dice shown above would print:

    Throw 1: 3 5 3 = 11
    Throw 2: 1 1 6 = 8
    Throw 3: 2 1 4 = 7
    Throw 4: 5 1 2 = 8
    Throw 5: 6 6 6 = 18
    

    Try to match this format exactly.

  7. Next you’ll need a mechanism for tracking something we’ll call a “Yahtzee, in honor of the Milton Bradley game with that name. Milton Bradley’s Yahtzee is played with 5 dice. When all 5 dice show the same value it’s called a “Yahtzee”. We’ll use the same basic definition: When all dice show the same value it’s considered a Yahtzee. In the sample run shown above, Throw 5 would be considered the only Yahtzee (all three dice show the same value). Update your code so it identifies the percentage of throws that would be considered “Yahtzees”. It should print in the following format:

    Throw 1: 3 5 3 = 11
    Throw 2: 1 1 6 = 8
    Throw 3: 2 1 4 = 7
    Throw 4: 5 1 2 = 8
    Throw 5: 6 6 6 = 18
    Of the 5 throws, 1 showed the same value on all of the dice, which is 20.0% of all throws.
    

    Again, try to match this format exactly.

  8. Now you need to keep track of the number of times each sum occurred. For example, the sum 8 occurred twice in the previous examples (and sums of 7 and 18 each occurred once).

    This is the most challening part of this Assignment. There are many approaches that work. Try to think of at least two and consider the strengths/weaknesses of each. For example, do you think one would take more/less code than another or be easier to “get right”?

    • Again, think about how you’d track this information if you had 50 dice and were doing 10,000 rolls.
    • You definitely want to use array concepts here!
      • How would you store information in one or more arrays?
      • What are the ranges of values you may expect?
        • For 3 dice what are the smallest / largest sums?
        • For 4 dice?
        • For dice?
      • What data types (as in data types) are being stored?
      • What would the concept of an index be used for?
      • Where, when, and how would you update this information?
  9. When your program completes it should indicate how many times each sum occurred and the percentage of rolls for that sum, but only if it occurred at least once. For example, your final output for two dice thrown twelve times may look like:

    Throw 1: 6 5 = 11
    Throw 2: 5 1 = 6
    Throw 3: 6 4 = 10
    Throw 4: 6 3 = 9
    Throw 5: 2 5 = 7
    Throw 6: 4 4 = 8
    Throw 7: 6 5 = 11
    Throw 8: 5 2 = 7
    Throw 9: 1 2 = 3
    Throw 10: 2 4 = 6
    Throw 11: 2 6 = 8
    Throw 12: 4 3 = 7
    Of the 12 throws, 1 showed the same value on all of the dice, which is 8.3% of all throws.
    The sum 3 appeared 1 times, which is 8.3% of all throws.
    The sum 6 appeared 2 times, which is 16.7% of all throws.
    The sum 7 appeared 3 times, which is 25.0% of all throws.
    The sum 8 appeared 2 times, which is 16.7% of all throws.
    The sum 9 appeared 1 times, which is 8.3% of all throws.
    The sum 10 appeared 1 times, which is 8.3% of all throws.
    The sum 11 appeared 2 times, which is 16.7% of all throws.
    

    Again, try to match this format exactly (but if you’d like you can make it say “time” rather than “times” when something occurs once).

Submitting your work

To submit your work come to office hours or class on an “Assignment day” and sign up for a demo via 131list.com.