• 给一手扑克牌分类


    最近看了《C语言程序设计程序 现代方法》,里面一个问题挺有意思的。原本自己写了一个,但结构混乱,示例程序条理清晰,全局变量用的挺好,故又按照书上的程序敲了一遍。

    每次读取一手五张牌,然后根据下列类别把手中的牌分类(列出的顺序依次是从最好类别到最坏类别):   
    Straight flush: 同花顺的牌(即顺序相连又都是同花色)   
    Four-of-a-kind: 四张相同的牌(四张牌级别相同)   
    Full house: 三张花色相同和两张花色相同的牌(三张牌是同样的花色,而另外两张牌是同样的花色)   
    Flush: 同花色的牌(五张牌是同花色的)   
    Straight: 同顺序的牌(五张牌的级别顺序相连)   
    Three-of-a-kind: 三张相同的牌(三张牌级别相同)   
    Two pairs: 两对子   
    Pair: 一对(两张牌级别相同)   
    High card: 其它牌(任何其它情况的牌) 

    代码如下:  

      1 #include <stdbool.h>
      2 #include <stdio.h>
      3 #include <stdlib.h>
      4 
      5 #define NUM_RANKS 13 //13种牌型
      6 #define NUM_SUITS 4
      7 #define NUM_CARDS 5  //发5张牌
      8 
      9 int num_in_rank[NUM_RANKS];
     10 int num_in_suit[NUM_SUITS];
     11 bool straight,flush,four,three;
     12 int pairs;
     13 
     14 void read_cards();
     15 void analyze_hand();
     16 void print_result();
     17 
     18 int main()
     19 {
     20     for(;;)
     21     {
     22         read_cards();
     23         analyze_hand();
     24         print_result();
     25     }
     26     return 0;
     27 }
     28 
     29 void read_cards()
     30 {
     31     bool card_exists[NUM_RANKS][NUM_SUITS];
     32     char ch,rank_ch,suit_ch;
     33     int rank,suit;
     34     bool bad_card;
     35     int cards_read = 0;
     36     for(rank = 0; rank < NUM_RANKS; rank++)
     37     {
     38         num_in_rank[rank] = 0;
     39         for(suit = 0; suit < NUM_SUITS; suit++)
     40         {
     41             card_exists[rank][suit] = false;
     42         }
     43     }
     44 
     45     for(suit = 0; suit <NUM_SUITS; suit++)
     46     {
     47         num_in_suit[suit] = 0;
     48     }
     49 
     50     while(cards_read <NUM_CARDS)
     51     {
     52         bad_card = false;
     53 
     54          printf("Enter %d piece of card
    ",cards_read+1);
     55         rank_ch = getchar();
     56         switch(rank_ch)
     57         {
     58         case '0':
     59             exit(EXIT_SUCCESS);
     60         case '2':
     61             rank = 0;
     62             break;
     63         case '3':
     64             rank = 1;
     65             break;
     66         case '4':
     67             rank = 2;
     68             break;
     69         case '5':
     70             rank = 3;
     71             break;
     72         case '6':
     73             rank = 4;
     74             break;
     75         case '7':
     76             rank = 5;
     77             break;
     78         case '8':
     79             rank = 6;
     80             break;
     81         case '9':
     82             rank = 7;
     83             break;
     84         case 't':
     85         case 'T':
     86             rank = 8;
     87             break;
     88         case 'j':
     89         case 'J':
     90             rank = 9;
     91             break;
     92         case 'q':
     93         case 'Q':
     94             rank = 10;
     95             break;
     96         case 'k':
     97         case 'K':
     98             rank = 11;
     99             break;
    100         case 'a':
    101         case 'A':
    102             rank = 12;
    103             break;
    104         default:
    105             bad_card = true;
    106         }
    107 
    108         suit_ch = getchar();
    109         switch(suit_ch)
    110         {
    111         case 'c':
    112         case 'C':
    113             suit = 0;
    114             break;
    115         case 'd':
    116         case 'D':
    117             suit = 0;
    118             break;
    119         case 'h':
    120         case 'H':
    121             suit = 0;
    122             break;
    123         case 's':
    124         case 'S':
    125             suit = 0;
    126             break;
    127         default:
    128             bad_card = true;
    129         }
    130 
    131         while((ch = getchar())!='
    ')
    132             if(ch!=' ')
    133                 bad_card = true;
    134 
    135         if(bad_card)
    136             printf("Bad card;Ignored.
    ");
    137         else if(card_exists[rank][suit])
    138         {
    139             printf("Duplicated card;Ignored.
    ");
    140         }
    141         else
    142         {
    143             num_in_rank[rank]++;
    144             num_in_suit[suit]++;
    145             card_exists[rank][suit] = true;
    146             cards_read++;
    147         }
    148     }
    149 }
    150 
    151 void analyze_hand()
    152 {
    153     int num_consec = 0;
    154     int rank,suit;
    155     straight = false;
    156     flush = false;
    157     four = false;
    158     three = false;
    159     pairs = 0;
    160     for(suit = 0; suit < NUM_SUITS; suit++)
    161     {
    162         if(num_in_suit[suit] == NUM_CARDS)
    163             flush = true;
    164     }
    165     rank = 0;
    166     while(num_in_rank[rank] == 0)
    167         rank++;
    168     for(; rank < NUM_RANKS &&num_in_rank[rank] > 0 ; rank++)
    169         num_consec++;
    170     if(num_consec == NUM_CARDS)
    171     {
    172         straight = true;
    173         return;
    174     }
    175 
    176     for(rank = 0; rank <NUM_RANKS; rank++)
    177     {
    178         if(num_in_rank[rank] == 4)
    179             four = true;
    180         if(num_in_rank[rank] == 3)
    181             three = true;
    182         if(num_in_rank[rank] == 2)
    183             pairs++;
    184     }
    185 }
    186 
    187 void print_result()
    188 {
    189     if(straight && flush)
    190         printf("Straight flush
    ");
    191     else if (four)
    192         printf("Four of a kind
    ");
    193     else if(three && pairs ==1)
    194         printf("Full house
    ");
    195     else if(flush)
    196         printf("Flush
    ");
    197     else if(straight)
    198         printf("Straight
    ");
    199     else if(three)
    200         printf("Three of a kind
    ");
    201     else if(pairs == 2)
    202         printf("Two pairs
    ");
    203     else if(pairs ==1)
    204         printf("Pair
    ");
    205     else
    206         printf("High card
    ");
    207     printf("
    ");
    208 }
  • 相关阅读:
    【bzoj3172】 Tjoi2013—单词
    【bzoj2434】 Noi2011—阿狸的打字机
    【bzoj1030】 JSOI2007—文本生成器
    【bzoj2001】 Hnoi2010—City 城市建设
    【bzoj3196】 Tyvj1730—二逼平衡树
    【bzoj3932】 CQOI2015—任务查询系统
    【bzoj3224】 Tyvj1728—普通平衡树
    【bzoj3514】 Codechef MARCH14 GERALD07加强版
    maven build脚本笔记
    jvm参数
  • 原文地址:https://www.cnblogs.com/zxy1992/p/3446363.html
Copyright © 2020-2023  润新知