Custom Dice

Line: Method Line
Medium Difficulty Methods Learning Outcome One Exercise
Type: Exercise
You should have completed:

Monthly Calendar

This topic leads to:

This is the end of the line.

Summary

You are probably familiar with the traditional six-sided (cube) dice, which have either pips or dots on the faces, or the numbers 1 to 6. These have been used in games since at least Roman times to provide the element of chance or luck.

Recently, more and more high-end boardgames have been using custom dice, and with numbers of sides other than 6. The sides of these dice can have one or more symbols on them to indicate success, failure or other effects. In this exercise you will start by simulating the standard dice (a.k.a. d6) and then go on to create an app that will simulate rolling a set of custom dice similar to those in this picture.

Star Wars Dice

Pseudo-random numbers can be generated by computers, and within the System library there is a class called Random. You can use the Next method on a Random object to get what appears to be a random sequence of numbers.

Task

  1. Create a console application.
  2. Create a method to generate a random integer between 1 and 6
  3. Write code in Main to call this method 10 times and output the result to the Console
  4. Test that your code does actually produce 1's and 6's as well as the numbers in between.
  5. Modify this method so that the upper limit of the number generated can be passed as a parameter.
  6. Create a 2-dimensional int array. This will be used to hold the details of our dice, each row of the array will be a different dice face, and each column will represent one of the icons on the face of the dice. For a simple d6 this would be int[,] d6 = new int [6,1], as we only have one icon, the 'pip'.
  7. Create a constant PIPS with the value of 0, and you can access the array as d6[faceNo,PIPS]. Initialise the array with the following data.
    indexPips
    01
    12
    23
    44
    45
    56
  8. Change your method to use this array to look up the number of pips on a dice face, rather than calculating it. E.g. generate a random number rN between 0 and 5 then return the value by looking up d6[rN,PIPS]. This is similar to the way you converted an integer to a month name in the Monthly Calendar.
  9. Test this method and check it is still returning 1 through 6 randomly.
  10. Now change your code, or create a new method to handle this look-up table (this is the green dice in the picture above) Consider that you may want to add some new constants for the columns.
    indexPips Hit Advantage
    00 0 0
    10 0 1
    20 0 1
    30 1 0
    40 1 0
    5 0 1 1
    6 0 0 2
    70 2 0
  11. How could you get more than one return value from your method?
  12. Test that you are getting the correct range of results from your program.

Questions

  1. Can you extend this program to ask the user how many d6 and how many 'green' dice they want to roll, then print the total rolled? Hint: Something like "You rolled a total of 7 plus 1 hit and one advantage"
  2. How could you add a third icon type to the dice, or have a dice which has both numbers and symbols on its faces?