C.I.S. 1.5

Brooklyn College

Professor Langsam

 

Assignment #3

 

Write a complete C program which will simulate the playing of a game of dice. The program will do the following:

 

1.         The main program will ask the user to type in two integer values, each in the range from 1 to 6, into variables called die1 and die2. Bad data is to be rejected (except for the special combination you use to end the set of data). For example, if someone types in 3 and -9, the combination is to be rejected. The main program will print the numbers as soon as they are read in.

2.         The main program will send these two integer values to a function named outcome. The function will determine the outcome of using these two numbers, according to this scheme:

            a.         If the numbers add up to 5, 7, or 12, then the player wins, and the function should return an indication of this (use some integer to represent a win).

            b.         If the numbers add up to 2, 4, or 11, then the player loses, and the function should return an indication of this (once again, use an integer).

            c.         If the numbers add up to anything else, then the game is a draw and the function should say this (by way of an integer).

            When the function returns to the main program, the main program will use the value returned by the function to print an appropriate message, describing which of the three cases applies in this situation.

3.         The main program will then add 2 to the value on the first die and 3 to the value on the second die. However, if the new value would turn out to be more than 6, the new value should be adjusted to fit the range from 1 to 6. [For example, if the two old values were 5 and 2, the two new values would be 1 (5 + 2 - 6 = 1) and 5 (2 + 3 = 5).  If the two old values were 1 and 5, the two new ones would be 3 (1 + 2 = 3) and 2 (5 + 3 - 6 = 2).] The main program should print the two new values, adjusted if necessary, then call function outcome again with these new values. Once again, the main program will print the result of the function call.

4.         The main program is to keep statistics about the various results. At the very end, print these statistics. For example, keep track of how many times the original pair of dice gave a winner, a loser, or a draw. Do the same for the new pair. Keep track of the amount of double winners, double losers, etc.

5.         Finally, the main program will continue with step 1 with a new pair of dice. At step 1, if the user types in a special combination (you must determine what this combination is, and you must explain it to the person using the program), the program will halt.

 

Data:    3          2

            3          1

            3          4

            3          6         

            5          1

            5          6

            5          5

            5          2

               ...

            Make up a total of about 15 - 20 games. Make sure that you include each of the three possible winning sums; make sure that you include each of the three losing sums; be sure to have one set with two winners, one with two losers, and one with a win and a loss. Also include at least one set of invalid data. Be sure that the output of your program is clear enough to allow someone to follow the game. You may want to place all the data in a file.

 

Extra Credit:

Investigate the use of C's built in random number function. In addition to submitting the above, modify the program so that it plays a predetermined number of games by itself, without any user intervention.

 

 

 

 

 

hw3a_c.doc(10/25/94)