关于扑克牌的算法

该题目来源于:http://www.dajiangsai.org/public/kecheng/c++_yt.html

       红色标记的地方是我标注的一个重点

       实现一个算法: 将一副54张扑克牌经过洗牌后顺序发给三个人,然后将每个人的牌按降序排序。我们已经设计出了扑克牌类Card、算法类Game,并且有了测试代码Main.cpp,但算法类Game中的洗牌函数shuffle()和发牌函数deal()没有实现,请写出具体的实现代码,以使测试代码Main.cpp中输出期望的内容。

 

扑克牌类:

Card.h

#ifndef CARD_H

#define CARD_H

#include <string>

using namespace std;

//扑克牌类

   class Card

    {

       /** 花色 */

       public:

           int suit;

       /** 点数,0代表3,1代表4.... */

           int rank;

            enum{

       /** 3 */

            THREE = 0,

       /** 4 */

            FOUR = 1,

       /** 5 */

            FIVE = 2,

       /** 6 */

            SIX = 3,

       /** 7 */

            SEVEN = 4,

       /** 8 */

            EIGHT = 5,

        /** 9 */

            NINE = 6,

       /** 10 */

            TEN = 7,

       /** J */

            JACK = 8,

       /** Q */

            QUEEN = 9,

       /** K */

            KING = 10,

       /** A */

            ACE = 11,

       /** 2 */

            DEUCE = 12,

       /** 小王 */

            BLACK = 13,

       /** 大王 */

            RED = 14

       };

       enum{

       /** 方块 */

            DIAMOND = 0,

       /** 梅花 */

            CLUB = 1,

       /** 红桃 */

            HEART = 2,

       /** 黑桃 */

            SPADE = 3,

       /** 王 */

            JOKER = 4

       };

       private:

           static const string SUIT_NAMES[5];;

           static const string RANK_NAMES[15];;

       public:

           Card(){}

           Card(int s, intr):suit(s),rank(r){}

           string toString();

   };

#endif

 

Card.cpp

#include <iostream>

#include "Card.h"

const string Card::SUIT_NAMES[] = { "方块", "梅花", "红桃", "黑桃", "" };

const string Card::RANK_NAMES[] ={"3","4","5","6","7","8","9","10","J",

"Q","K","A","2","小王","大王"};

 

string Card::toString(){

   return SUIT_NAMES[suit] + RANK_NAMES[rank];

}

 

 

 

算法类:

Game.h

#ifndef GAME_H

#define GAME_H

#include "Card.h"

using namespace std;

   class Game{

       public:

           //洗牌函数,把一副牌的次序随机打乱

           void shuffle(Card* cards,int len);

           //发牌函数,第一个数组是洗牌后的结果,第二个数组是二维

           //数组的首地址,num是人数。发牌的时候每个人的牌要做排序

           void deal(Card* cards,int len,Card* ret,int num);

   };

#endif

         

 

Main.cpp

#include <iostream>

#include <cstdio>

#include <cstdlib>

#include <ctime>

#include "Game.h"

using namespace std;

 

int main()

{

       //生成牌

   Card* cards = new Card[54];

   int i = 0,j = 0;

   int rank,suit;

   for (rank = Card::THREE; rank <= Card::DEUCE; rank++)

    {

       for(suit = Card::DIAMOND;suit <= Card::SPADE;suit++){

           (cards+i)->suit = suit;

           (cards+i)->rank = rank;

           i++;

       }

    }

   (cards+i)->suit = Card::JOKER;

   (cards+i)->rank = Card::BLACK;

   i++;

   (cards+i)->suit = Card::JOKER;

   (cards+i)->rank = Card::RED;

   //洗牌

   Game* g = new Game;

   g->shuffle(cards,54);

   cout<<"一副扑克牌"<<endl;

   //发牌

   Card res[3][18];

   g->deal(cards,54,(Card*)res,3); //注意:这里的(card*)是必须的,因为二维数组名是指//向指针的指针,这里也可以写成res[0]

   cout<<"发牌之后"<<endl;

   for(i=0;i<3;i++){

       cout << "[";

       for(j=0;j<18;j++){

           cout << res[i][j].toString();

           if(j!=17) cout<<",";

       }

       cout <<"]"<< endl;

    }

}

 

         

 

Game.cpp

#include "Game.h"

//洗牌函数

void Game::shuffle(Card* cards,int len){

   srand(time(0));

   printf("%p\n",cards);

   int i;

   for (i = len-1; i >= 1; i--)

    {

       int j = rand()%i;

       Card t = cards[i];

       cards[i] = cards[j];

       cards[j] = t;

    }

}

//排序函数(冒泡)

void sort(Card* ary,int len){

   int i, j;  

   for (i = 0; i < len-1; i++)

    {

       for (j = 0; j < len-1-i; j++)

       {

           if((ary+j)->rank < (ary+j+1)->rank){

                Card k = ary[j];

                ary[j] = ary[j+1];

                ary[j+1] = k;

           }

       }

    }

}

//发牌函数

void Game::deal(Card* cards,int len,Card*ret,int num){

   int sum = len/num;

   int i,j;

   for(i=0,j=0;i<sum;i++){

              ret[i]= cards[j++];

              ret[sum+i]= cards[j++];

              ret[2*sum+i]= cards[j++];

    }

   for(i=0;i<num; i++){

              sort(ret+i*sum,sum);

    }

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值