zoukankan      html  css  js  c++  java
  • C++分享笔记:扑克牌的洗牌发牌游戏设计

      笔者在大学二年级期间,做过的一次C++程序设计:扑克牌的洗牌发牌游戏。具体内容是:除去大王和小王,将52张扑克牌洗牌,并发出5张牌。然后判断这5张牌中有几张相同大小的牌,是否是一条链,有几个同花等。

      笔者在学习他人设计的基础上,完成了自己的程序设计。这里将源程序分享给大家。

    [1] 文件“card.h”

     1 #ifndef CARD_H
     2 #define CARD_H
     3 #include<string>
     4 using namespace std;
     5 
     6 class Card
     7 {
     8 public:
     9     static const int totalFaces=13;
    10     static const int totalSuits=4;
    11 
    12     Card(int,int);
    13     int getFace();
    14     int getSuit();
    15     string toString();
    16 
    17 private:
    18     int face,suit;
    19     static const string faceNames[totalFaces];
    20     static const string suitNames[totalSuits];
    21 
    22 };
    23 #endif // CARD_H

    [2] 文件“card.cpp”

     1 #include"card.h"
     2 
     3 Card::Card(int faceNumber,int suitNumber)
     4 {
     5     face=faceNumber;
     6     suit=suitNumber;
     7 }
     8 
     9 string Card::toString()
    10 {
    11     return faceNames[face]+" of "+suitNames[suit];
    12 }
    13 
    14 int Card::getFace() {return face;}
    15 int Card::getSuit() {return suit;}
    16 
    17 const string Card::faceNames[totalFaces]=
    18 {"A","2","3","4","5","6","7","8","9","10","J","Q","K"};
    19 
    20 const string Card::suitNames[totalSuits]=
    21 {"heart","diamond","club","spade"};

    [3] 文件“deckofcards.h”

     1 #ifndef DECKOFCARDS_H
     2 #define DECKOFCARDS_H
     3 #include<vector>
     4 #include"card.h"
     5 using namespace std;
     6 
     7 class DeckOfCards
     8 {
     9 public:
    10     static const int fiveCards=5;
    11     static const int allCards=52;
    12     DeckOfCards();
    13     void shuffle();
    14     Card dealCard(int);
    15 
    16     void sortCards();
    17     int duiziCards(int* sortFaces);
    18     //bool moreCards();
    19 
    20 private:
    21     vector<Card> deck;
    22     int currentCard;
    23 
    24 };
    25 #endif // DECKOFCARDS_H

    [4] 文件“deckofcards.cpp”

      1 #include"deckofcards.h"
      2 #include<cstdlib>
      3 #include<ctime>
      4 
      5 DeckOfCards::DeckOfCards()
      6 {
      7     //currentCard=0;
      8 
      9     for(int i=0;i<allCards;i++)
     10     {
     11         Card card(i%Card::totalFaces,i/Card::totalFaces);
     12         deck.push_back(card);
     13     }
     14 }
     15 
     16 void DeckOfCards::shuffle()
     17 {
     18     srand(time(0));
     19 
     20     int swapRandom[allCards];
     21 
     22     for(int i=0;i<allCards;i++)
     23         swapRandom[i]=int(rand())%allCards;
     24 
     25     for(int i=0;i<allCards;i++)
     26     {
     27         Card swapCard(0,0);
     28         swapCard=deck[i];
     29         deck[i]=deck[swapRandom[i]];
     30         deck[swapRandom[i]]=swapCard;
     31     }
     32 
     33 }
     34 
     35 Card DeckOfCards::dealCard(int how_many)
     36 {
     37     for(int i=0;i<how_many;i++)
     38     {
     39         cout <<deck[i].toString() <<endl;
     40         if((i+1)%13==0) cout <<endl;
     41     }
     42 }
     43 
     44 void DeckOfCards::sortCards()
     45 {
     46     int sortFaces[fiveCards];
     47     int sortSuits[fiveCards];
     48     for(int i=0;i<fiveCards;i++)
     49     {
     50         sortFaces[i]=deck[i].getFace();
     51         sortSuits[i]=deck[i].getSuit();
     52     }
     53 
     54     for(int i=fiveCards-1;i>=0;i--)
     55         for(int j=0;j<=i-1;j++)
     56             if(sortFaces[j]>sortFaces[j+1])
     57             {
     58                 int t;
     59                 t=sortFaces[j];
     60                 sortFaces[j]=sortFaces[j+1];
     61                 sortFaces[j+1]=t;
     62             }
     63 
     64     if((sortFaces[0]==sortFaces[1]&&sortFaces[0]==sortFaces[2]&&sortFaces[0]==sortFaces[3])||
     65       (sortFaces[1]==sortFaces[2]&&sortFaces[1]==sortFaces[3]&&sortFaces[1]==sortFaces[4]))
     66         cout <<"There are 4 same cards." <<endl;
     67 
     68     else if((sortFaces[0]==sortFaces[1]&&sortFaces[0]==sortFaces[2])||
     69            (sortFaces[1]==sortFaces[2]&&sortFaces[1]==sortFaces[3])||
     70            (sortFaces[2]==sortFaces[3]&&sortFaces[2]==sortFaces[4]))
     71         cout <<"There are 3 same cards." <<endl;
     72 
     73     else if(int i=duiziCards(sortFaces))
     74         cout <<"There are " <<i <<" pairs." <<endl;
     75 
     76     else
     77         cout <<"There is no same cards." <<endl;
     78 
     79     if(sortFaces[0]+1==sortFaces[1]&&sortFaces[1]+1==sortFaces[2]&&
     80        sortFaces[2]+1==sortFaces[3]&&sortFaces[3]+1==sortFaces[4])
     81         cout <<"The five cards is a straight." <<endl;
     82     else
     83         cout <<"The five cards is not a straight." <<endl;
     84 
     85     if(sortSuits[0]==sortSuits[1]&&sortSuits[0]==sortSuits[2]&&
     86        sortSuits[0]==sortSuits[3]&&sortSuits[0]==sortSuits[4])
     87         cout <<"The five cards have same flower." <<endl;
     88     else
     89         cout <<"The five cards have different flower." <<endl;
     90 
     91 }
     92 
     93 int DeckOfCards::duiziCards(int* sortFaces)
     94 {
     95     int duiziNum=0;
     96 
     97     for(int i=0;i<fiveCards-1;i++)
     98         if(sortFaces[i]==sortFaces[i+1]) duiziNum++;
     99 
    100     return duiziNum;
    101 }

    [5] main函数文件“main_FiveCards.cpp”

     1 #include<iostream>
     2 #include"card.cpp"
     3 #include"deckofcards.cpp"
     4 using namespace std;
     5 
     6 int main()
     7 {
     8     DeckOfCards aDeck;
     9 
    10     cout <<"Deal all cards before shuffle." <<endl;
    11     aDeck.dealCard(aDeck.allCards);
    12 
    13     aDeck.shuffle();
    14 
    15     cout <<"Deal five cards after shuffle." <<endl;
    16     aDeck.dealCard(aDeck.fiveCards);
    17 
    18     cout <<"
    The result after sorting." <<endl;
    19     aDeck.sortCards();
    20 
    21     return 0;
    22 }

      将5个文件放在同一目录下,对文件“main_FiveCards.cpp”编译运行即可。以下是在CodeBlocks中的运行结果:

      

    注:如有疑问或者高见,欢迎各位读者与笔者交流,以期共同学习进步。

  • 相关阅读:
    2016/1/24 笔记 集合类 异常
    2016/1/22 3,将id为005的对象从集合中移除
    2016/1/22 1, 1-100 放集合 特定对象移除 2,List集合和Set集合是否可以重复添加
    2016/1/21 练习 arraylist 1,添加 add() 2,遍历集合
    2016/1/21 练习 创建 接口interface 应用implements 类class 并实例化调用
    2016/1/21 解读泛型
    2016/1/20 笔记 1, 包 引入 static 已经补充到类里 2,继承
    2016/1/20 总结构建子类对象时的顺序
    2016/1/20 继承作业
    笔记练习
  • 原文地址:https://www.cnblogs.com/EarthPioneer/p/9163247.html
Copyright © 2011-2022 走看看