20111109编程练习1:洗牌和发牌

 

//shuffle and deal 52 cards

#include <iostream.h>
#include <iomanip.h>
#include <stdlib.h>
#include <time.h>

void shuffle(int [][13]);
void deal(const int [][13], const char *[], const char *[], int [][2]);

void pair(const int [][2], const char *[]);
void threeOfHand(const int [][2], const char *[]);
void fourOfHand(const int [][2], const char *[]);
void flushHand(const int[][2], const char *[]);
void straightHand(const int[][2]);

int main()
{
 const char *suit[4] = {"Hearts", "Spades", "Diamonds", "Clubs"};
 const char *face[13] = {"Ace", "Deuce", "Three", "Four", "Five", "Six", "Seven","Eight", "Nine", "Ten", "Jack", "Queen", "King"};
 int deck[4][13] = {0}, hand[5][2] = {0};
  srand(time(0));
  shuffle(deck);
  deal(deck, suit, face, hand);
  pair(hand, face);
  threeOfHand(hand, face);
  fourOfHand(hand, face);
 flushHand(hand, suit);

 straightHand(hand);

 return 0;
}

void shuffle(int sDeck[][13]) 
{
 int row, column;
 for (int card = 1; card <= 52; card++)
 {
  do
  {
   row = rand() % 4;
   column = rand() % 13;
  }while(sDeck[row][column] != 0);                                                                                                               // shuffle the cards: if meet the previous card, rand again.
  sDeck[row][column] = card;//card number chosen
 }
}

void deal(const int dDeck[][13], const char *dSuit[], const char *dFace[], int dHand[][2])
{
 int r = 0;
 for (int card = 1; card <= 5; card++)                                                                                                               // get five hand cards
 {
  for (int row = 0; row <=3; row++)
  {
   for (int column = 0; column <= 12; column++)
   {
    if (dDeck[row][column] == card)
    {
     dHand[r][0] = row;
     dHand[r][1] = column;
     cout << setw(5) << setiosflags(ios::right) << dFace[column] << " of "
      << setw(8) << setiosflags(ios::left) << dSuit[row]
      << (card % 2 == 0 ? '\n' : '\t');
     r++;
    }
   }
  }
 }
 cout << endl;
}

void pair(const int dHand[][2], const char *dFace[])                                       //判断手中有没有对牌
{
 int count[13] = {0};
 for (int r = 0; r < 5; r++)
 {
  ++count[dHand[r][1]];
 }
 //cout << endl;
 for(int i = 0; i < 13; i++)
  if(count[i] == 2)
   cout << "the hand contains a pair of "<< dFace[i] << endl;
}

void threeOfHand(const int dHand[][2], const char *dFace[])          // 判断有没有三张同号牌
{
 int count[13] = {0};
 for (int r = 0; r < 5; r++)
 {
  ++count[dHand[r][1]];
 }
 //cout << endl;
 for(int i = 0; i < 13; i++)
  if(count[i] == 3)
   cout << "the hand contains three of "<< dFace[i] << endl;
}

void fourOfHand(const int dHand[][2], const char *dFace[])          // 四张同号牌
{
 int count[13] = {0};
 for (int r = 0; r < 5; r++)
 {
  ++count[dHand[r][1]];
 }
 //cout << endl;
 for(int i = 0; i < 13; i++)
  if(count[i] == 4)
   cout << "the hand contains four of "<< dFace[i] << endl;
}

void flushHand(const int dHand[][2], const char *suit[])                   //清一色
{
 int count[4] = {0};
 for(int r = 0; r < 5; r++)
  ++count[dHand[r][0]];
 for (int p = 0; p < 5; p++)
 {
  if(count[p] == 5)
   cout << "the hand has a flush of "<< "suit[p]"<< endl;
 }
}

void straightHand(const int dHand[][2])                                               // 同花顺
{
 int s[5] = {0};
 for (int i = 0; i < 5; i++)
 {
  s[i] = dHand[i][1];
 }
 for (int pass = 1; pass <= 4; pass++)
 {
  for(int j = 0; j < 5; j++)
  {
   if (s[j] > s[j+1])
   {
    int hold = s[j];
    s[j] = s[j+1];
    s[j+1] = hold;
   }
  }
 }
 if(s[4]-1 == s[3] && s[3]-1 == s[2] && s[2]-1 == s[1] && s[1]-1 == s[0])
  cout << "the hand contains a straight" << endl;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值