Advertisement
metalx1000

Deck of Cards with James

Dec 20th, 2016
648
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.72 KB | None | 0 0
  1. #ifndef __CARD_H__
  2. #define __CARD_H__
  3.  
  4. #include <stdlib.h>
  5. #include <stdio.h>
  6. #include<time.h>
  7.  
  8.  
  9. typedef enum {HEARTS, CLUBS, SPADES, DIMONDS} suit_t;
  10.  
  11. typedef struct card_s {
  12.   suit_t suit;
  13.   int value;
  14. } *card_t;
  15.  
  16. typedef card_t deck[52];
  17.  
  18. card_t Make_Card(suit_t suit, int value)
  19. {
  20.   card_t tmp;
  21.   if(value > 13 || value < 1){
  22.     fprintf(stderr,"Value Must Be Between 1 and 13\n");
  23.     exit(1);
  24.   }
  25.   tmp = (card_t)(malloc(sizeof(struct card_s)));
  26.   tmp->suit = suit;
  27.   tmp->value = value;
  28.   return tmp;
  29. }
  30.  
  31. void Print_Suit(FILE* fp, suit_t suit)
  32. {
  33.   if (suit == HEARTS) {
  34.     fprintf(fp, "❤");
  35.   } else if (suit == CLUBS) {
  36.     fprintf(fp, "♣");
  37.   } else if (suit == DIMONDS) {
  38.     fprintf(fp, "♦");
  39.   } else if (suit == SPADES) {
  40.     fprintf(fp, "♠");
  41.   } else {
  42.     fprintf(fp, "NAN");
  43.   }
  44. }
  45.  
  46. void Print_Card(FILE* fp, card_t card)
  47. {
  48.   if(card->value == 1){
  49.     fprintf(fp, " A");
  50.   }
  51.   else if(card->value == 11){
  52.     fprintf(fp, " J");
  53.   }
  54.   else if(card->value == 12){
  55.     fprintf(fp, " Q");
  56.   }
  57.   else if(card->value == 13){
  58.     fprintf(fp, " K");
  59.   }
  60.   else if(card->value == 10){
  61.     fprintf(fp, "10");
  62.   }
  63.   else{
  64.     fprintf(fp, " %d", card->value);
  65.   }
  66.  
  67.   Print_Suit(fp, card->suit);
  68.   fprintf(fp, "\n");
  69. }
  70.  
  71. void Create_Deck(deck mydeck)
  72. {
  73.   int i;
  74.   for (i=0;i<13;i++){
  75.     mydeck[i*4+0] = Make_Card(DIMONDS, i+1);
  76.     mydeck[i*4+1] = Make_Card(CLUBS, i+1);
  77.     mydeck[i*4+2] = Make_Card(SPADES, i+1);
  78.     mydeck[i*4+3] = Make_Card(HEARTS, i+1);
  79.   }
  80.  
  81.  
  82. }
  83.  
  84. int main()
  85. {
  86.   int i;
  87.   deck mydeck;
  88.   Create_Deck(mydeck);
  89.  
  90.   srand(time(0));  
  91.  
  92.   for(i=0;i<5;i++){  
  93.     int c = rand()%52;
  94.     Print_Card(stdout, mydeck[c]);
  95.   }
  96. }
  97.  
  98.  
  99.  
  100. #endif
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement