Advertisement
juinda

Untitled

Nov 4th, 2016
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.03 KB | None | 0 0
  1. // 20161103樸克牌.cpp : 定義主控台應用程式的進入點。
  2. //
  3.  
  4. #include "stdio.h"
  5. #include<stdlib.h>
  6. #include<time.h>
  7. #include<stdbool.h>
  8. #define NUM_CARD 5
  9. #define RANK_TYPE 13
  10. #define FLOWER_TYPE 4
  11. void release_card(void);
  12. void check_card(void);
  13. void show_funtion(void);
  14. int rank[RANK_TYPE];
  15. int flower[FLOWER_TYPE];
  16. int differ_rank = 0, differ_flower = 0;
  17. bool hand_card[FLOWER_TYPE][RANK_TYPE];
  18. bool royal_flush = 0, flush = 0, straight = 0, four = 0, three = 0;
  19. int pairs;
  20. int main()
  21. {
  22. while (1)
  23. {
  24. release_card();
  25. check_card();
  26. show_funtion();
  27. }
  28. return 0;
  29. }
  30. void release_card(void)
  31. {
  32. int time = 0;
  33. for (int i = 0; i < RANK_TYPE; i++)
  34. {
  35. rank[i] = 0;
  36. for (int j = 0; j < FLOWER_TYPE; j++)
  37. hand_card[j][i] = 0;
  38. }
  39. for (int i = 0; i < FLOWER_TYPE; i++)flower[i] =0;
  40. royal_flush = 0, flush = 0, straight = 0, four = 0, three = 0,pairs=0;
  41. while (time<NUM_CARD)
  42. {
  43. char ch, rank_ch, suit_ch;
  44. bool badcard = 0;
  45. int tmprank, tmpflower;
  46. rank_ch = getchar();
  47. switch (rank_ch)
  48. {
  49. case '0': exit(0);
  50. case '2': tmprank=0; break;
  51. case '3': tmprank=1; break;
  52. case '4': tmprank=2; break;
  53. case '5': tmprank=3; break;
  54. case '6': tmprank=4; break;
  55. case '7': tmprank=5; break;
  56. case '8': tmprank=6; break;
  57. case '9': tmprank=7; break;
  58. case 't':case 'T': tmprank=8; break;
  59. case 'j':case 'J': tmprank=9; break;
  60. case 'q':case 'Q': tmprank=10; break;
  61. case 'k':case 'K': tmprank=11; break;
  62. case 'a':case 'A': tmprank=12; break;
  63. default: badcard = 1; break;
  64. }
  65. suit_ch = getchar();
  66. switch (suit_ch)
  67. {
  68. case 's':case 'S': tmpflower=3; break;
  69. case 'h':case 'H': tmpflower=2; break;
  70. case 'd':case 'D': tmpflower=1; break;
  71. case 'c':case 'C': tmpflower=0; break;
  72. default: badcard = 1; break;
  73. }
  74. while (ch = getchar() != '\n')
  75. if (ch != ' ')badcard = 1;
  76. if (badcard)printf("Bad card; ignored.\n");
  77. else if (hand_card[tmpflower][tmprank])
  78. {
  79. printf("Duplicate card; ignored.\n");
  80. continue;
  81. }
  82. else
  83. {
  84. hand_card[tmpflower][tmprank] = 1;
  85. rank[tmprank]++;
  86. flower[tmpflower]++;
  87. time++;
  88. }
  89. }
  90. }
  91. void check_card(void)
  92. {
  93. int i,keep=0;
  94. for (i = 0; i < RANK_TYPE; i++)//flush
  95. if (flower[i] == NUM_CARD)flush = 1;
  96.  
  97. for (i = 0; rank[i] == 0; i++);//straight
  98. for (i; (i < RANK_TYPE) && (rank[i] == 1); i++, keep++);
  99. if (keep == 5)straight = 1;
  100.  
  101. if (keep == 5 && i == 13 && flush == 1)royal_flush = 1;//royalflush
  102. for (i = 0; i < RANK_TYPE; i++)
  103. {
  104. if (rank[i]!=0)differ_rank++;
  105. if (rank[i] >= 4)four = 1;
  106. if (rank[i] == 3)three = 1;
  107. if (rank[i] == 2)pairs++;
  108. }
  109. }
  110. void show_funtion(void)
  111. {
  112. if (royal_flush)puts("royal flush");
  113. else if (straight&&flush)puts("straight of flush");
  114. else if (four)puts("four of kind");
  115. else if (three&&pairs==1)puts("full house");
  116. else if (straight)puts("straight");
  117. else if (three)puts("Three of kind");
  118. else if (pairs == 2)puts("two pair");
  119. else if (pairs)puts("pair");
  120. else puts("hight card");
  121. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement