Question

In: Computer Science

**C programming language The following description has been adopted from Deitel & Deitel. One of the...

**C programming language

The following description has been adopted from Deitel & Deitel. One of the most popular games of chance is a dice game called "craps," which is played in casinos and back alleys throughout the world. The rules of the game are straightforward:

A player rolls two dice. Each die has six faces. These faces contain 1, 2, 3, 4, 5, and 6 spots. After the dice have come to rest, the sum of the spots on the two upward faces is calculated. If the sum is 7 or 11 on the first throw, the player wins. If the sum is 2, 3, or 12 on the first throw (called "craps"), the player loses (i.e. the "house" wins). If the sum is 4, 5, 6, 8, 9, or 10 on the first throw, then the sum becomes the player's "point." To win, you must continue rolling the dice until you "make your point." The player loses by rolling a 7 before making the point.

Write a program that implements a craps game according to the above rules. The game should allow for wagering. This means that you need to prompt that user for an initial bank balance from which wagers will be added or subtracted. Before each roll prompt the user for a wager. Once a game is lost or won, the bank balance should be adjusted. As the game progresses, print various messages to create some "chatter" such as, "Sorry, you busted!", or "Oh, you're going for broke, huh?", or "Aw cmon, take a chance!", or "You're up big, now's the time to cash in your chips!"

Use the below functions to help you get started! You may define more than the ones suggested if you wish!

(5 pts) void print_game_rules (void) — Prints out the rules of the game of "craps".

(5 pts) double get_bank_balance (void) - Prompts the player for an initial bank balance from which wagering will be added or subtracted. The player entered bank balance (in dollars, i.e. $100.00) is returned.

(5 pts) double get_wager_amount (void) - Prompts the player for a wager on a particular roll. The wager is returned.

(5 pts) int check_wager_amount (double wager, double balance) - Checks to see if the wager is within the limits of the player's available balance. If the wager exceeds the player's allowable balance, then 0 is returned; otherwise 1 is returned.

(5 pts) int roll_die (void) - Rolls one die. This function should randomly generate a value between 1 and 6, inclusively. Returns the value of the die.

(5 pts) int calculate_sum_dice (int die1_value, int die2_value) - Sums together the values of the two dice and returns the result. Note: this result may become the player's point in future rolls.

(10 pts) int is_win_loss_or_point (int sum_dice) - Determines the result of the first dice roll. If the sum is 7 or 11 on the roll, the player wins and 1 is returned. If the sum is 2, 3, or 12 on the first throw (called "craps"), the player loses (i.e. the "house" wins) and 0 is returned. If the sum is 4, 5, 6, 8, 9, or 10 on the first throw, then the sum becomes the player's "point" and -1 is returned.

(10 pts) int is_point_loss_or_neither (int sum_dice, int point_value) - Determines the result of any successive roll after the first roll. If the sum of the roll is the point_value, then 1 is returned. If the sum of the roll is a 7, then 0 is returned. Otherwise, -1 is returned.

(5 pts) double adjust_bank_balance (double bank_balance, double wager_amount, int add_or_subtract) - If add_or_subtract is 1, then the wager amount is added to the bank_balance. If add_or_subtract is 0, then the wager amount is subtracted from the bank_balance. Otherwise, the bank_balance remains the same. The bank_balance result is returned.

(5 pts) void chatter_messages (int number_rolls, int win_loss_neither, double initial_bank_balance, double current_bank_balance) - Prints an appropriate message dependent on the number of rolls taken so far by the player, the current balance, and whether or not the player just won his roll. The parameter win_loss_neither indicates the result of the previous roll.

(10 pts) Others?

(20 pts) A main ( ) function that makes use of the above functions in order to play the game of craps as explained above. Note that you will most likely have a loop in your main ( ) function (or you could have another function that loops through the game play).

Have a great time with this assignment! There is plenty of room for creativity! Note: I have not stated how you must display the game play! You may do as you wish!

Solutions

Expert Solution

// C program simulating the game of Craps

#include <stdlib.h>

#include <stdbool.h>

#include <stdio.h>

#include<time.h>

// implement a method to print

void print_game_rule(void)

{

               fflush(stdout);

               printf("A Player Roll Two Dice. Each Dice Faces Contains 1 to 6.\n");

               printf("After the Dice Come to Rest. The Sum of the Spots on the Two Upward Faces is Calculated.\n");

               printf("If Sum is 7 or 11 on First throw then Player Wins\n");

               printf("If Sum is 2 , 3 or 12 on First throw then Player Loses.\n");

               printf("If the Sum is 4, 5, 6, 8, 9 or 10 on the first throw then the Sum become the Playes's point.");

               printf("To Win you must Contine Rolling the Dice Until you, 'Make the Point'. The player Loses by Rolling a 7 before making the Point.\n");

}

// Implement the get_bank_balance method

double get_bank_balance(void)

{

               // display

               printf("Enter initial bank Balance: ");

               fflush(stdout);

               // declare required

               double bankBalance;

               scanf("%lf",&bankBalance);

               return bankBalance;

}

// Implement the get_wager_amount method

double get_wager_amount(void)

{

               printf("Enter the Wager Amount: ");

               fflush(stdout);

               // declare varaible

               double wagerAmount;

               scanf("%lf", &wagerAmount);

               return wagerAmount;

}

// Implement the check_wager_amount method

int check_wager_amount(double wager, double balance)

{

               // checks and compared

               if (wager > balance)

                              return 0;

               else

               // returns

                              return 1;

}

// Implement the roll_die method

int roll_die(void)

{

               // generate random number

               srand(time(0));

               return(rand() % 6 + 1);

}

// Implement the calculate_sum_dice method

int calculate_sum_dice(int die1_value, int die2_value)

{

// return value

return die1_value + die2_value;

}

// Implement the is_win_loss_or method

int is_win_loss_or_point(int sum_dice)

{

               // check and compared

               if (sum_dice == 7 || sum_dice == 11)

                              return 1;

               // then check

               else if (sum_dice == 2 || sum_dice == 3 || sum_dice == 12)

                              return 0;

               else

                              return -1;

}

// Implement the is_point_loss_neither method

int is_point_loss_neither(int sum_dice, int point_value)

{

               // checks

               if (sum_dice == point_value)

                              return 1;

               else if (sum_dice == 7)

                              return 0;

               else

                              return -1;

}

// Implement the adjust_bank_balance method

double adjust_bank_balance(double bank_balance,double wager_amount, int add_or_subtract)

{

               // checks

               if (add_or_subtract == 1)

               // assigning the balance

                              bank_balance += wager_amount;

               // then

               else if (add_or_subtract == 0)

               // assigning balance

                              bank_balance -= wager_amount;

               return bank_balance;

}

// Implement the chatter_message method

void chatter_message(int number_rolls, int win_loss_neither, double initail_bank_balance, double current_bank_balance)

{

               // displays

               printf("The Number of Rolls %d\n", number_rolls);

               printf("Current Balance %lf\n", current_bank_balance);

               // checks

               if (initail_bank_balance - current_bank_balance > 0)

               // display

                              printf("Loosing Amount %lf\n", initail_bank_balance - current_bank_balance);

               else

                              printf("Winning Amount %lf\n", current_bank_balance - initail_bank_balance);

               // checks

               if (win_loss_neither == 1)

                              printf("You Won\n");

               else if (win_loss_neither == 0)

                              printf("You Loss\n");

               else if (win_loss_neither == -1)

               // print

                              printf("Sorry It is Neither Win or Loss\n");

}

int main(void) {

               // method definition

               print_game_rule();

               // declare double variable

               double bank_balance = get_bank_balance();

               // declare double variable

               double initial_bank_balance = bank_balance;

               // assign

               bool play = true;

               // declared

               int pValue, rNumber = 1;;

               // using looping

               while (play && bank_balance > 0)

               {

                              // assign

                              double wager_amount = get_wager_amount();

                              // checks

                              if (!check_wager_amount(wager_amount, bank_balance))

                              {

                              // print

                                             printf("You don't sufficient balance please enter lesser amount\n");

                                             continue;

                              }

                              // set dice

                              int di1 = roll_die();

                              int di2 = roll_die();

                              int diSum = calculate_sum_dice(di1, di2);

                              // print

                              printf("Dices roll value %d\n", diSum);

                              // checks

                              if (rNumber == 1)

                              {

                              // assign the method

                                             int check = is_win_loss_or_point(diSum);

                                             if (check == 1)

                                             {

                                                            printf("You win the Game\n");

                                                            // calling method

                                                            bank_balance = adjust_bank_balance(bank_balance, wager_amount, 1);

                                                            // calling method

                                                            chatter_message(rNumber, check, initial_bank_balance, bank_balance);

                                                            break;

                                             }else if(check == 0)

                                             {

                                                            // print

                                                            printf("Ohhh! Craps. House wins. Better luck next time.\n");

                                                            // calling method

                                                            bank_balance = adjust_bank_balance(bank_balance, wager_amount, 0);

                                                            // calling method

                                                            chatter_message(rNumber, check, initial_bank_balance, bank_balance);

                                                            break;

                                             }else

                                             {

                                                            // set

                                                            pValue = diSum;

                                             }

                                             // calling method

                                             chatter_message(rNumber, check, initial_bank_balance, bank_balance);

                              }else

                              {

                                             // assigning and calling method

                                             int check = is_point_loss_neither(diSum,pValue);

                                             if (check == 1)

                                             {

                                                            printf("You win the Game\n");

                                                            // calling method

                                                            bank_balance = adjust_bank_balance(bank_balance, wager_amount, 1);

                                                            // calling method

                                                            chatter_message(rNumber, check, initial_bank_balance, bank_balance);

                                                            break;

                                             }else if (check == 0)

                                             {

                                                            // print

                                                            printf("Ohhh! Craps. House wins. Better luck next time.\n");

                                                            // calling method

                                                            bank_balance = adjust_bank_balance(bank_balance, wager_amount, 0);

                                                            // calling method

                                                            chatter_message(rNumber, check, initial_bank_balance, bank_balance);

                                                            break;

                                             }else

                                             {

                                                            // set

                                                            pValue = diSum;

                                             }

                                             // calling method

                                             chatter_message(rNumber, check, initial_bank_balance, bank_balance);

                              }

               }

               return EXIT_SUCCESS;

}

// end of program

Output:

Comment for queries


Related Solutions

Class object in C++ programming language description about lesson inheritance example.
Class object in C++ programming language description about lesson inheritance example.
Class object in C++ programming language description about lesson inheritance example.
Class object in C++ programming language description about lesson inheritance example.
Code in C-language programming description about convert binary number to decimal number.
Code in C-language programming description about convert binary number to decimal number.
Class object in C++ programming language description about lesson Overloading function example.
Class object in C++ programming language description about lesson Overloading function example.
Class object in C++ programming language description about lesson copy constructor example.
Class object in C++ programming language description about lesson copy constructor example.
Class object in C++ programming language description about lesson static variable example.
Class object in C++ programming language description about lesson static variable example.
Code in C++ programming language description about read and write data to memory example.
Code in C++ programming language description about read and write data to memory example.
Class object in C++ programming language description about lesson inheritance multi level example.
Class object in C++ programming language description about lesson inheritance multi level example.
Class object in C++ programming language description about lesson unary overloading function example.
Class object in C++ programming language description about lesson unary overloading function example.
Description: In this assignment, you will implement a deterministic finite automata (DFA) using C++ programming language...
Description: In this assignment, you will implement a deterministic finite automata (DFA) using C++ programming language to extract all matching patterns (substrings) from a given input DNA sequence string. The alphabet for generating DNA sequences is {A, T, G, C}. Write a regular expression that represents all DNA strings that begin with ‘A’ and end with ‘T’. Note: assume empty string is not a valid string. Design a deterministic finite automaton to recognize the regular expression. Write a program which...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT