CPS 6: Dice Games

Spring 1999

Due before your next lab meeting

For thousands of years people have enjoyed playing dice games. Now you and your computer are going to take playing dice to another level. In this lab you will, eventually, write a computer player for the dice game pig.  You will start by writing some functions that will help you understand the probabilities of certain events occurring.

Getting Started

Change into your cps006 directory and create a directory called lab2 using the UNIX command mkdir: mkdir lab2. Change into that directory and copy the following files from the professor's home directory using the UNIX command cp (do not forget the trailing "."): cp ~rcduvall/cps006/lab2/* .

If you type the UNIX command ls you should see the files listed below.

Rolling Two Dice

Complete the function  RollTwoUntilNum, that rolls two dice until the sum of the both rolls is a given number. This function should return the number of rolls thrown before the given number was reached. You can assume the number given will be between 2 and 12, i.e., you can use a 6 sided dice in your function.  For example, suppose you called RollTwoUntilNum with the parameter 6 and the first roll shows a 4 on one dice and a 2 on the other. Then RollTwoUntilNum should return 1.

After writing, saving, compiling, and testing your function, answer the following questions (in your README file):

  1. Why is it important that you sum both rolls instead of simply rolling one dice with twice as many sides?
  2. How would you write a program to verify your answer?
  3. Is it important that you use two different Dice variables (rolling each once) as compared to using one Dice variable (rolling it twice)?  Why or why not?

Getting Doubles

Next complete the function RollUntilDoubles, that rolls a pair of dice until they both show the same number (called rolling doubles).  The parameter given is the specific number that should show on both dice after they are rolled. Your function should return the number of rolls it takes to get doubles. You can assume the number given will be between 1 and 6, i.e., you can use a 6 sided dice in your function.  For example, RollUntilDoubles with a parameter of 3 would roll until both dice rolls showed 3.

After writing, saving, compiling, and testing your function, answer the following questions (in your README file):

  1. Why would you expect it to take longer to roll doubles that sum to a given number?
  2. What changes are necessary for this function to look for any set of doubles? Is a parameter still necessary in this case?

Finding the average number of rolls

Complete the function AvgRollsToDoubles, that computes the average number of rolls it takes to find doubles for a given number. The first parameter is the number we are looking for on the dice and the second parameter is the number of times over which to average.  You can assume the first parameter given will be between 1 and 6. You must use the function RollUntilDoubles that you just wrote. For example, calling AvgRollsToDoubles with the parameters 2 and 1,000 should roll the dice until they show double twos 1,000 times and compute the average number of rolls it took to get the doubles.

UNIX: Removing files

Do not remove any of your CPS 6 C++ programs until the end of the semester. However, the executable files created when you compile a program take up lots of space, so they should be removed.

List the files in your lab2 directory, by typing the UNIX command ls. To see their size, type the same command with a flag: ls -ls. You will see something like:

   8 -rw-r--r--   1 rcduvall staff       3456 Feb  2 04:31 Makefile
 126 -rwxr-xr-x   1 rcduvall staff      64196 Feb  2 04:31 pig
   4 -rw-r--r--   1 rcduvall staff       1540 Feb  2 04:31 pig.cc
At the right end of the line is the name of the file. Moving to the left from this end, you will see the time and date the file was created. The number to the left of the month (1540 for pig.cc above) is the number of characters in the file. Note that your C++ program "pig.cc" is much smaller (has fewer characters) than the executable file "pig". Another size indicator is the first number on each line. This is the number of "blocks" of space a file is taking up. 1 block is approximately 512 characters. (pig.cc above takes up 4 blocks)

Type the command quota and you will see the number of blocks you are allowed to have (your quota), and the number and percentage you are currently using. You will also see how much space is left on the disk (or partition) where your files are stored. If the partition fills up, then you can't create any new files.

You do not need to leave your executable files around since they take up a lot of space, and you can always recreate them by compiling. You should make it a habit to delete an executable after you have a program working correctly. To remove a file, type "rm filename". For example, now type rm pig to remove the pig executable file. Once you remove a file it is gone, so be careful deleting files.

For this part of the assignment, show one of the lab TAs the following.

  1. Move to your lab1 directory.
  2. List the files in your lab1 directory.
  3. Remove all the executable files in your lab1 directory.
  4. Show how to "recreate" a file named "eight.html"
  5. Remove the file "eight.html"
  6. Move back to your lab2 directory.

Playing Pig

A popular dice game is Pig, whose object is to be the first player to score 100 points by rolling two dice and adding their sum to your score.  The game is called Pig, because the player can roll as many times as she wants in one turn but, if she gets greedy she could lose everything. There are risks with each roll of the dice.

The rules to Pig are as follows:

  1. The object is to accumulate 100 points before your opponent.
  2. Points are the sum of the numbers showing on the two dice.
  3. On your turn, you may roll as many times as you like, but
You are to write a function, PigRound,  that will play one round of the game Pig. You will receive as input the number of points gained so far by the player and return the number it has gained as of the end of the round. There are four cases in which the function will end:
  1. Losing your points for the round by rolling a one on one of the dice.

  2. For example, suppose the player has gained 67 points so far in the game. PigRound is called with the value 67. Now, suppose the player rolls and gets a one and a five. The turn should end and return a score of 67.
     
  3. Losing all your points by rolling a one on both of the dice.

  4. Suppose again the player has gained 67 points. PigRound is called with a value of 67 and a one is rolled on both dice. The turn should end and return a score of 0.
     
  5. Winning by gaining 100 points.

  6. Suppose PigRound is called with 90 points and the player rolls a six and a five; the turn should end and return 101.
     
  7. Choosing not to be a pig

  8. The player has rolled and gained some points and voluntarily stops its turn instead of risking another roll in which it may lose points. Again, suppose PigRound is called with a value of 67. The player rolls a 13, then a 7, and then decides that is enough. The function should return 87.
The key to winning the game is to know when to stop rolling the dice. See if you can make your player smart enough to regularly increase its score. A player that decides to only roll once, no matter what the result, will probably not reach 100 points very quickly. On the other hand, a player that wants to roll 100 times will probably lose its points every turn. So to make your dice player a winner, choose a strategy that rolls a variable number of times depending on the number of points gained in the game and in the round.

After writing, saving, compiling, and testing your function, answer the following questions (in your README file):

  1. State in English your strategy for playing a single round.
  2. How many points do you think is won in a "good" round?
  3. What is the maximum number of points you scored in a single round?
  4. What is the minimum number of rounds it took you to reach 100 points?