Question

Spring 2019 ECE 103 Engineering Programming Problem Statement A standard deck of playing cards contains 52 cards. There are f
ECE 103 Engineering Programming Spring 2019 How should you handle an ace? Since an ace card can be either or in value, you mu
ECE 103 Engineering Programming Spring 2019 Requirements The program should . Prompt the user to enter a seed value for a ran
DHHCDCSDDDSHS ccccccccccccc A23456789@JQK 8Q,, 9 A K K 3 4 5 8 2 9 of 们DDDDDDDDDDDDD lHDSCCDHCSDSCD 3H KS 85 25 ied 75 our A
C Program Please! Thanks!
0 0
Add a comment Improve this question Transcribed image text
Answer #1

Complete Program:

File: hw6.c

// Header files section
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <time.h>

// define the constants
#define MAX_CARDS 52
#define MAX_RANKS 13
#define MAX_SUITS 4
#define COLS 4
#define str(s) #s

// declare an enumuration for suit
enum Suit { H, D, C, S };

// declare a structure for card
struct card
{
    char* rank;
    enum Suit suit;
    int value;
};
typedef struct card Card;

// function prototypes
void initialize(Card deck[]);
void shuffle(Card deck[]);
void findCombinationsOf21(Card deck[]);
void display(Card deck[]);

// start main function
int main()
{
    unsigned int seedValue;

    printf("Enter a seed value: ");
    scanf("%u", &seedValue);

    srand(seedValue);

    Card deck[MAX_CARDS] = { "", ' ', 0};

    initialize(deck);

    printf("\nBefore shuffling:\n");
    display(deck);

    shuffle(deck);

    printf("\nAfter shuffling:\n");
    display(deck);
      
    printf("\nFind combinations of 21:\n");
    findCombinationsOf21(deck);

    return 0;
} // end of main function

// initialize function implementation
void initialize(Card deck[])
{
    int i;

    for (i = 0; i < MAX_CARDS; i++)
    {
        if(i % MAX_RANKS == 0)
            deck[i].rank = "A";
        else if (i % MAX_RANKS == 1)
            deck[i].rank = "2";
        else if (i % MAX_RANKS == 2)
            deck[i].rank = "3";
        else if (i % MAX_RANKS == 3)
            deck[i].rank = "4";
        else if (i % MAX_RANKS == 4)
            deck[i].rank = "5";
        else if (i % MAX_RANKS == 5)
            deck[i].rank = "6";
        else if (i % MAX_RANKS == 6)
            deck[i].rank = "7";
        else if (i % MAX_RANKS == 7)
            deck[i].rank = "8";
        else if (i % MAX_RANKS == 8)
            deck[i].rank = "9";
        else if (i % MAX_RANKS == 9)
            deck[i].rank = "10";
        else if (i % MAX_RANKS == 10)
            deck[i].rank = "J";
        else if (i % MAX_RANKS == 11)
            deck[i].rank = "Q";
        else if (i % MAX_RANKS == 12)
            deck[i].rank = "K";
              
        if(i / MAX_RANKS == 0)
            deck[i].suit = H;
        else if (i / MAX_RANKS == 1)
            deck[i].suit = D;
        else if (i / MAX_RANKS == 2)
            deck[i].suit = C;
        else if (i / MAX_RANKS == 3)
            deck[i].suit = S;

        if (i % MAX_RANKS < 10)
            deck[i].value = i % MAX_RANKS + 1;
        else
            deck[i].value = 10;
    }
} // end of initialize function

// shuffle function implementation
void shuffle(Card deck[])
{
    int i, randIndex;
    Card randCard = { "", ' ', 0 };  

    for (i = 0; i < MAX_CARDS; i++)
    {
        randIndex = rand() % MAX_CARDS;

        randCard = deck[i];
        deck[i] = deck[randIndex];
        deck[randIndex] = randCard;
    }
} // end of shuffle function

// display function implementation
void display(Card deck[])
{  
    int i, j, k;

    for (i = 0; i < MAX_RANKS; i++)
    {
        for(j = 0, k = 0; j < COLS; j++, k += MAX_RANKS)
        {
            if(deck[i + k].suit == H)
                printf("%2s%2s\t", deck[i + k].rank, str(H));
            else if (deck[i + k].suit == D)
                printf("%2s%2s\t", deck[i + k].rank, str(D));
            else if (deck[i + k].suit == C)
                printf("%2s%2s\t", deck[i + k].rank, str(C));
            else if (deck[i + k].suit == S)
                printf("%2s%2s\t", deck[i + k].rank, str(S));
        }
        printf("\n");
    }
} // end of display function

// findCombinationsOf21 function implementation
void findCombinationsOf21(Card deck[])
{
    int i = 0;
    int total = 0;
    int busts = 0;
    int wins = 0;
    Card currCard = { "", ' ', 0 };

    for (i = 0; i < MAX_CARDS; i++)
    {
        currCard = deck[i];
      
        if (currCard.suit == H)
            printf("%s%s ", currCard.rank, str(H));
        else if (currCard.suit == D)
            printf("%s%s ", currCard.rank, str(D));
        else if (currCard.suit == C)
            printf("%s%s ", currCard.rank, str(C));
        else if (currCard.suit == S)
            printf("%s%s ", currCard.rank, str(S));      

        if (currCard.rank == "A")
        {
            if(total + 11 <= 21)
                total = total + 11;
            else
                total = total + 1;
        }
        else
        {
            total = total + currCard.value;
        }

        if (total > 21)
        {
            printf("BUST\n");
            total = 0;
            busts++;
        }
        else if (total == 21)
        {
            printf("WIN\n");
            total = 0;
            wins++;
        }
    }  
    printf("OUT OF CARDS\n");

    printf("\nNumber of wins = %d\n", wins);
    printf("Number of busts = %d\n", busts);
} // end of findCombinationsOf21 function

Sample Output:

Enter a seed value: 4569 Before shuffling: AH AD 2D 3 H 2H IIIIIIII OU OOOOO WND 0000000000000 ՄԻ Մ Մ Մ Մ Մ OU OVOU WND Մ Մ Մ

Add a comment
Know the answer?
Add Answer to:
C Program Please! Thanks! Spring 2019 ECE 103 Engineering Programming Problem Statement A standard deck of playi...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • NEED HELP TO CREATE A BLACKJACK GAME WITH THE UML DIAGRAM AND PROBLEM SOLVING TO GET...

    NEED HELP TO CREATE A BLACKJACK GAME WITH THE UML DIAGRAM AND PROBLEM SOLVING TO GET CODE TO RUN!! THANKS Extend the DeckofCards and the Card class in the book to implement a card game application such as BlackJack, Texas poker or others. Your game should support multiple players (up to 5 for BlackJack). You must build your game based on the Cards and DeckofCards class from the book. You need to implement the logic of the game. You can...

  • The following program simulates shuffling a deck of cards. (The line numbers are not ) part of the code). Without changing its functionality, rewrite the program by replacing the 2D array card [5...

    The following program simulates shuffling a deck of cards. (The line numbers are not ) part of the code). Without changing its functionality, rewrite the program by replacing the 2D array card [52] [2] with a double pointer char **card and replacing the code between line 12 and line 26 with a function. The function prototype could be void shuffleCards (char **, char [, char [) 1 7/Shuffling cards 2 4 using namespace std; 6 main() #include #include <cstdlib> <iostream>...

  • 7.12 (Card Shuffling and Dealing) Modify the program in Fig. 7.24 so that the card-dealing function deals a five-card...

    7.12 (Card Shuffling and Dealing) Modify the program in Fig. 7.24 so that the card-dealing function deals a five-card poker hand. Then write the following additional functions: a) Determine whether the hand contains a pair. b) Determinewhetherthehandcontainstwopairs. c) Determine whether the hand contains three of a kind (e.g., three jacks). d) Determinewhetherthehandcontainsfourofakind(e.g.,fouraces). e) Determine whether the hand contains a flush (i.e., all five cards of the same suit). f) Determine whether the hand contains a straight (i.e., five cards of...

  • C++ Purpose: To practice arrays of records, and stepwise program development. 1. Define a record data...

    C++ Purpose: To practice arrays of records, and stepwise program development. 1. Define a record data type for a single playing card. A playing card has a suit ('H','D','S',or 'C'), and a value (1 through 13). Your record data type should have two fields: a suit field of type char, and a value field of type int. 2. In main(), declare an array with space for 52 records, representing a deck of playing cards. 3. Define a function called initialize()...

  • (Card Shuffling and Dealing) Modify the program below so that the card-dealing function deals a five-card...

    (Card Shuffling and Dealing) Modify the program below so that the card-dealing function deals a five-card poker hand. Then write the following additional functions: a) Determine whether the hand contains two pairs b) Determine whether the hand contains a full house (i.e., three of a kind with pair). c) Determinewhetherthehandcontainsastraight flush (i.e.,fivecardsofconsecutivefacevalues). d) Determine whether the hand contains a flush (i.e., five of the same suit) #include <stdio.h> #include <stdlib.h> #include <time.h> #define SUITS 4 #define FACES 13 #define CARDS...

  • HELP NEED!! Can some modified the program Below in C++ So that it can do what the Problem below asked it today???? #include <iostream> #include <stdlib.h> #include <string> #include...

    HELP NEED!! Can some modified the program Below in C++ So that it can do what the Problem below asked it today???? #include <iostream> #include <stdlib.h> #include <string> #include <time.h> /* time */ #include <sstream> // for ostringstream using namespace std; // PlayingCard class class PlayingCard{ int numRank; int numSuit; string rank; string suit; public: PlayingCard(); PlayingCard(int numRank,int numSuit); void setRankString(); void setSuitString(); void setRank(int numRank); void setSuit(int numSuit); string getRank(); string getSuit(); int getRankNum(); int getSuitNum(); string getCard(); };...

  • HELP NEED!! Can some modified the program Below in C++ So that it can do what...

    HELP NEED!! Can some modified the program Below in C++ So that it can do what the Problem below asked it today???? #include <iostream> #include <stdlib.h> #include <string> #include <time.h> /* time */ #include <sstream> // for ostringstream using namespace std; // PlayingCard class class PlayingCard{ int numRank; int numSuit; string rank; string suit; public: PlayingCard(); PlayingCard(int numRank,int numSuit); void setRankString(); void setSuitString(); void setRank(int numRank); void setSuit(int numSuit); string getRank(); string getSuit(); int getRankNum(); int getSuitNum(); string getCard(); };...

  • Write a Java program to simulate Blackjack. The program should be as basic as possible while...

    Write a Java program to simulate Blackjack. The program should be as basic as possible while following the rules below. Thanks a. There is one deck of cards b. Cards are drawn randomly using (Shuffle() method) c. The value of a hand is computed by adding the values of the cards in hand d. The value of a numeric card such as four is its numerical value e. The value of a face card is 10 f. Ace is either...

  • C++ programming: Card game Can same one help me out with a example finsih and working...

    C++ programming: Card game Can same one help me out with a example finsih and working programm ? Program the basis for a card game (as a class card game): ● A card is represented by its suit/symbol (clubs, spades, hearts, diamonds) and a value (seven, eight, nine, ten, jack, queen, king, ace). ● A deck of cards consists of a pile of 32 cards that are completely connected to four players are distributed. ● Implement the following menu: ===...

  • C Programming The following code creates a deck of cards, shuffles it, and deals to players....

    C Programming The following code creates a deck of cards, shuffles it, and deals to players. This program receives command line input [1-13] for number of players and command line input [1-13] for number of cards. Please modify this code to deal cards in a poker game. Command line input must accept [2-10] players and [5] cards only per player. Please validate input. Then, display the sorted hands, and then display the sorted hands - labeling each hand with its...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT