Advertisement
Guest User

Untitled

a guest
Oct 22nd, 2014
130
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.95 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <time.h>
  3. #include <stdlib.h>
  4.  
  5.  
  6. /*
  7. GLOBAL VARIABLES
  8. */
  9.  
  10. #define CARDS_NUM 13
  11. #define CARDS_IN_DECK 52
  12.  
  13. typedef struct {
  14. int value;
  15. int representation;
  16. } card_t;
  17.  
  18. typedef struct {
  19. card_t * cards;
  20. } deck_t;
  21.  
  22. /*
  23. PRIMITIVES
  24. */
  25.  
  26. void constructorDeckT(deck_t *);
  27. char * stringOfCard(card_t);
  28. int valueOfCard(card_t);
  29. void printDeck(deck_t);
  30.  
  31. /*
  32. FUNCTIONS
  33. */
  34.  
  35. int main(int argc, char*argv[]) {
  36. deck_t mazzo;
  37. constructorDeckT(&mazzo);
  38. printDeck(mazzo);
  39. }
  40.  
  41. char * stringOfCard(card_t card) {
  42. switch (card.representation) {
  43. case 1:
  44. return "A\0";
  45. case 2:
  46. return "2\0";
  47. case 3:
  48. return "3\0";
  49. case 4:
  50. return "4\0";
  51. case 5:
  52. return "5\0";
  53. case 6:
  54. return "6\0";
  55. case 7:
  56. return "7\0";
  57. case 8:
  58. return "8\0";
  59. case 9:
  60. return "9\0";
  61. case 10:
  62. return "10\0";
  63. case 11:
  64. return "J\0";
  65. case 12:
  66. return "Q\0";
  67. case 13:
  68. return "K\0";
  69. default:
  70. return "?\0";
  71. }
  72. }
  73.  
  74. int valueOfCard(card_t card) {
  75. if (card.representation > 0 && card.representation < 11)
  76. return card.representation;
  77. else
  78. return 10;
  79. }
  80.  
  81. void constructorDeckT(deck_t * deck) {
  82. deck->cards = malloc(sizeof(CARDS_IN_DECK));
  83. int i, cardValue, cont;
  84. for (cardValue = 1, i = 0; i < CARDS_IN_DECK; cardValue++) {
  85. for(cont = 0; cont < 4; ) {
  86. deck->cards[i].representation = cardValue;
  87. deck->cards[i].value = valueOfCard(deck->cards[i]);
  88. cont++;
  89. i++;
  90. }
  91. cont = 0;
  92. }
  93. }
  94.  
  95.  
  96. void printDeck(deck_t deck) {
  97. int i;
  98. for (i = 0; i < CARDS_IN_DECK; i++) {
  99. printf("Card %d = %s\n", i+1, stringOfCard(deck.cards[i]));
  100. }
  101. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement