Advertisement
Guest User

Untitled

a guest
Mar 24th, 2018
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 5.10 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdbool.h>
  3. #include "string.h"
  4. #include "stdlib.h"
  5. #include "time.h"
  6.  
  7. typedef enum S {CLUBS, DIAMONDS, HEARTS, SPADES} SUIT;
  8. char suits[][10] = {"CLUBS", "DIAMONDS", "HEARTS", "SPADES"};
  9.  
  10. typedef enum F {TWO, THREE, FOUR, FIVE, SIX, SEVEN, EIGHT, NINE, TEN, JACK, QUEEN, KING, ACE} FACE;
  11. char faces[][10] = {"TWO", "THREE", "FOUR", "FIVE", "SIX", "SEVEN", "EIGHT", "NINE", "TEN", "JACK", "QUEEN", "KING", "ACE"};
  12.  
  13. typedef struct
  14. {
  15.     SUIT suit;
  16.     FACE face;
  17. } card;
  18.  
  19. typedef struct
  20. {
  21.     card cards[5];
  22. } hand;
  23.  
  24. // function deals a random hand of cards - BUT .... same card CANNOT be dealt twice!
  25. // so if same card already in the hand, you would need to draw again
  26. // also, the card cannot appear in another hand either
  27. hand dealHand(hand handsDealt[], int numHandsDealt);
  28.  
  29. // returns pointer to string containing, for example, "ACE of HEARTS"
  30. char * printCard(card aCard);
  31.  
  32. // compares the FACE values of two cards (TWO is lowest, ACE is highest)
  33. // returns pointer to string containing name of winner: "You" or "Dealer" (or "Draw" if the face values are the same)
  34. char * compareCards(card yourCard, card dealersCard);
  35.  
  36. // returns true if the hand contains four ACES
  37. bool fourAces(hand aHand);
  38.  
  39.  
  40. void main()
  41. {
  42.     int i = 0;
  43.     hand myHand, dealersHand;
  44.     int seed = time(NULL);
  45.     srand(seed);
  46.     hand hands[10];
  47.  
  48.     // deal the first hand
  49.     myHand = dealHand(hands, 0);
  50.  
  51.     // add this new hand to the set of dealt hands
  52.     hands[0] = myHand;
  53.  
  54.     // deal another hand; let dealHand know what has already been dealt
  55.     dealersHand = dealHand(hands, 1);
  56.  
  57.     // here you are just comparing each card one at a time in the two hands
  58.     while(i<5)
  59.     {
  60.         printf("card#%d: %s (you) vs. %s (dealer). Winner: %s \n", i+1, printCard(myHand.cards[i]), printCard(dealersHand.cards[i]),  compareCards(myHand.cards[i], dealersHand.cards[i]));
  61.         i++;
  62.     }
  63.  
  64.     // now try to deal 4 Aces !
  65.     while(1)
  66.     {
  67.         // deal a new hand; assume new deck every time, so nothing dealt already
  68.         myHand = dealHand(hands,0);
  69.  
  70.         // does it contain 4 ACES?
  71.         if (fourAces(myHand)) break;
  72.         i++; // keep track of number of hands dealt
  73.     }
  74.     // print out how many hands it took to find 4 aces
  75.     printf ("\n\n4 aces found after %d hands \n\n\n", i);
  76. }
  77.  
  78. // compares the FACE values of two cards (TWO is lowest, ACE is highest)
  79. // returns pointer to string containing name of winner: "You" or "Dealer" (or "Draw" if the face values are the same)
  80. char * compareCards(card yourCard, card dealersCard)
  81. {
  82.     char * sptr;
  83.     sptr = (char *)malloc(10*sizeof(char));
  84.  
  85.     if(yourCard.face<dealersCard.face) strcpy(sptr ,"Dealer");
  86.     else if(dealersCard.face<yourCard.face) strcpy(sptr ,"You");
  87.     else if(dealersCard.face==yourCard.face){
  88.         if(yourCard.suit<dealersCard.suit) strcpy(sptr ,"Dealer");
  89.         else if(dealersCard.suit<yourCard.suit) strcpy(sptr ,"You");
  90.     }
  91.  
  92.  
  93.     return sptr;
  94. }
  95.  
  96. // returns pointer to string containing, for example, "ACE of HEARTS"
  97. char * printCard(card aCard)
  98. {
  99.     char * sptr;
  100.     sptr = (char *)malloc(18*sizeof(char));
  101.     strcat(sptr, faces[aCard.face]);
  102.     strcat(sptr, " of ");
  103.     strcat(sptr, faces[aCard.suit]);
  104.  
  105.     return sptr;
  106. }
  107.  
  108. // function deals a random hand of cards - BUT .... same card CANNOT be dealt twice!
  109. // so if same card already in the hand, you would need to draw again
  110. // also, the card cannot appear in another hand either
  111. hand dealHand(hand handsDealt[], int numHands)
  112. {
  113.     int i=0;
  114.     int found = 0;
  115.     hand newHand;
  116.     int ncards = 0;
  117.     bool redeal=false;
  118.  
  119.     SUIT newSuit;
  120.     FACE newFace;
  121.  
  122.     while(found<5) {
  123.         newSuit = (SUIT) rand() % (SPADES + 1);
  124.         newFace = (FACE) rand() % (ACE + 1);
  125.  
  126.         while (ncards < numHands) {
  127.             if (newSuit == newHand.cards[ncards].suit) {
  128.                 ncards = 0;
  129.                 redeal = true;
  130.                 break;
  131.             }
  132.             if (newFace == newHand.cards[ncards].face) {
  133.                 ncards = 0;
  134.                 redeal = true;
  135.                 break;
  136.             }
  137.             ncards++;
  138.         }
  139.         if (numHands != 0) {
  140.             ncards = 0;
  141.             while (i < numHands) {
  142.                 if (newSuit == handsDealt[i].cards[ncards].suit) {
  143.                     ncards = 0;
  144.                     redeal = true;
  145.                     break;
  146.                 }
  147.                 if (newFace == handsDealt[i].cards[ncards].face) {
  148.                     ncards = 0;
  149.                     redeal = true;
  150.                     break;
  151.                 }
  152.                 i++;
  153.             }
  154.             if (redeal) {
  155.                 continue;
  156.             }
  157.  
  158.             newHand.cards[found].face = newFace;
  159.             newHand.cards[found].suit = newSuit;
  160.             found++;
  161.         }
  162.     }
  163.  
  164.     return newHand;
  165. }
  166.  
  167. // returns true if the hand contains four ACES
  168. bool fourAces(hand aHand)
  169. {
  170.     int i;
  171.     int numAces = 0;
  172.     bool fourAces = false;
  173.  
  174.     // your code goes here
  175.  
  176.     return fourAces;
  177.  
  178. }
  179. //by Jaroslaw Janas
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement