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

Purpose 1) Call and return functions in and out of main. 2) Perform basic array

ID: 3850737 • Letter: P

Question

Purpose

1)         Call and return functions in and out of main.

2)         Perform basic array operations. (Creation, indexing, shuffling)

Description

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             //You are dealing with only 13 cards not 52 just an FYI

Goes at the top of your code, and:

            srand(time(NULL));

As the first line of code inside of main()

Win/loss calculation table

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

lose

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. Example: After loading the deck with values from 1-13 in the locations 0-12, your deck is loaded. Now to shuffle, as of now at location 0, value is 1 and at location 1 value is 2 and so on. Imagine rand() returns a random index 3(which has the card value 4 in it now), you have to swap this with whatever is in location 0. This was just a one-time swap; you have to do it for all the 13 locations. How do you do it??

Hint: Bubble sort logic (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.

Explanation / Answer


#include<stdio.h>
#define SIZE 13
struct Casino

{
   public:

int game_played; //game that is being played

int win_loss; //whether the player lost or won

double amount_bet ; //the amount invested in the bet

double amount ; //the amount the user is left out with

void initialise()

{

amount =100;

}

   void display_menu()

{
   int opt;
printf("choose an option");
printf("choose 1 for lucky seven ");
printf("choose 2 for Highcard ");
printf("choose 3 for Matchcard ");
scanf("%d",&game_played);
printf("enter the amount u want to bet");
scanf("%lf",&amount_bet);
  

}

   int error_check(int index)
   {
       if(index>=0 && index<SIZE && index!=-1 && amount>0)
          return 1;
       else
       return 0;
   }

void swap(int *a,int*b) //call by reference swap function

{

int temp;

temp=*b;

*b=*a;

*a=temp;

}
   void shuffle(int cards[13])
   {
           int *a[13];

for(int i=0;i<13;i++)

{
           a[i]= &cards[i];

}
           for(int i=0; i<13;i++)
           {
               cards[i]=i+1;
           }
          
           for(int i=0; i<13;i++)
           {
               int r;
               r= random(); //everytime u call the random function it gives u a random index number
               swap(&cards[r],&cards[i]); //thus all the thirteen locations are swapped
           }
   }
   int draw_card(int cards[13])
   {
       int i=0;
         
           while(a[i]==-1 && i<13)
           {
              i=i+1;
           }
           index=i;
           a[i]=-1;
           return index;
   }
  
   int lucky_seven(int a[index])
   {
       if(a[index]%7 == 0)
       {
           return 1;
       }
       else
       {
           return 0;
       }
   }
  
   int high_card(int a[index], int[random])
   {
       if(a[index]>=a[random])
       {
           return 1;
          
       }
      
       else
       {
           return 0;
       }
   }
  
   double calculate_winnings(int game_played, int win_loss, double amount_bet)
   {
       switch(game_played)
       {
           case 1:
               if(win_loss == 1)
               {
                   amount=amount+(amount_bet*7);
               }
               else
               {
                   amount=amount-amount_bet;
               }
               break;
              
           case 2:
               if(win_loss == 1)
               {
                   amount=amount+(amount_bet);
               }
               else
               {
                   amount=amount-amount_bet;
               }
               break;
           case 3:
               amount_bet=0;
               if(win_loss == 1)
               {
                   amount=amount*2;
               }
               else
               {
                   amount=0;
               }
               break;
printf("the total amount is %d",amount);

  
                       }


   }
};

main()

{

int cards[13];
struct Casino c;
c.shuffle(cards[13]);
c.display_menu();
int index; //index of the card drawn
index=c.draw_card(cards[13]);
int r;
r=c.error_check(index);
if(r==1)
{
   switch(game_played)
   {
       case 1: win_loss= lucky_seven(cards[index]);
               break;
              
       case 2: int random=random();
               win_loss= high_card(cards[index],cards[random]);
               break;
   case 3: int random=random();
               win_loss= high_card(cards[index],cards[random]);
               break;
              
   }
}
c.calculate_winnings(game_played,win_loss,amount_bet);

}

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