This assignment involves writing a program to play a dice game called Chance-it. The following sections describe the game, show a game, and begin a program design which specifies what you should do.
Because I am providing you an object file which was created on unix1, your final program can only be linked on unix1. So I recommend you do all of your development work on unix1. (I think it should compile and link on unix2-4, as well.)
The rules governing the play of Chance-it are described below.
Below is an example of a game of Chance-it to illustrate how the game is played. Use input is shown in bold for clarity.
Welcome to the game of Chance-it!!
You will be playing against the computer:
Please enter a seed for my random number generator (0 for random seed):
3
Please select a strategy for the computer to use:
1. Computer strategy 1
2. Computer strategy 2
Which strategy do you select? 2
Computer will use strategy 2, good luck!
You rolled a: 3
You have already rolled:
3
Would you like to chance it? (Y or N) y
You rolled a: 2
You have already rolled:
2 3
Would you like to chance it? (Y or N) y
You rolled a: 9
You have already rolled:
2 3 9
Would you like to chance it? (Y or N) n
-----*****-----
Computer rolled a: 6
Chance-it! computer rolled a: 10
-------------------------------------------------------------------
|Round | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 |Total|
|Human | 9 | | | | | | | | | | 9 |
|Computer | 10 | | | | | | | | | | 10 |
-------------------------------------------------------------------
Hit enter to continue:
You rolled a: 6
You have already rolled:
6
Would you like to chance it? (Y or N) y
You rolled a: 9
You have already rolled:
6 9
Would you like to chance it? (Y or N) n
-----*****-----
Computer rolled a: 3
Chance-it! computer rolled a: 6
Chance-it! computer rolled a: 6
Computer LOST on Chance-it
-------------------------------------------------------------------
|Round | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 |Total|
|Human | 9 | 9 | | | | | | | | | 18 |
|Computer | 10 | 0 | | | | | | | | | 10 |
-------------------------------------------------------------------
Hit enter to continue:
You rolled a: 3
You have already rolled:
3
Would you like to chance it? (Y or N) y
You rolled a: 12
You have already rolled:
3 12
Would you like to chance it? (Y or N) n
-----*****-----
Computer rolled a: 11
-------------------------------------------------------------------
|Round | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 |Total|
|Human | 9 | 9 | 12 | | | | | | | | 30 |
|Computer | 10 | 0 | 11 | | | | | | | | 21 |
-------------------------------------------------------------------
You probably get the idea, so I’ve cut out rounds 4-10.
10 rounds are completed, and the final results are:
-------------------------------------------------------------------
|Round | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 |Total|
|Human | 9 | 9 | 12 | 10 | 0 | 8 | 9 | 0 | 8 | 8 | 73 |
|Computer | 10 | 0 | 11 | 10 | 6 | 7 | 8 | 7 | 8 | 12 | 79 |
-------------------------------------------------------------------
The computer wins with a Grand Total of: 79
You have only a measly: 73
There are many ways to break down the problem of writing this program into sub- problems. Below is a description of a number of functions you MUST have. You are strongly encouraged to test each function individually before combining with other functions.
Roll the dice
The dice will consist of two randomly generated numbers between (and including) 1 and 6. By adding these numbers together you get the value for each roll.
You must define this function with a prototype of: int roll(void);
For this task, you will need to randomly generate a two numbers between 1 and 6 and add them together. To do this, you can use the c-library function rand(). This function returns a random number between 0 and RAND_MAX, which is a very large number. You’ll need to scale this number down to a value between 1 and 6. Hint: The mod operator (%) will be helpful.
Below is an example usage of the rand() library function. Notice the use of the srand() function. This function "seeds" the random number generator and must be called once before you call rand(). You only need one call to srand() at the very beginning of the program. (Don’t call srand() each time you roll!)
Using the same seed value allows you to get the same sequence of random numbers from rand(), which frequently makes testing programs that use random numbers easier. Play with the function below and try different seed values to see what I mean. When you want your program to truly behave randomly, you can seed the random number generator with the current time. (see example below).
#include stdio.h
#include stdlib.h /* Needed for rand() and srand()
*/
int main(void)
{
srand(1440); /* seed random # generator */
printf(“%dn”, rand())
printf(“%dn”, rand())
printf(“%dn”, rand())
return 0;
}
Example using the system time to seed the random number generator for truly random behavior.
#include stdio.h
#include stdlib.h /* Needed for rand() and srand()
*/
#include time.h /* Needed for time() function. */
int main(void)
{
srand(time(0)); /* seed random # generator with
current time. */
printf(“%dn”, rand())
printf(“%dn”, rand())
printf(“%dn”, rand())
return 0;
}
Player's turn
You must define and use a function with a prototype of: int playerTurn(void);
This function performs the human player’s functionality and returns the value for the player’s turn (either 0 or the value of the last roll.)
This function must roll the dice, keep track of which roll this is, and the values of all previous rolls for this turn (use an array). The player should be prompted if they wish to “chance it” or not. If the value of a roll equals the value of a previous roll, the score for this turn is 0 and the task is completed. If the user does not wish to chance it, the score for this turn is the value of the last roll. This function should return the score for this turn.
Computer’s turn
The computer’s turn will be guided by different strategies. One strategy will be provided for you in a separate object file. Another strategy is defined below which you must implement. The strategy functions roll the dice, keep track of which roll this is, and the values of all previous rolls for this turn (again, use an array). However, there is no prompt about chancing it or not. Instead, the strategy determines if another roll should be taken.
Predefined Computer Strategy (written by your professors)
Prototype: int computerStrat1(void);
The object file containing this function is called strategy1.o and the header file is called strategy1.h. These files are located on unix1 in:
~phatalsk/www/101/projects/chance-it
This function uses your roll() function to randomly roll the dice. It continues to roll the dice until a 10, 11, or 12 is returned.
Computer Strategy you write
Prototype: int computerStrat2(void);
You are required to implement the following strategy. In this strategy you will re-roll and continue to re-roll the dice based on a certain probability that is determined by the last roll.
Your function must implement the following strategy:
The following example code has a 40% chance of printing “below 40%”:
int randomNumber = rand() % 100;
if (randomNumber < 40)
printf("below 40%");
else
printf("in the upper 60%n");
Print Game Board
This function should print the game board showing the values of each round for each player, and the current totals. Note that for rounds not played, no numbers show up.
Main
The main() function, as usual, acts as the ringmaster by coordinating the other functions together. For this program the main() should perform the following actions (using the functions above).
Files Given
Copy the all the given files into a subdirectory that you create for this project.
On unix1 use: cp ~phatalsk/www/101/projects/chance-it/* .
Note: the last character is a ‘.’ (a period)
The files should include:
Alternatively, the sample programs can be run with the following commands:
Using the strategy files (Needed for Part 2 of the project)
These files contain the strategy function we are providing. You must copy both the object file (strategy.o) and the header file (strategy.h) into your directory (see copy command above). Not that the .o file will only work on unix1.
To use the header file, use the following statement in both of your .c files:
#include "strategy.h"
To link this object file into your program use:
gcc -Wall -pedantic -ansi chance_util.c main.c strategy.o -o chance-it
Files You Will Write
Write the following functions in a file called chance_util.c:
Write a file called chance_util.h that contains prototypes for all the above functions.
Write the following code in a file called main.c:
int main() – Write a main that will test these functions. Write your main such that it mimics the behavior of my chance-test executable. You will need to #include “chance_util.h”. To compile the main code with your chance_util code, use the following command: gcc -Wall -pedantic -ansi chance_util.c main.c
For testing purposes you can diff your output with my test output. My test program is: ~phatalsk/www/101/projects/chance-it/chance-test
Your output for Part 1 will need to diff with my output for Part 1 exactly for your Part 2 to work. Test files for Part1: in1 out1
Complete the rest of the program! Do not begin this portion until you get your Part 1 diff-ing perfectly. Your professors will not help you with this part unless you can show us a perfectly working Part 1.
Add/complete the following functions to chance_util.c:
Add the prototypes for the above functions to chance_util.h.
Change your main() code to implement the full Chance-It game. To do so your program must successfully use the strategy.o file. Both of your code files (.c files) must #include “strategy.h”.
Think carefully about how you will store the human player’s rolls. Note that you must display them in numerical order. This program does NOT require you to sort the rolls if you make a smart design choice.
Make sure your output diff’s with the sample program
The sample program can be run with the following command: ~phatalsk/www/101/projects/chance-it/chance-it-full
Verify that your code meets the style requirements.
Test your code. Here are SOME test cases. Make your own! in1 in2 in3 (Tie game)