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

In C! Description ( I need help immediately!!!!) Build a casino where the user c

ID: 3863216 • Letter: I

Question

In C! Description ( I need help immediately!!!!)

Build a casino where the user can play the following games.

1)   Lucky Seven

2)   High Card

3)   Match Card (Bonus)

The user starts with $100. The user is then prompted to pick a game. All inputs must be error checked. The user is then asked how much they want to bet on the game. (Cannot be 0 or more than the user currently has.) Depending on the user’s outcome, the user either wins or loses the amount they bet. All the games require a deck of cards (simulated with an array). The cards will need to be shuffled before each game. The user is kicked out of the casino if they have no money left.

Game Descriptions

Lucky Seven

                           The user draws a card. If the card value is equal to 7, the user wins 7X the amount bet. If the card value is not 7, the user loses the amount bet.

High Card

                           The user draws a card from the deck, and the computer picks a random (different) card. If the computer’s card is higher, then the player loses. If the player’s card is higher, then the player wins. For this version, all cards are different numbers.

             Match Card (Bonus Only)

                           The user does not bet for this game. Works similar to high card, except there will be 4 “decks of cards” in play. (Use a 2D array) The user will choose a deck and a card. The computer will pick a random card from a random deck. If the value of the user’s card matches the computer’s card


, then the user doubles their total money. If the user loses, then the user loses everything.

Random Number Generator

Use the rand() function from to generate random numbers. The output will need to be between 0-12 for card operations. Use rand() and the % operator. SIZE is a global constant. Treat it like a variable that you can’t change. Use it for all card operations where you need the value for size of the deck.

To use the functions srand and rand:

#include

#include

#define SIZE 13            

Goes at the top of your code, and:

             srand(time(NULL));

As the first line of code inside of main()

Win/loss calculation table

win lose

Lucky 7

+ 7 * AmountBet

AmountBet

High Card

+ AmountBet

- AmountBet

Match Card (Bonus)

+ totalMoney

-totalMoney

Example

For Lucky Seven, if the user has an initial amount of $100, bets $25 and wins, then total = 100 + 7 * 25->$275

             If they lose, then the total = 100 - 25 -> $75

Function Prototypes & Descriptions

void display_menu();

/*

    Displays the menu for choosing a game. Should only have printf statements.

    Arguments – None

    Return Nothing

*/

int error_check(int);

/*

    Argument – int: option chosen in main

   Check if the options chosen is valid or not

    Returns 1 if input is valid, 0 otherwise.

*/

void shuffle(int[]);

/* Argument – int Array signifying deck of cards, which was created in main

    Return Nothing

    Takes in the deck of cards array, loads with values from 1-13 and shuffles the order. Do not create a new array. Hint: Pass by reference!

Remember: Index of an array starts from location 0, your deck must have card values from 1-13. To shuffle the order use rand() function to get a random index and then swap the values. Hint: Bubble sort(but you are not sorting anything here).

*/

int draw_card(int[]);

/*

    Picks the first card that hasn’t been drawn already, marks it as “taken” and

    returns the value of the card.

    Change value to -1 at the location to indicate “taken”

    Arguments— int[] deck of cards, which was created in main()

    return – int: value of card drawn.

*/

int lucky_seven(int);

/*

    Implements lucky seven game logic. Takes in one card value and checks if it is equal to 7.

    return 1 if value matches indicating the user won, return 0 if user loses.

*/

int high_card(int, int);

/*

    High Card game logic. Takes in card values and checks which one is higher.

    return 1 if user card is greater than dealer card indicating user wins, return 0 if user loses.

*/

double calculate_winnings(int, int, double);

/*

    Calculates winnings based on game played and if the user won.

    Arguments- int: game played, int: win or loss, double: amount bet

    return double: amount won or lost.

    Refer the table above.

*/

int match_card(int, int); // Bonus

/*

    Game is only played when the user decides to quit. (Give them a last minute chance to double their money)

    Takes in card values and checks if they are the same.

    return 1 if user wins, return 0 if user loses

*/

int main(void) {}

/*

    Acts as user interaction, creates arrays, calls other functions, keeps track of user’s total money.

             First display the menu of games to play.
             Declare all the variable you need to use and also the array representing the deck

          Every time a game is played, display the amount the user has left and ask the user to enter the amount he wants to bet. Assume the user won’t mess up (brownie points if you check this too).

         Next, display the result of the game (display if the user won or lost, display the card(s) drawn, and the amount of money gained/lost).

Continue to play the game as long as user does not choose to exit and still has money.

    Display final amount of money when the user quits or gets thrown out.

             For the bonus, create a 2D Array representing 4 decks of 13 cards each. Shuffle each deck and choose two random cards from two different decks. Display cards drawn and if the user won or lost.

*/

Explanation / Answer

Here is the implementation for you:

//Function Prototypes & Descriptions
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define SIZE 13
void display_menu()
/*
Displays the menu for choosing a game. Should only have printf statements.
Arguments – None
Return Nothing
*/
{
    printf("1. Lucky Seven. 2. High Card. 3. Match Card & Quit. 4. Quit. ");
}
int error_check(int option)
/*
Argument – int: option chosen in main
Check if the options chosen is valid or not
Returns 1 if input is valid, 0 otherwise.
*/
{
    if(option < 1 || option > 4)
        return 0;
    return 1;  
}
void shuffle(int deck[])
/* Argument – int Array signifying deck of cards, which was created in main
Return Nothing
Takes in the deck of cards array, loads with values from 1-SIZE and shuffles the order.
Do not create a new array. Hint: Pass by reference!
   Remember: Index of an array starts from location 0, your deck must have card values from 1-SIZE.
   To shuffle the order use rand() function to get a random index and then swap the values.
   Hint: Bubble sort(but you are not sorting anything here).
*/
{
    for(int i = 0; i < SIZE; i++)
        deck[i] = i+1;
    for(int i = 0; i < rand()%SIZE*SIZE; i++)
    {
       int index1 = rand() % SIZE;
       int index2 = rand() % SIZE;
       int temp = deck[index1];
       deck[index1] = deck[index2];
       deck[index2] = temp;
    }  
}
int draw_card(int deck[])
/*
Picks the first card that hasn’t been drawn already, marks it as “taken” and
returns the value of the card.
Change value to -1 at the location to indicate “taken”
Arguments— int[] deck of cards, which was created in main()
return – int: value of card drawn.
*/
{
    for(int i = 0; i < SIZE; i++)
    {
       if(deck[i] != -1)
       {
          int temp = deck[i];
          deck[i] = -1;
          return temp;
       }
    }
    return -1;
}
int lucky_seven(int value)
/*
Implements lucky seven game logic. Takes in one card value and checks if it is equal to 7.
return 1 if value matches indicating the user won, return 0 if user loses.
*/
{
    return value == 7;
}
int high_card(int user, int dealer)
/*
High Card game logic. Takes in card values and checks which one is higher.
return 1 if user card is greater than dealer card indicating user wins, return 0 if user loses.
*/
{
    return user > dealer;
}
double calculate_winnings(int gameChoice, int result, double amountBet)
/*
Calculates winnings based on game played and if the user won.
Arguments- int: game played, int: win or loss, double: amount bet
return double: amount won or lost.
Refer the table above.
*/
{
    if(gameChoice == 1)
        if(result)
            return 7*amountBet;
        else
            return -amountBet;
    else //if(gameChoice == 2)
        if(result)
            return amountBet;
        else
            return -amountBet;
    /*else
        if(result)
            return    amountBet;   */              
}
int match_card(int user, int dealer) // Bonus
/*
Game is only played when the user decides to quit. (Give them a last minute chance to double their money)
Takes in card values and checks if they are the same.
return 1 if user wins, return 0 if user loses
*/
{
    return user == dealer;
}
int main(void)
/*
Acts as user interaction, creates arrays, calls other functions, keeps track of user’s total money.
First display the menu of games to play. Declare all the variable you need to use and
also the array representing the deck. Every time a game is played, display the amount
the user has left and ask the user to enter the amount he wants to bet. Assume the
user won’t mess up (brownie points if you check this too). Next, display the result of
the game (display if the user won or lost, display the card(s) drawn, and the amount
of money gained/lost). Continue to play the game as long as user does not choose to
exit and still has money. Display final amount of money when the user quits or gets
thrown out.
For the bonus, create a 2D Array representing 4 decks of SIZE cards each.
Shuffle each deck and choose two random cards from two different decks.
Display cards drawn and if the user won or lost.
*/
{
    srand(time(NULL));
    int deck[SIZE], userCard, dealerCard, gameChoice, userWon;
    float totalMoney = 100, betAmount;
    while(totalMoney > 0)
    {
        display_menu();
        printf("Choose your option: ");
        scanf("%d", &gameChoice);
        while(!error_check(gameChoice))
        {
           printf("Invalid selection. Try again. ");
           display_menu();
            printf("Choose your option: ");
            scanf("%d", &gameChoice);
        }
        if(gameChoice == 1 || gameChoice == 2)
        {
           printf("Enter your bet amount: ");
            scanf("%f", &betAmount);
            while(betAmount > totalMoney)
            {
               printf("You are left with only $%.2f, and can't bet more than what you have. ", totalMoney);
               printf("Enter your bet amount: ");
                scanf("%f", &betAmount);
            }
        }
        else if(gameChoice == 3)
            betAmount = totalMoney;
        else
        {
           printf("Your ended up with $%.2f ", totalMoney);
           return 0;     
        }  
        shuffle(deck);
        userCard = draw_card(deck);
        if(gameChoice == 3)
            shuffle(deck);
        dealerCard = draw_card(deck);
        if(gameChoice == 1)
            userWon = lucky_seven(userCard);
        else if(gameChoice == 2)
            userWon = high_card(userCard, dealerCard);
        else
            userWon = match_card(userCard, dealerCard);
        totalMoney += calculate_winnings(gameChoice, userWon, betAmount);  
        if(gameChoice == 3)
        {
           printf("Your ended up with $%.2f ", totalMoney);
           return 0;  
        }      
    }
}