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

Problem Description Many assignments for novice programmers involve card games.

ID: 1716264 • Letter: P

Question

Problem Description

Many assignments for novice programmers involve card games. Central to the development of these games is the use of a virtual deck of playing cards that accurately emulates a real deck. A deck of playing cards is composed of 52 cards with thirteen ascending face values of 2, 3, … , 10, Jack, Queen, King and Ace, and four suites of Clubs, Spades, Diamonds and Hearts . To represent a card deck in our code, we will use an array of 52 integers, where the face value and the suite are encoded into the stored number and extracted using integer math. This can be done in two ways:

(i)Assume our array counts from 0 to 51, and our interpretation is that this ascending set of numbers represents each face value in all four suites, followed by the next face value in all four suites, etc. Thus, we may obtain the face value by integer division of 4 and adding 2. The suite is obtained by arbitrarily assigning 0 to Clubs, 1 to Spades, 2 to Diamonds and 3 to Hearts. The suite is then found as the remainder when dividing by 4 (using the modulo operator).

(ii)Assume our array assigns the 52 cards the following numbers with all aces being low: 0-12 (diamonds), 13-25 (hearts), 26-38 (clubs), and 39-51 (spades). So the ace of diamonds is assigned the value 0, the jack of clubs is assigned the value 36 etc. In this case, face and suite are determined via integer math using 13, divide by 13 for suite and modulus 13 to get face.

---------------------------------------------------------------------------------------------------------------------------------------

Assignment

Explain the program to the user and ask if the user wants to play the game “Card Count”. Allow the user to accept and play the game or exit.

Assuming the user wants to play, your program is to create a deck of 52 playing cards using one of the two schemes detailed above. The deck is to be shuffled randomly (see section below on generating random numbers), and then two hands of 5 cards are to be dealt to the two players (the user and the computer), alternating card dealing between the two. As each card is dealt, display each player’s updated hand. When complete, determine each players score by adding up the face values of the cards they are holding, and display these scores (Jack, Queen and King are 10 points, Ace is 11 points). The player with the highest score is to be declared the winner (allow ties). Determine if the player wants to play again; if not, exit. If so, recreate a new deck, shuffle it, and play again. Keep track of user wins, losses and ties (just for the current session of play). Your code should include appropriate pause points to allow the user to follow play. When the user choses to end the session of play, report the number of wins, losses, and ties to the screen.

In your program, you are to create and use the following functions:

a. This function accepts an array and writes to it to create a deck of 52 playing cards

b. This function accepts a deck of “size” cards (assume ) and randomly shuffles that deck by swapping two random cards in the deck

c. This function accepts an encoded number representing one of the 52 possible cards and displays the cards face and suite. Example display is below:

6 of Clubs

Ace of Diamonds

d. This function calls number of times to display a player’s hand.

e. This function removes the top card from the deck and returns it to the calling function, while shifting the rest of the deck up one card. Note that the size of the deck must be decremented to reflect the loss of one card (This should be done using Call by Reference).

f. This function adds up the points of the face cards in a deck of “size” number of cards and returns that value.

To generate random numbers:

Include stdlib.h and time.h in your preprocessor directives

In the main body of the program, after all variables have been initialized use the srand() function to seed your random number generator rand() example: srand(time(NULL));

Random_Card_Index = rand() % 52; /* random number 0 to 51 */

Explanation / Answer

Write the required C program.

#include<stdio.h>
#include<math.h>
#include<stdlib.h>
#include<iostream>
#include<time.h>
void createdeck(int [ ]);
void shuffledeck(int size, int deck[ ]);
void displaycard(int card);
void displayhand(int size, int deck[ ]);
int popCard(int size, int deck[ ]);
int findScore(int size, int deck[ ]);
int main()
{
   //DECLARE VARIABLES
   int deck[52];
   int size=52;
   char replayCh = 0;
   srand(time(NULL));//SEEDING RANDOM NUMBER GENERATOR
   //DISPLAY INFO
   printf("RULES: 1)FIVE CARDS DEALT TO EACH(USER AND COMPUTER) ");
   printf("2)FOR JACK,QUEEN,KING 10 POINTS AND FOR ACE 11 POINTS WILL BE GIVEN ");
   printf("3)HIGHEST POINT GAINER USERPLAYWINS ");
   //GET USER CHOICE
   printf("WANTS TO PLAY GAME(PRESS Y OR Y TO PLAY)) :");
   scanf("%c",&replayCh);
   if(replayCh=='y'||replayCh == 'Y')
   {
       int userPlayWin=0,userPlayLose = 0,gameDraw = 0;
       do
       {
          
           size=52;
           int userPlayScore = 0;
           int computerPlayScore = 0;
           int userPlayHand[5];
           int computerPlayHand[5];
           createdeck(deck);
           shuffledeck(52,deck);
           getchar();
           for(int k=1;k<10;k++)
           {
               //COMPUTER PLAYS
               if(k%2==0)
               {
                   computerPlayHand[(k/2)-1]=popCard(size,deck);
                   size--;
                   printf(" CARD ON COMPUTER :",(k/2));
                   displaycard(computerPlayHand[(k/2)-1]);
                   printf(" ");
               }
               //USER PLAYS
               else
               {
                   userPlayHand[k/2]=popCard(size,deck);
                   size--;
                   printf("CARD ON USER :",(k/2)+1);
                   displaycard(userPlayHand[k/2]);
               }
           }
           //FIND SCORE FOR USER  
           userPlayScore = findScore(5,userPlayHand);
           //FINS DOMPUTER SCORE
           computerPlayScore = findScore(5,computerPlayHand);
           //MAKE DECISION ON WIN / LOSE  
           if(userPlayScore>computerPlayScore)
           {
               printf("YOU WIN ");
               userPlayWin++;
           }
           else if(userPlayScore==computerPlayScore)
           {
               printf("Its a gameDraw ");
               gameDraw++;
           }
           else
           {
               printf("YOU LOSE ");
               userPlayLose++;
           }
           //DISPLAY GAME STATISTICS
           printf(" Play STATISTICS WIN : %d LOST: %d DRAW: %d ",userPlayWin,userPlayLose,gameDraw);
           printf(" WANT TO PLAY ONCE AGAIN(y/N):");
           scanf("%c",&replayCh);
       }while(replayCh=='y'||replayCh=='Y');
      
      
      
   }
   else
   {
       printf("Bye");
   }
  
   return 0;
}
//FILL DECK WITH 1 TO 52
void createdeck(int deck[ ])
{
    int k;
   for(k=0;k<52;k++)
    {
      deck[k]=k+1;  
    }
}
//SHUFFLE THE DECK RANDOMLY
void shuffledeck(int size, int deck[ ])
{
   int k;
   for(k=0;k<size;k++)
    {
          int Random_Card_Index = rand()%size;
          int t;
          t = deck[k];
          deck[k]=deck[Random_Card_Index];
          deck[Random_Card_Index]=t;
   }
}
//DISPLAY THE CARD
void displaycard(int card)
{
    int face_Value = (card%13)+1;
    int suitValue1 = (card-1)/13;
   
    if(face_Value>=1&&face_Value<=10)
    {
       printf("%d of ",face_Value);   
   }
   else
   {
       switch(face_Value)
       {
           case 11:
           printf("JACK of ");
           break;
           case 12:
           printf("QUEEN of ");
           break;
           case 13:
           printf("KING of ");          
           break;
           case 1:
           printf("ACE of ");   
       }
   }
  
   switch(suitValue1)
   {
       case 0:
       printf("DIAMONDS");
       break;
       case 1:
       printf("HEARTS");
       break;
       case 2:
       printf("CLUBS");
       break;
       case 3:
       printf("SPADES");
       break;
   }
   printf(" ");
}

void displayhand(int size, int deck[ ])
{
   int k;
   for(k=0;k<size;k++)
    {
      displaycard(deck[k]);  
   }
}

//DELETE CARD FORM DECK
int popCard(int size, int deck[ ])
{
   int newCardValue = deck[0];
   int k;
   for(k=0;k<size-1;k++)
   {
       deck[k]=deck[k+1];
   }
   return newCardValue;
}
//FIND THE SCORE FOR USER AND COMPUTER
int findScore(int size, int deck[ ])
{
   int playScore= 0 ;
   int k;
   for(k=0;k<size;k++)
   {
       switch(deck[k]%13)
       {
           case 0:
           playScore +=11;
           break;
           case 10:
           case 11:
           case 12:
           playScore +=10;
           break;
       }
   }
   return playScore;
  
}

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