# Dice Game Homework Solution in C

## The Game Of Dice

The project is to create a 2-player game of dice.

Game Rules:

• The user selects the number of rounds to be run. Each dice round consists of 3 values:
• Dice – the value of the dice rolled. It is a random number between 1-6.
• Points – value associated with the round. It is a random number from 10-100 in multiples of 10. (10 * random number between 1-10).
• Type – the type of the round. There are 3 types of rounds:

REGULAR – keep the “points” equal to the number of points as calculated above using a random number generator.

BONUS – replace “points” to be equal to 200.

DOUBLE – update “points” to be equal to DOUBLE the number of points as calculated above using a random number generator.

Probability of the round types:

• 20% for BONUS, 30% for DOUBLE, and 50% for REGULAR
• The game consists of two players (1& 2). The game starts with one player at random.
• For each round, one of the following cases can occur:
• Success: Player-1 (odd player) is the current player and the dice rolled is odd

OR Player-2 (even player) is the current player and the dice rolled is even.

The current player gains the points, based on the type of round (see above). The current player’s turn continues in the next round.

• Failure: Player-1 (odd player) is the current player and the dice rolled is even

OR Player-2 (even player) is the current player and the dice rolled is odd.

Current player incurs the penalty of the same number of points, based on the type of the round (see above). In the next round, the current player is changed to the other player.

• At the end of all rounds, the player with the highest points wins the game.

Solution:

Task 1: Complete the main.c

• Follow the instructions written in the comments in the main() function. The main() is the driver of the program. It calls the functions below to play the game.

Task 2 – create and write dice game.h

• Include the header guard in the correct format.
• Create an enum named ROUND TYPE to identify the three-round types:

BONUS, DOUBLE, REGULAR

• Write all the function prototypes – see Task below.

Task 3 – create and write dice game.c

Write the following functions that use the game rules described above:

• int getRandomNumber( int min, int max )– This function computes a random number between min and max, inclusive, and returns it
• ROUNDTYPEgetRoundType( ) – This function decides the type of the round based on the probability values mentioned in-game rules (20% for BONUS, 30% for DOUBLE, and 50% for REGULAR). HINT: Use the getRandomNumber()function to generate a random number between 0through9 and use this value for the probability
• intgetRoundPoints( ROUND TYPE)– This function calculates and returns the points associated with the given round type, based on the rules mentioned above. You will need to call the getRandomNumber() function to generate the points randomly.
• void printPlayerPoints( intp1, int p2 ) – This function prints out the player points at the end of each round in the format shown below:

P1:200

P2: 100

• void printRoundInfo( ROUND TYPE t, int dice, int points ) – This function prints out the information for each round in the format shown below:

Type: BONUS

DICE: 4

POINTS: 200

Sample Output

############### RUN-1 ###############

Enter the number of rounds: 5

P1 : 0

P2 : 0

ROUND 1

--------

Player: 1

Type: REGULAR

Dice: 5

Points: 70

P1: 70

P2 : 0

ROUND 2

--------

Player: 1

Type: REGULAR

Dice: 2

Points: 50

P1: 20

P2 : 0

ROUND 3

--------

Player: 2

Type: DOUBLE

Dice: 4

Points: 180

P1: 20

P2: 180

ROUND 4

--------

Player: 2

Type: DOUBLE

Dice : 3

Points: 140

P1: 20

P2: 40

ROUND 5

--------

Player : 1

Type : DOUBLE

Dice : 3

Points : 180

P1 : 200

P2 : 40

GAME OVER!!

P1 Won

############### RUN-2 ###############

Enter the number of rounds: 5

P1 : 0

P2 : 0

ROUND 1

--------

Player: 2

Type: REGULAR

Dice: 6

Points: 40

P1 : 0

P2: 40

ROUND 2

--------

Player: 2

Type: BONUS

Dice : 3

Points: 200

P1 : 0

P2: -160

ROUND 3

--------

Player : 1

Type : BONUS

Dice : 4

Points : 200

P1 : -200

P2 : -160

ROUND 4

--------

Player: 2

Type: REGULAR

Dice: 5

Points: 20

P1: -200

P2: -180

ROUND 5

--------

Player: 1

Type: DOUBLE

Dice: 6

Points: 100

P1: -300

P2: -180

GAME OVER!!

P2 Won

Solution:

Main.c

``` // Add all the includes for the required header files #include #include #include #include "dicegame.h" // Start the main() function with both the parameters int main() { int player1Points; int player2Points; int numRounds; int round; int roundPoints; int currentPlayer; int dice; ROUNDTYPE roundType; // Initialize the srand() to start the random generator srand((unsigned) time(0)); // Initialize the player-1 and player-2 points to 0 player1Points = 0; player2Points = 0; // Initialize all other required variables currentPlayer = 1; // Ask the user for the number of rounds to run the game printf("Enter the number of rounds: "); scanf("%d", &numRounds); // Call printPlayerPoints() function to print the initial player points which will be 0 printPlayerPoints(player1Points, player2Points); // Set up the loop to go through all the rounds one at a time for(round = 1; round <= numRounds; round++) { // Call the corresponding functions to get the information for this round - type, dice, points roundType = getRoundType(); dice = getRandomNumber(1, 6); roundPoints = getRoundPoints(roundType); // Print round number printf("\nROUND %d\n", round); printf("--------\n"); // Print current player printf("Player : %d\n", currentPlayer); // Call printRoundInfo() to print the round information printRoundInfo(roundType, dice, roundPoints); // MAIN GAME LOGIC // Write code here to get the main game logic using branches // Success: Player-1 (odd player) is the current player and the dice rolled is odd // OR Player-2 (even player) is the current player and the dice rolled is even. if ((currentPlayer == 1 && dice % 2 != 0) || (currentPlayer == 2 && dice % 2 == 0)) { // Current player gains the points, based on the type of the round (see above). The current playerâ€™s turn continues in the next round. if (currentPlayer == 1) player1Points += roundPoints; else player2Points += roundPoints; } else { // Failure: Player-1 (odd player) is the current player and the dice rolled is even // OR Player-2 (even player) is the current player and the dice rolled is odd. // Current player incurs penalty of the same number of points, based on the type of the round (see above). In the next round, the current player is changed to the other player. if (currentPlayer == 1) { player1Points -= roundPoints; currentPlayer = 2; } else { player2Points -= roundPoints; currentPlayer = 1; } } // Call printPlayerPoints() to print the player information at the end of the round printPlayerPoints(player1Points, player2Points); } printf("\nGAME OVER!!\n"); // Compare the final points for player-1 and player-2 // Print the winner as the one with higher points if (player1Points > player2Points) printf("P1 Won\n"); else if (player1Points < player2Points) printf("P2 Won\n"); else printf("Tie\n"); // Return from the main() function to end the program successfully return 0; } ```

Dicegame.h

``` #ifndef dicegame_h #define dicegame_h typedef enum { BONUS, DOUBLE, REGULAR } ROUNDTYPE; // Computes a random number between min and max, inclusive, and returns it int getRandomNumber(int min, int max); // Decides the type of round based on the probability values mentioned in game rules // (20% for BONUS, 30% for DOUBLE, and 50% for REGULAR ROUNDTYPE getRoundType(); // Calculates and return the points associated with the given round type, based on the // rules mentioned above. int getRoundPoints(ROUNDTYPE roundType); // Prints out the player points at the end of each round. void printPlayerPoints(int p1, int p2); // Prints out the information for each round. void printRoundInfo(ROUNDTYPE t, int dice, int points); #endif ```

Dicegame.c

``` #include "dicegame.h" #include #include // Computes a random number between min and max, inclusive, and returns it int getRandomNumber(int min, int max) { return (rand() % (max - min + 1)) + min; } // Decides the type of round based on the probability values mentioned in game rules // (20% for BONUS, 30% for DOUBLE, and 50% for REGULAR ROUNDTYPE getRoundType() { int randomNumber; randomNumber = getRandomNumber(1, 10); // CHANCES: [1 2 3 4 5] [6 7 8] [9 10] // REGULAR DOUBLE BONUS if (randomNumber >= 9) return BONUS; if (randomNumber >= 6) return DOUBLE; return REGULAR; } // Calculates and return the points associated with the given round type, based on the // rules mentioned above. int getRoundPoints(ROUNDTYPE roundType) { int points; points = 10 * getRandomNumber(1, 10); if (roundType == BONUS) points = 200; else if(roundType == DOUBLE) points *= 2; return points; } // Prints out the player points at the end of each round. void printPlayerPoints(int p1, int p2) { printf("P1 : %d\n", p1); printf("P2 : %d\n", p2); } // Prints out the information for each round. void printRoundInfo(ROUNDTYPE t, int dice, int points) { printf("Type : "); if (t == BONUS) printf("BONUS"); else if (t == DOUBLE) printf("DOUBLE"); else printf("REGULAR"); printf("\n"); printf("DICE : %d\n", dice); printf("POINTS : %d\n", points); } ```