### Orientation

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.)

### Chance-it: Rules of Play

The rules governing the play of Chance-it are described below.

• A game of Chance-it involves two players. The player with the highest total score after all rounds is the winner. For this program, you are to have the computer act as player 2 and you are player 1. Also, for this program a game will consist of 10 rounds.
• During a round, each player takes a turn, resulting in a score for each player for the round.
• A turn for a player consists of rolling the dice one or more times. After each roll, the player must decide whether to take the value of that roll or Chance-it in hopes of getting a higher roll.
• If a player decides to stop, then their turn ends and their score for that turn is the value of their last roll.
• If, in re-rolling, a player rolls any previous roll of this turn, then their turn ends and their score for this turn is zero.

### Example Game

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
3
Would you like to chance it? (Y or N) y
You rolled a: 2
2 3
Would you like to chance it? (Y or N) y
You rolled a: 9
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
6
Would you like to chance it? (Y or N) y
You rolled a: 9
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
3
Would you like to chance it? (Y or N) y
You rolled a: 12
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

### Program Design

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:

• If the last roll <= 5 always reroll
• If (5 < last roll <= 7) then reroll with a 40% probability.
• If (7 < last roll <= 9) then reroll with a 20% probability.
• If (last roll == 10) then reroll with a 10% probability.
• If last roll equals 11 or 12 then do not reroll.

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).

• Determine which computer strategy to use by asking the user.
• Seed the random number generator used by the roll() function.
• Play 10 rounds of the game, where each round does:
• Player plays a turn, resulting in a value of the turn for this round.
• Computer plays a turn, resulting in a value of the turn for this round.
• Display the game board.
• Display who won and the final scores.

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:

• strategy.o – object files for the computer strategy (only works on unix1)
• strategy.h – header file with the strategy function prototype
• chance-test – part 1 executable – provides sample output for part1 (only works on unix1)
• chance-it-full – chance-it executable (only works on unix1).

Alternatively, the sample programs can be run with the following commands:

• ~phatalsk/www/101/projects/chance-it/chance-it-full
• ~phatalsk/www/101/projects/chance-it/chance-test

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"

gcc -Wall -pedantic -ansi chance_util.c main.c strategy.o -o chance-it

Files You Will Write

• main.c This is the file that will contain your main() C program that plays Chance-it.
• chance_util.c, chance_util.h These files contain the code for your functions and the prototypes for these functions.

### Part 1 – Do this first!!!

Write the following functions in a file called chance_util.c:

• int roll() – this function should return the sum of the two dice.
• int calcReroll(int givenProbability) - Write a function that will take in a given probability (like 10 for 10% or 20 for 20%) and returns true (1) if the calculated value is less than the given probability otherwise false (0). In other words, if you pass the function a value of 30, it has a 30% chance of returning true, and a 70% chance of returning 0.
• void printBoard(int ps[], int cs[], int round) - Write the function to print out the board. Since the game is not implemented yet, this temporary version of the function will print out zero’s for all scores. You do not need to process any arrays in the function for this part of the assignment. You will print out the values from the arrays when you implement Part 2 of the project. The ps array holds the player’s scores. The cs array holds the computer’s scores.

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

### Part 2

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:

• int playerTurn(void);
• int computerStrat2(void);
• void printBoard(int ps[], int cs[], int round) (Complete this function such that it prints the values from the arrays.)

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.

### Testing

Test your code. Here are SOME test cases. Make your own! in1 in2 in3 (Tie game)