Programming Practice: Dice

In this exercise, you will write C code to roll a pair of dice. It will give you practice with the basic concepts learned in the first week of this course. If you need any help or want me to check your work, email me at lworthin@cs.princeton.edu. Normally, when you start working on a programming assignment, you should read all the directions before starting to write any code. However, for this exercise, it's fine if you want to work as you read.

The main part of your program should be a loop in which the dice are repeatedly rolled. After each roll, you should prompt the user with, "Do you want to continue?" The user should answer 'y' or 'n' (for yes or no.) I'd recommend using a 'while' loop for this purpose. (Think about why you might choose a 'while' loop instead of a 'for' loop.)

Besides the 'main' function, you will also have to write several others:

Write a 'prompt' function that will return 1 (for continue) or 0 (for quit) as a result. To determine whether or not to continue, the user should be prompted.

Write a 'roll' function. When called, it should print the result of rolling the dice to the screen.

Now, we're going to create a couple of 'helper' functions for our roll function. These functions will simplify the roll function code and make our program more modular. (more on that later on in the course.)

One function will be called 'getRandom.' It will randomly return a number between 1 and 6.

The second function will be called 'showDice.' It will print out (to the screen) the values of the two dice. In the first version of our program, we'll just print the number values like this: "3, 6" In the final version, we'll try to print a more graphical version, like this:

|*    | |*   *|
|  *  | |*   *|
|    *| |*   *|

 

By breaking up the problem of writing a program into smaller pieces, you will greatly reduce the degree of difficulty.

Now, we have five functions to write. It's up to you where you want to start.


main function

A good way to start is with writing "pseudo-code." Pseudo-code is like shorthand for a programming language. It's halfway between an English description of the problem and actual C code. Writing pseudo-code is as important to programming as creating an outline is to writing a paper for your English class. (my opinion.)

Take a separate piece of paper now and try to outline your code with some pseudo-code. There's no 'right' way to do this - you should develop your own style. Here's what mine looks like.

You can see that it's pretty simple to write pseudo-code when you've already figured out which other functions. When you haven't been told which functions to write, pseudo-code can help you figure out that step. For example, for this program you might have determined that you wanted to write a roll function after you wrote some very simple pseudo-code.


prompt function

In the "hello world" exercises, you were asked to write C code using the printf and scanf functions. We're going to need those library functions to write the prompt. Why do you think we might want to write this portion of the program as a separate function instead of just writing the following code in our main() function?
printf("Do you want to continue? ");
scanf("%c",&response);

An answer


roll function

What kind of function should this be? Should it take any parameters? If so, how many? Which types? [answer]

After determining the structure of a function, you can think about how you're goint to write it. Again, by writing pseudocode you should be able to see how our two 'helper' functions will make this part easier.


getRandom function

In the first couple of lectures, you saw sample code that generated random bits. (See slides 2.9 - 2.12). This is a good place to look for hints. Also, you should read about the 'random number generator' library function in a C reference book.

One operator that you will need to use is the 'mod' operator: %. It's a very useful operator for this type of problem. Basically, you can think of it as the 'remainder' operator. a % b is equal to the remainder you get when you divide a by b. So, 17 % 5 is 2. The important observation to make is that the remainder of a division operation can never be bigger than the number you're dividing by.

If you're not a 'mathy' person, this may be the hardest function for you to write. You should definitely test your function by itself. This is always important. An easy way to do this is with a main function that looks something like this:

main ()
{
   int ret;
   int count;
   
   for (count = 0; count < 10; count++)
   {
      ret = getRandom();
      printf("The random number is: %d\n", ret);
   }
}

By calling your function 10 times with a loop, you can see whether your function is doing what it's supposed to. Run this test a few times until you're confident that getRandom() is correct. Also, if you are finding that you are generating patterns, you might want to try to modify your function to make it appear to work more 'randomly.'


showDice function

The easy version of this function (just printing the numbers) only requires one line of code. Write this version of the function and make sure the rest of your program works correctly before you try the more difficult, graphical version.

To draw the dice graphically, you should draw the dice 'dots patterns' on paper first. You should think about whether 'helper' functions can help you write the showDice function. You may be tempted to write a function for each die value, like: drawOne(), drawTwo(), drawThree(), etc. However, you will quickly realize that this won't work.

The fundamental difficulty in drawing the dice one at a time is that the computer always prints to the screen from left to right. So, if you draw the dice on top of each other, that's fine. However, as illustrated above, you should draw the dice side-by-side. To do this, you may want to write functions like: drawTopRow(), drawMiddleRow(), and drawBottomRow(). What parameters should these functions have?


some finishing details

test, test, test.

It may not seem like it in an introductory course (when you're usually happy if it runs at all), but testing is a very important part of programming.

If I receive email about this exercise, I'll write the common bugs here.

For now, one thing I can think of that you might forget is to include the libraries at the beginning of the program. You will need them for the printing and random library functions.

Also, you need to make sure that (in your file), you write any function before (above) you call it.

Be careful about the difference between '=' and '==.'

Make sure you initialize all variables before you use them.

Make sure you update the values of the necessary variables within loops.