Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a program that simulates the game of craps, which is played with two dice.

ID: 675366 • Letter: W

Question

Write a program that simulates the game of craps, which is played with two dice. On the first roll, the player wins if the sum of the dice is 7 or 11. The player loses if the sum is 2, 3, or 12. Any other roll is called the “point”, and the game continues. On each subsequent roll, the player wins if he or she rolls the point again. The player loses by rolling a 7. Any other roll is ignored and the game continues. At the end of the each game, the program will ask the user whether or not to play again. When the user enters a response other than ‘y’ or ‘Y’, the program will display the number of wins and losses and then terminate. Specifications: Write the program using three functions: main, roll_dice, and play_game. Use the following function prototypes: int roll_dice(void); bool play_game(void); roll_dice should generate two random numbers, each between 1 and 6, and return their sum. Use the rand function to generate random numbers. play_game should play one craps game (calling roll_dice as many times as necessary to determine the outcome of each dice roll); it will return TRUE if the player wins and FALSE if the player loses. play_game is also responsible for displaying messages showing the results of the player’s dice rolls. main will call play_game repeatedly, keeping track of the number of wins and losses, and displaying the “You win!” and “You lose!” messages. At the beginning main, after displaying a “Welcome to Craps” message, you should prompt the user to enter a random seed (to satisfy the grader program). Finally, use the exit function and the EXIT_SUCCESS constant to exit your program (which are both defined in ). From this point forward, each of your programs should use the exit function to terminate. If you execute the program, the following information should be displayed: ~> hw21.o Welcome to Craps Please enter a random seed: 23 You rolled: 4 Your point is: 4 You rolled: 8 You rolled: 8 You rolled: 5 You rolled: 6 You rolled: 11 You rolled: 7 You lose! Play again?: n Wins: 0 Losses: 1 ~> ~> hw21.o Welcome to Craps Please enter a random seed: 63 You rolled: 7 You win! Play again? y You rolled: 7 You win! Play again? y You rolled: 10 Your point is: 10 You rolled: 4 You rolled: 7 You lose! Play again? n Wins: 2 Losses: 1 ~> ~> hw21.o Welcome to Craps Please enter a random seed: 152 You rolled: 5 Your point is: 5 You rolled: 3 You rolled: 5 You win! Play again?: q Wins: 1 Losses: 0

Explanation / Answer

#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <stdbool.h>
#include <time.h>
#include <strings.h>

#define MAX_WORD_LEN 80
#define ALPHABET 26

/* A pair of dice and their sum */
struct game_craps
{
   int die_1;
   int die_2;
   int dice_sum;
};

struct game_craps roll_dice(void);
bool play_game(void);

/* main loop
*
*/
int main(void)
{
   int wins = 0;
   int losses = 0;
   char choice[2] = "Y";

   srand((unsigned)time(NULL)); /* seed random number generator */

   while (strcasecmp(choice, "N")) {
       play_game() ? ++wins : ++losses;

       printf("Play again? (y/n): ");
       scanf("%1s", choice);
   }
   printf("wins: %d losses: %d ", wins, losses);

   return 0;
}


/* roll_dice
*
*/
struct game_craps roll_dice(void)
{
   struct game_craps roll;

   /* printf("roll_dice() "); */
   printf(" Roll dice... ");
   roll.die_1 = (rand() % 6) + 1;
   roll.die_2 = (rand() % 6) + 1;
   roll.dice_sum = roll.die_1 + roll.die_2;
   printf("| %d | | %d | ", roll.die_1, roll.die_2);
   printf("You rolled: %d ", roll.dice_sum);
   return roll;
}

/* play_game
*
*/
bool play_game(void)
{
   bool win = false;
   bool win_point = false;
   int point = 0;          
   struct game_craps game;

   game = roll_dice();

   switch(game.dice_sum) {
       case 7: case 11: win = true;
           break;
       case 2: case 3: case 12: win = false;
           break;
       default: point = game.dice_sum;
   }

   if (point) {
       printf("Your point is %d ", point);
       do {
           game = roll_dice();
           if (game.dice_sum == 7) {
               win_point = true;
               win = false;
               printf("Bad luck, Snake Eyes! ");
           }
           if (game.dice_sum == point) {
               win_point = true;
               win = true;
           }
       } while (win_point == false);
   }

   printf("You %s ", win ? "Won!" : "Lost... :-(");

   return win;
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote