**Dice Game**

**Introduction to Procedural Programming**

GUTSY

GUTSY is a table game for two or more players. It aims at getting 101 points by throwing a dice

while overcoming the one dot rule, and not being so greedy. Each player, at a turn, can throw the

dice and accumulate the obtained points, but if s(he) gets one dot, the accumulated points are lost.

The rules are:

·At each player turn, the player can throw the dice as many times as s(he) wants and stops

when s(he) decides.

·Each time the dice is thrown, the points are accumulated.

·Once a player decides to stop, his/her accumulated points count towards the goal of 101 points.

The turn goes to the next player.

·One dot rule: If the player gets one dot, s(he) does not get any point (zero) to be accumulated

towards the goal, and the turn goes to the next player.

·The player who gets 101 or more points wins.

PROBLEM STATEMENT

Your Gutsy solution must play this game automatically and its statistics. We will need to collect

statistics as follows:

·Probability of getting one dot.

·Probability of getting two dots.

·Probability of getting three dots.

·Probability of getting four dots.

·Probability of getting five dots.

·Probability of getting six dots.

·Average number of times the dice is thrown to get a “defined number” (risk factor).

·Average number of times the dice is thrown to get 101 or more (to win).

Your program should make use functions for input, and statistics calculations. The following methods are suggestions, but you can use your own functions:

·The main() function that controls all of the others.

·A function, playGutsy, that prompts the user for the number of Gutsyplayers and the number

of games they will play. This function stores the results of the games and required statistics

in one or more appropriately-sized array(s). The computer will play the given number of

games without human intervention, but before, it will ask the user for information regarding

each player:

1. Name of player,

2. Minimum number to stop a turn on the dice (“defined number” – risk factor).

·A function that throws a dice several times and accumulates values in order to reach/pass the

“defined number”, and returns: the accumulated value or zero (in case a one dot is obtained),

number of times the dice was thrown, and counters required for the statistics. Suggested

function signature: void diceThrown(intdNumber, int *accumValue, int *numTimes, int*count1, int *count2, int *count3, int *count4, int *count5, int *count6).

**·A function that plays a turn for a player by using the previous function and accumulates the**

value towards the goal and statistics. This function returns if the player has won. Suggested

function signature: intplayerTurn(int player, intaccumValue, int *playerTotal, int

***numTimes101).**

**·A function that prints out the player name, defined number, accumulated value and total**

points. Suggested function signature: void playerPrint(int player, char playerName[], int

**dNumber, intaccumValue, intplayerTotal).**

**·A function, displayTable, that displays (prints out) all totals per player at the end of a round**

turn (scores table). This table will be displayed at the end of each game, as well as at the final

results.

·A function, statistics, that stores statistical values accordingly in order to calculate them at the

end of the games.

·A function, statisticsTotal, that calculates and displays (prints out) final statistics for the

following questions:

1. Probability of getting one dot, P(1). P(1) = # times one dot / # times thrown dice

**2. Probability of getting two dots.**

3. Probability of getting three dots.

4. Probability of getting four dots.

5. Probability of getting five dots.

6. Probability of getting six dots.

The sum of these probabilities should total 1.

**7. Average number of times the dice is thrown to get a defined accumulated value (per**

player).

8. Average number of times the dice is thrown to get 101 or more (to win).

GUTSY GUI

NOTE: This GUI is a sample using fictional inputs and results.

A message with instructions must be displayed at the beginning of the program. After this, the computer will ask:

Please type in the number of players: 2

Please type in the number of games: 2

Take into account that these inputs must be valid as they must be positive values and greater than

zero. Then the computer will ask:

Player 1:

Name: Amy

Minimum number to stop in a turn: 40

Player 2:

Name: Ben

Minimum number to stop in a turn: 45

The user can type in lower case or upper case for inputs, but the program will always convert to upper

case. Once information from the user is gathered, the computer will start the automatic game.

***** GAME STARTING *****

The program will then take turns for each player will display:

Game 1

Amy’s turn: 6 6 6 5 6 5 6 =40. Accumulated total=40. Throws=7

Ben’s turn: 6 1 =0. Accumulated total=0. Throws=2

Amy’s turn: 1 =0. Accumulated total=40. Throws=1

Ben’s turn: 6 6 6 1 =0. Accumulated total=0. Throws=4

Amy’s turn: 6 6 6 5 6 6 6 =41. Accumulated total=81. Throws=7

Ben’s turn: 5 6 6 1 =0. Accumulated total=0. Throws=4

Amy’s turn: 6 6 6 6 5 6 1 =0. Accumulated total=81. Throws=7

Ben’s turn: 6 6 5 6 6 6 6 6 =47. Accumulated total=47. Throws=8

Amy’s turn: 6 6 6 6 5 5 6 =40. Accumulated total=121.Throws=7

When a play scores 101, the winner will be declared a table of statistics will be displayed as follows:

***** Winner: Amy *****

Game 1 – Statistics

P(1): 0.1064

P(2): 0.0000

P(3): 0.0000

P(4): 0.0000

P(5): 0.1702

P(6): 0.7234

Amy – A(40) = 9

Ben – A(45) = 18

A(101) = 29

Final Table

Player Risk Factor Times Won

Amy 40 1

Ben 45 0

Then the program will continue playing the following games and displaying as presented above. At the end will display again the final table and statistics (similar tables as presented above), but sorted alphabetically.

Final Statistics for 2 Games

P(1): 0.1585

P(2): 0.0000

P(3): 0.0000

P(4): 0.0000

P(5): 0.1768

P(6): 0.6646

Amy – A(40) = 16

Ben – A(45) = 20

A(101) = 45

PROGRAMMING REQUIREMENTS**
**·No global variables are allowed.

·All statistical values must be displayed with 4 decimal places.

·You must manage information about players and statistics using arrays.

·Your program needs to display all information such that is easy to read. Your code also must

be very well commented and have head of code and head of functions.

·You should use all methods you create. You can also create any additional functions/methods

that you considered necessary.

Your program will most probably required the includes of stdafx.h (provides printf_s, scanf_s,

getchar), stdlib.h (provides rand, srand, and abs), and time.h (provides time, which is used to “seed” the random number generator).

Averages, probabilities or means are commonly used in engineering calculations. The mean*
*number is easy to calculate. Simply add up the number of throws of XX dots and divide by the total number of throws.

Solution

// Defines the entry point for the console application.

#include “stdafx.h”

#include “stdio.h”

#include “stdlib.h”

#include “time.h”

#include “string.h”

/* define a structure to hold the information of players */

structplayer_t {

char name[64];

int min; /* minimum number to stop in each turn */

/* saved in each turn */

int rolls[101]; /* saved dice rolls in this turn */

intnumTimes; /* number of rolls */

int value; /* value gained in this turn */

/* saved in each game */

intaccumValue; /* accumulated total in this game */

inttotalNum; /* total number of throws */

intreachMin; /* number of times reach min (to stop) */

/* saved across games */

int wins; /* number of wins */

inttotalNumAll;

intreachMinAll;

};

/* store the state of games */

structstat_t {

int counts[6]; /* number of rolls of each dice value */

intnum; /* total number of rolls */

/* to calculate average A(101) = num101 / reach101. */

int reach101; /* number of times reaching 101 */

int num101; /* total number of rolls to reach 101 */

};

voiddiceThrown(intdNumber, int *accumValue, int *numTimes, int rolls[]);

intplayerTurn(structplayer_t *p);

voidprintPlayer(structplayer_t *p);

void statistics(structstat_t *st, structplayer_t *p);

/* accumulate the statistics in one game to all games */

voidstatisticsTotal(structstat_t *game, structstat_t *all, structplayer_tps[], int players);

/* print the statistics table */

voidprintTable(structstat_t *st, structplayer_tps[], int players, int flag);

int main() {

/* prompt the user to enter the game information */

int players, games;

int i, j;

structplayer_tpls[32]; /* assuming at most 32 players in the game */

structstat_t game, all; /* statistics of each game and all games */

structplayer_t *p;

srand(time(NULL));

printf(“Game 101 (automatic) — whoever reaches 101 wins.\n\n\n”);

printf(“Please type in the number of players: “);

scanf_s(“%d”, &players);

printf(“Please type in the number of games: “);

scanf_s(“%d”, &games);

if (players <= 0 || games <= 0) {

printf(“Error: the number of players and games must be positive.\n”);

return -1;

}

memset(pls, 0, sizeof(pls));

/* ask for the name of player and minimum number to stop rolling in each turn */

for (i = 0; i < players; i++) {

p = &pls[i];

printf(“\nPlayer %d:\n”, i + 1);

printf(“\t Name: “);

scanf_s(“%32s”, p->name, sizeof(p->name));

printf(“\t Mininum number to stop in a turn: “);

scanf_s(“%d”, &p->min);

}

memset(&all, 0, sizeof(all));

/* automatically play the game */

printf(“\n\n\t\t\t ***** GAME STARTING *****\n”);

for (i = 1; i <= games; i++) {

int win = 0;

/* clear up the state for this turn */

for (j = 0; j < players; j++) {

p = &pls[j];

p->accumValue = 0;

p->totalNum = 0;

p->reachMin = 0;

}

memset(&game, 0, sizeof(game));

printf(“\nGame %d\n\n”, i);

while (win == 0) { /* player turn by turn, until one player win */

for (j = 0; j < players; j++) {

p = &pls[j];

win = playerTurn(p);

printPlayer(p);

/* accumulate statistics for this player */

statistics(&game, p);

if (win == 1) { /* this player win */

pls[j].wins++;

win = j + 1;

break;

}

}

printf(“\n”);

}

/* accumulate the total statistics */

statisticsTotal(&game, &all, pls, players);

/* display the result of this game */

win = win – 1;

printf(“\n ***** Winner: %s *****\n”, pls[win].name);

printf(“Game %d – Statistics\n”, i);

printTable(&game, pls, players, 0);

}

/* display the final table */

printf(“\n\nFinal Table\n”);

printf(“\t %-16s Risk Factor Times\n”, “Player”);

for (i = 0; i < players; i++) {

p = &pls[i];

printf(“\t %-16s %2d %5d\n”, p->name, p->min, p->wins);

}

printf(“\nFinal Statistics for %d Games\n”, games);

printTable(&all, pls, players, 1);

return 0;

}

/** implementation of the functions **/

voiddiceThrown(intdNumber, int *accumValue, int *numTimes, int rolls[]) {

int value = 0;

intnum = 0;

while (value <dNumber) {

/* keep rolling until reach dNumber or roll 0 */

int dice = rand() % 6 + 1;

rolls[num] = dice;

num++;

if (dice == 1) {

value = 0;

break;

}

/* accumulate to the total value */

value += dice;

}

/* save into the pointer to return */

*accumValue = value;

*numTimes = num;

}

intplayerTurn(structplayer_t *p) {

/* initialize the state for this turn */

p->numTimes = 0;

p->value = 0;

/* make a roll and accumulate the data */

diceThrown(p->min, &p->value, &p->numTimes, p->rolls);

p->totalNum += p->numTimes;

p->accumValue += p->value;

/* return if this player win */

return p->accumValue>= 101;

}

voidprintPlayer(structplayer_t *p) {

int i;

/* display the state of this player in this turn */

printf(“\t %s’s turn: “, p->name);

for (i = 0; i < p->numTimes; i++) {

printf(“%d “, p->rolls[i]);

}

printf(“=%d. Accumulated total=%d. Throws=%d\n”,

p->value, p->accumValue, p->numTimes);

}

void statistics(structstat_t *st, structplayer_t *p) {

int i;

/* accumulate number of each rolls */

for (i = 0; i < p->numTimes; i++) {

int value = p->rolls[i];

st->counts[value – 1] ++;

}

st->num += p->numTimes;

if (p->value >= p->min) {

p->reachMin++;

}

if (p->accumValue>= 101) { /* win the game, accumulate 101 state */

st->reach101 += 1;

st->num101 += p->totalNum;

}

}

voidprintTable(structstat_t *st, structplayer_tps[], int players, int flag) {

int i;

structplayer_t *p;

for (i = 0; i < 6; i++) { /* probability of each dice count */

printf(“\t P(%d): %.4f\n”, i + 1, st->counts[i] * 1.0 / st->num);

}

/* display the information of each player */

for (i = 0; i < players; i++) {

p = &ps[i];

if (flag == 0) { /* each game */

if (p->reachMin> 0) {

printf(“\t %s – A(%d) = %d\n”, p->name, p->min, p->totalNum / p->reachMin);

}

else { /* never reach the min value */

printf(“\t %s – A(%d) = 0\n”, p->name, p->min);

}

}

else { /* all games */

if (p->reachMinAll> 0) {

printf(“\t %s – A(%d) = %d\n”, p->name, p->min, p->totalNumAll / p->reachMinAll);

}

else { /* never reach the min value */

printf(“\t %s – A(%d) = 0\n”, p->name, p->min);

}

}

}

printf(“\t A(101) = %d\n”, st->num101 / st->reach101);

}

voidstatisticsTotal(structstat_t *game, structstat_t *all, structplayer_tps[], int players) {

int i;

structplayer_t *p;

for (i = 0; i < 6; i++) {

all->counts[i] += game->counts[i];

}

all->num += game->num;

all->reach101 += game->reach101;

all->num101 += game->num101;

for (i = 0; i < players; i++) { /* accumulate the statistics of each player */

p = &ps[i];

p->totalNumAll += p->totalNum;

p->reachMinAll += p->reachMin;

}

}