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

The Problem: Casino You will write a program that simulates a casino for a singl

ID: 3676510 • Letter: T

Question

The Problem: Casino

You will write a program that simulates a casino for a single player. The user will initially start with $1000. The user will then be able to choose from the following options:

1) Buy chips

2) Sell chips

3) Play Craps

4) Play Arup's Game of Dice

5) Status Report

6) Quit

Your program will execute each choice until the quits. At this point all of their chips automatically get sold back to the casino and a message prints out how much money the user has left (of the $1000) after gambling.

Craps

One of the most "fair" games to play at a casino is Craps. Here is one version of how to play:

1) Roll a pair of fair six-sided dice.

2) If you roll a 7 or 11, you win!

3) If you roll a 2, 3, or 12, you lose.

4) Otherwise, record what you've rolled. Let this sum be k; also known as your point.

5) If you rolled a point, continue rolling the pair of dice until you get either your point (k) or a sum of seven on the two dice.

6) If k comes up first, you win!

7) If 7 comes up first, you lose.

Arup's Game of Dice

Amazingly, this game is even more "fair" than Craps, but the house still has a 50.2% chance of winning, which is why the casino hasn't gone broke yet! Here are the rules:

1) Roll a pair of dice.

2) If you roll a sum of 11 or 12, you win.

3) If you roll a sum of 2, you lose.

4) Otherwise, record what you've rolled. Let this sum be k; also known as your point.

5) Roll one more time. If this roll exceeds your point(k), you win!

6) If this roll is the same as your point(k) or lower, you lose.

Buying Chips

Chips cost $11. Whenever a customer buys chips, he/she must give the banker some money. The banker will always give the user the maximum number of chips they can buy with the money given to them and return the leftover cash. You will write a single function that takes care of this transaction.

Selling Chips

The casino buys chips back at $10 a piece. You will write a single function that takes care of this transaction.

Functions you must write

Though you may write more functions, here are function prototypes for the ones you are required to write:

// Precondition: None.

// Postcondition: Returns the sum of two random dice rolls.

int pairofdice();

// Precondition: None.

// Postcondition: Plays one game of Craps and returns 1 if

//                the player won and 0 if they lost.

int craps();

// Precondition: None.

// Postcondition: Plays one game of Arup's game of dice and

//                returns 1 if the player won and 0 if they

//                lost.

int arupsdice();

// Precondition: cash is the address of the variable

//               storing the amount of money the user is

//               wants to spend on chips.

// Postcondition: The number of chips purchased is returned

//                and the variable storing the amount of

//                money the user paid for chips is adjusted

//                to equal the change left over after the

//                transaction.

int buychips(int *cash);

// Preconditions: numchips > 0.

// Postconditions: Returns the cash obtained for selling

//                 numchips number of chips.

int sellchips(int numchips);

// Precondition: The first parameter is the number of

//               chips the user has, the second is how

//               much cash they currently have.

// Postcondition: A report detailing the number of chips

//                and the amount of cash the user has is

//                printed.

void statusreport(int numchips, int cash);

References

Textbook: Chapters 3 and 4                            Notes: Lectures on loops, functions, random

                                                                                    number generator functions

Restrictions

Name the file you create and turn in casino.c. Although you may use other compilers, your program must compile and run using gcc. If you use your olympus account to work on this assignment, please follow the steps shown in class to create, compile, and test your program. Your program should include a header comment with the following information: your name, course number, section number, assignment title, and date. You should also include comments throughout your code, when appropriate. If you have any questions about this, please see a TA.

Input Specification

Assume that the user always enters the proper type of input, but not always appropriate values. Here are some possible errors you need to check for:

1) Do not allow the user to spend more money on chips than they have.

2) Do not allow the user to bet a number of chips they don't have.

3) Do not allow the user to sell a number of chips they don't have.

4) The user must always bet at least one chip for a game, or they can not play the game.

Assume that these specifications will be followed by the user:

1) They will never enter any negative integers.

2) They will never enter any invalid menu choices.

Output Specification

Your output should follow the examples on the following pages.

Deliverables

A single source file named casino.c turned in through WebCT.

Example Output #1

Welcome to the Casino. Here are your choices:

1) Buy chips

2) Sell chips

3) Play Craps

4) Play Arup's Game of Dice

5) Status Report

6) Quit

1

How much cash do you want to spend for chips?

1100

Sorry, you do not have that much money. No chips bought.

Welcome to the Casino. Here are your choices:

1) Buy chips

2) Sell chips

3) Play Craps

4) Play Arup's Game of Dice

5) Status Report

6) Quit

1

How much cash do you want to spend for chips?

500

Welcome to the Casino. Here are your choices:

1) Buy chips

2) Sell chips

3) Play Craps

4) Play Arup's Game of Dice

5) Status Report

6) Quit

5

You currently have $505 left and 45 chips.

Welcome to the Casino. Here are your choices:

1) Buy chips

2) Sell chips

3) Play Craps

4) Play Arup's Game of Dice

5) Status Report

6) Quit

3

How many chips would you like to bet?

0

Sorry, that is not allowed. No game played.

Welcome to the Casino. Here are your choices:

1) Buy chips

2) Sell chips

3) Play Craps

4) Play Arup's Game of Dice

5) Status Report

6) Quit

3

How many chips would you like to bet?

10

Press 'r' and hit enter for your first roll.

r

You rolled a 8.

Press 'r' and hit enter for your next roll.

r

You rolled a 12.

Press 'r' and hit enter for your next roll.

r

You rolled a 6.

Press 'r' and hit enter for your next roll.

r

You rolled a 7.

Sorry, you have lost.

Welcome to the Casino. Here are your choices:

1) Buy chips

2) Sell chips

3) Play Craps

4) Play Arup's Game of Dice

5) Status Report

6) Quit

4

How many chips would you like to bet?

17

Press 'r' and hit enter for your first roll.

r

You rolled a 3.

Press 'r' and hit enter for your next roll.

r

You rolled a 8.

You win!

Welcome to the Casino. Here are your choices:

1) Buy chips

2) Sell chips

3) Play Craps

4) Play Arup's Game of Dice

5) Status Report

6) Quit

6

After selling your chips, you have $1025. Thanks for playing!

Example Output #2

Welcome to the Casino. Here are your choices:

1) Buy chips

2) Sell chips

3) Play Craps

4) Play Arup's Game of Dice

5) Status Report

6) Quit

1

How much cash do you want to spend for chips?

500

Welcome to the Casino. Here are your choices:

1) Buy chips

2) Sell chips

3) Play Craps

4) Play Arup's Game of Dice

5) Status Report

6) Quit

2

How many chips do you want to sell?

46

Sorry, you do not have that many chips. No chips sold.

Welcome to the Casino. Here are your choices:

1) Buy chips

2) Sell chips

3) Play Craps

4) Play Arup's Game of Dice

5) Status Report

6) Quit

4

How many chips would you like to bet?

46

Sorry, you do not have that many chips to bet. No game played.

Welcome to the Casino. Here are your choices:

1) Buy chips

2) Sell chips

3) Play Craps

4) Play Arup's Game of Dice

5) Status Report

6) Quit

4

How many chips would you like to bet?

5

Press 'r' and hit enter for your first roll.

r

You rolled a 11.

You win!

Welcome to the Casino. Here are your choices:

1) Buy chips

2) Sell chips

3) Play Craps

4) Play Arup's Game of Dice

5) Status Report

6) Quit

4

How many chips would you like to bet?

10

Press 'r' and hit enter for your first roll.

r

You rolled a 5.

Press 'r' and hit enter for your first roll.

r

You rolled a 5.

Sorry, you have lost.

Welcome to the Casino. Here are your choices:

1) Buy chips

2) Sell chips

3) Play Craps

4) Play Arup's Game of Dice

5) Status Report

6) Quit

6

After selling your chips, you have $905. Thanks for playing!

(i need help writing it in c)

Explanation / Answer


#include <stdio.h>
#include <stdlib.h>
#include <time.h>

// Costs for buying and selling a chip.
#define CHIP_BUY 11
#define CHIP_SELL 10

// Used to determine the status of a game.
#define WIN 1
#define LOSS 0
#define CONTINUE 2

int pairofdice();
int craps();
int craps_outcome(int roll);
int arupsdice();
int arups_outcome(int roll);
void statusreport(int numchips, int cash);
int buychips(int *cash, int *chips);
int sellchips(int numchips);
void dobuy(int *cash, int *numchips);
void dosell(int *cash, int *numchips);

int menu();
int validchips(int *chipsbet, int numchips);

int main() {

    // Set up variables and initial values.
    int choice, cash=1000;
    int numchips=0, chipsbet, numsell;

    srand(time(0)); // Initialize random number generator.

    // Continue until the user quits.
    while ((choice = menu()) != 6) {

        // Execute the appropriate choice.
        if (choice == 1) {
            dobuy(&cash, &numchips);
        }
        else if (choice == 2) {
            dosell(&cash, &numchips);
        }
        else if (choice == 3) {

            // Only play if a valid number of chips is bet.
            if (validchips(&chipsbet, numchips)) {

                // Adjust current number of chips based on the game outcome.
                if (craps())
                    numchips += chipsbet;
                else
                    numchips -= chipsbet;
            }
        }
        else if (choice == 4) {

            // Only play if a valid number of chips is bet.
            if (validchips(&chipsbet, numchips)) {

                // Adjust current number of chips based on the game outcome.
                if (arupsdice())
                    numchips += chipsbet;
                else
                    numchips -= chipsbet;
            }
        }
        else if (choice == 5) {
            statusreport(numchips, cash);
        }
    }

    // Sell the remaining chips and print out the final message.
    cash += sellchips(numchips);
    printf("After selling your chips, you have $%d.",cash);
    printf(" Thanks for playing! ");

    return 0;
}

// Returns an integer in between 2 and 12, inclusive, representing the
// result of rolling two fair six-sided dice.
int pairofdice() {
   int randNum;
   srand(time(0));
   randNum = 2+rand()%12+1;
    return 0;
}

// Let's the user play craps. Returns 1 if the user wins, 0 otherwise.
int craps() {
   int diceCount, user;
   diceCount = pairofdice();
    if (diceCount == 7 || diceCount == 11)
       user = WIN;
    else
       user = LOSS;
    return 0;
}

// Returns the status of a craps game based on the first roll.
int craps_outcome(int roll) {
    roll = craps();
    return 0;
}

// Let's the user play Arup's Game of Dice.
// Returns 1 if the user wins, 0 otherwise.
int arupsdice() {

    int first_roll, second_roll, status;
    char dummy[50];

    // Execute the first die roll.
    printf("Press 'r' and hit enter for your first roll. ");
    scanf("%s", dummy);
    first_roll = pairofdice();
    printf("You rolled a %d. ", first_roll);

    // Determine the current game status.
    status = arups_outcome(first_roll);

    // Go ahead and execute the second die roll if necessary.
    if (status == CONTINUE) {

        // Execute the second die roll.
        printf("Press 'r' and hit enter for your next roll. ");
        scanf("%s", dummy);
        second_roll = pairofdice();
        printf("You rolled a %d. ", second_roll);

        // Determine if the user won or not.
        if (second_roll > first_roll)
            status = WIN;
        else
            status = LOSS;
    }

    // Return the final outcome of the game.
    if (status == WIN) {
        printf("You win! ");
        return WIN;
    }
    else {
        printf("Sorry, you have lost. ");
        return LOSS;
    }
}

// Returns the status of Arup's Game of Dice, based on the first roll.
int arups_outcome(int roll) {
   int diceCount;
   diceCount = arupsdice();
    if (diceCount == 11 || diceCount == 12)
       roll = WIN;
    else
       roll = LOSS;
    return 0;
}

// Prints out the user's current status.
void statusreport(int numchips, int cash) {
    printf("You currently have $ %d left "), buychips(cash);

   }

// Adjusts the variable pointed to by cash to equal the change received
// from purchasing the maximum possible number of chips for the value
// of that variable, originally. Returns the total number of purchased
// chips.
void buychips(int *cash, int *chips) {
    printf("How much cash do you want to spend for chips? ");
    scanf("%d", &cash);
    *chips = (cash)/(*CHIP_BUY);

    return buychips(*chips);

}

// Returns the cash gained by selling numchips chips.
int sellchips(int numchips) {
   numchips = numchips * CHIP_SELL;
   return sellchips(numchips);
}

// Prints out the menu for the user and returns the user's choice.
int menu() {
     int choice = 0;

printf("Welcome to the Casino. Here are your choices: ");
printf("1. Buy Chips ");
printf("2. Sell Chips ");
printf("3. Play Craps ");
printf("4. Play Arup's Game of Dice' ");
printf("5. Status Report ");
printf("6. Quit. ");

// Loop until a valid answer is entered.
while (1) {
    scanf( "%d", &choice);
    if (choice < 1 || choice > 6)
      printf("Sorry, that is not a valid choice, try again. ");
    else
      break;
}

return choice;
}

// Determines if the user has entered a valid number of chips to bet.
// chipsbet is a pointer to the variable storing the number of chips the
// user is betting and numchips is the current number of chips the user
// has. 1 is returned if the bet is valid, 0 is returned otherwise.
int validchips(int *chipsbet, int numchips) {
    if (chipsbet == numchips){
        return 1;
   }
    else if (numchips > chipsbet){
        return 1;
   }
    else{
       return 0;
    }
}

// Executes buying chips. Both cash and numchips are pointers to the
// variables storing the user's amount of cash and number of chips,
// respectively.
void dobuy(int *cash, int *numchips) {
   *cash = (CHIP_BUY)*(numchips);
   *numchips = (*cash / *numchips);
   return dobuy(cash, numchips);
   }

// Executes selling chips. Both cash and numchips are pointers to the
// variables storing the user's amount of cash and number of chips,
// respectively.
void dosell(int *cash, int *numchips) {

    int numsell;

    // Determine the number of chips to be sold.
    printf("How many chips do you want to sell? ");
    scanf("%d", &numsell);

    // Print out the error message if this is too much.
    if (numsell > *numchips)
        printf("Sorry, you do not have that many chips. No chips sold. ");

    // Execute the transaction.
    else {
        (*cash) += sellchips(numsell);
        (*numchips) -= numsell;
    }
}


sample output

Welcome to the Casino. Here are your choices:

1) Buy chips

2) Sell chips

3) Play Craps

4) Play Arup's Game of Dice

5) Status Report

6) Quit

1

How much cash do you want to spend for chips?

1100

Sorry, you do not have that much money. No chips bought.

Welcome to the Casino. Here are your choices:

1) Buy chips

2) Sell chips

3) Play Craps

4) Play Arup's Game of Dice

5) Status Report

6) Quit

1

How much cash do you want to spend for chips?

500

Welcome to the Casino. Here are your choices:

1) Buy chips

2) Sell chips

3) Play Craps

4) Play Arup's Game of Dice

5) Status Report

6) Quit

5

You currently have $505 left and 45 chips.

Welcome to the Casino. Here are your choices:

1) Buy chips

2) Sell chips

3) Play Craps

4) Play Arup's Game of Dice

5) Status Report

6) Quit

3

How many chips would you like to bet?

0

Sorry, that is not allowed. No game played

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