三张牌的游戏,C/C++实现

此程序为模拟扑克牌游戏!先由计算机自动洗牌,再发按规则发牌。具体游戏规则如下:*1.有两个人玩,分别为A和B;*2.一副扑克牌有52张牌,4种花色(方块、梅花、红桃和黑桃... 此程序为模拟扑克牌游戏!先由计算机自动洗牌,再发按规则发牌。具体游戏规则如下:
* 1.有两个人玩,分别为A和B;
* 2.一副扑克牌有52张牌,4种花色(方块、梅花、红桃和黑桃),每种花色的牌的点数按升序排列
* 有2,3,4,...,10,J,K,A等13种;
* 3.给每个人发3张牌,牌面向上(即,亮牌),赢者立即可以确定;
* 4.最高等级的一手牌称为同花顺,即3张牌均为同一种花色,最大的同花牌是同一种花色的Q、K、A;
* 5.第二等级的牌称为顺子,即点数相同的3张牌,最大的顺子是花色不同的Q、K、A;
* 6.第三等级的牌是同点,即点数相同的3张牌,最大的同点是A、A、A;
* 7.第四等级的牌是对子,即3张牌中有两张牌点数相同,最大的对子是A、A、K;
* 8.第五等级的牌是杂牌,即除去上列四等之外的任何一手牌,最大的杂牌是不同花色的A、K、J;
* 9.若两人的牌类型不同,则等级高者胜;若等级相同,则点数高者胜;若点数也相同,则为平局。
在VC6.0上运行,通过的再加上100分,发到我的邮箱zyxszzx@163.com
展开
 我来答
akyeren
推荐于2016-06-21 · TA获得超过188个赞
知道小有建树答主
回答量:130
采纳率:0%
帮助的人:0
展开全部
  #include <iostream>
  #include <assert.h>
  #include <stdlib.h>
  #include <time.h>

  using namespace std;

  typedef enum {heart,diamond,club,spade} Suit;
  typedef enum {two,three,four,five,six,seven,eight,nine,ten,jack,queen,king,ace} FaceValue;
  typedef enum {uninitialized = -1,zitch=0, onePair, threeOfAKind, straight, straightFlush } PokerHandsRanking;

  static char *suitName[4]={"\03","\04","\05","\06"}; //suit names {heart, diamond, club, spade}
  static char *cardName[13]={"2","3","4","5","6","7","8",
  "9","10","J","Q","K","A"}; //holds strings with card names
  static char *playerName[4]={"A","B","C","D"}; // 玩家名

  static char *rankingName[5]={"杂牌","一对","三条","顺","同花顺"};

  class Card
  {
  friend ostream& operator <<(ostream&,Card);
  public:
  Card(){cardSuit=Suit(0);cardFaceValue=FaceValue(0);} // default constructor
  Card(const Card& a){cardSuit=a.cardSuit;
  cardFaceValue=a.cardFaceValue;} // copy constructor
  ~Card(){}
  void setValue(int a,int b){cardSuit=Suit(a);
  cardFaceValue=FaceValue(b);}
  //set suit and face value of the card
  Card& operator = (const Card& a){cardSuit=a.cardSuit;
  cardFaceValue=a.cardFaceValue;
  return *this; };// assignment operator

  bool operator<(const Card& a) const{
  if( cardFaceValue < a.cardFaceValue )
  return true;
  else if( cardFaceValue == a.cardFaceValue )
  return cardSuit < a.cardSuit;
  else
  return false;
  };// compare operator

  bool CompareSuit(const Card& a) const{
  return cardSuit == a.cardSuit;
  }

  bool CompareFaceValue(const Card& a) const{
  return cardFaceValue == a.cardFaceValue;
  }

  bool FollowedBy(const Card& a) const{
  return a.cardFaceValue - cardFaceValue == 1;
  }
  inline int Point() const {
  if(cardFaceValue==ace)
  return 1;
  else
  return cardFaceValue + 2; // starts with 2
  }

  void Swap(Card& a){
  FaceValue fv = cardFaceValue;
  Suit s = cardSuit;
  cardFaceValue = a.cardFaceValue;
  cardSuit = a.cardSuit;
  a.cardFaceValue=fv;
  a.cardSuit=s;
  };

  private:

  Suit cardSuit;// cards suit
  FaceValue cardFaceValue; // cards face value
  };

  class Deck
  {
  friend ostream& operator <<(ostream&, Deck);
  public:
  Deck();
  ~Deck(){}
  void shuffle(); // create the list of cards in random order
  Card& deal(){assert(topCard<52);
  return cards[topCard++];}// deals the card at topCard and increments topCard
  private:
  Card cards [52];
  int topCard;// top of the deck
  };

  template <class Type, int sz> //General array template
  class Array {
  public:

  Array(){size=sz; ia=new Type[size];}
  ~Array() {delete [] ia;}
  Type& operator[](int index){ assert(index<size);
  return ia[index]; }

  protected:
  int size; //number of elements in array
  Type *ia; //pointer to first array element
  };

  template <int hsz, int pla>
  class Game
  {
  public:
  Game(){ players=pla;
  handSize=hsz;}; // number of players and number of cards in hand

  Game(const Game<hsz,pla>&){ players=pla;
  handSize=hsz;};
  void deal();
  void print(ostream&);
  PokerHandsRanking getRanking(int player);
  int getPoints(int player);

  private:
  Deck theDeck; //Deck of cards
  int players; //# of players
  int handSize; //Size of each hand
  Array<Array<Card,hsz>,pla> hands; //Arrays are nested
  };

  ostream& operator << (ostream& os, Card c ) //card output
  {
  int cs = int(c.cardSuit);
  int fv = int(c.cardFaceValue);
  assert( cs>=0 && cs<4 && fv>=0 && fv<13 );
  os<<suitName[cs]<<' '<<cardName[fv];
  return os;
  };

  Deck::Deck() //construct the deck
  {
  int k = 0;
  for (int i = 0; i < 4; ++i)
  for (int j = 0; j < 13;++j)
  {cards[k++].setValue(i,j);
  assert(i<4 && j<13);}
  topCard = 0;
  }

  void Deck::shuffle()
  { //shuffle cards randomly
  // randomize();
  for(int i = 0; i < 52;++i)
  {
  int k = rand() %52;
  assert(k<52);
  if (k!=i)
  {Card t = cards[i];cards[i] = cards[k]; cards[k] = t;}

  }
  }

  template <int hsz, int pla> //Deal cards
  void Game<hsz,pla>::deal() {

  theDeck.shuffle();
  for(int i=1;i<=players;++i) //loop thru players
  {
  for(int j=1;j<=handSize;++j) //loop thru hands
  {
  hands[i-1][j-1]=theDeck.deal();
  }
  // sort the player's cards
  for(int n=0;n<handSize-1;n++)
  for(int m=n+1;m < handSize;m++)
  {
  Card& ca = hands[i-1][n];
  Card& cb = hands[i-1][m];
  if( ca<cb)
  {
  ca.Swap(cb);
  }
  }
  } //deal a card
  }

  template <int hsz, int pla>
  void Game<hsz,pla>::print(ostream& os) {

  PokerHandsRanking winrank = uninitialized;
  int winpoints(0);
  int winner(-1);
  bool draw(false);
  for(int i=0;i<players;++i) {
  cout<<"\n玩家 "<<playerName[i]<<endl;
  for(int j=0;j<handSize;++j)
  cout<<hands[i][j]<<"\n";

  PokerHandsRanking ranking = getRanking(i);
  cout << rankingName[ranking] << endl;

  int points = getPoints(i);
  cout << "点数: " << points << endl;
  if( winrank < ranking )
  {
  winrank = ranking;
  winpoints = points;
  winner = i;
  }
  else if( winrank == ranking )
  {
  if(winpoints < points)
  {
  winpoints = points;
  winner = i;
  }
  }
  }
  if( winner >=0)
  cout << "赢家是:"<<playerName[winner]<<endl;
  else
  cout << "和局"<<endl;
  }

  template <int hsz, int pla>
  int Game<hsz,pla>::getPoints(int player) {
  int points(0);
  for(int j=0;j<handSize;++j)
  {
  Card& card =hands[player][j];
  points+= card.Point();
  }
  return points;
  }

  template <int hsz, int pla>
  PokerHandsRanking Game<hsz,pla>::getRanking(int player) {
  bool sameSuit(true);
  bool isStraight(true);
  bool pair(false);
  bool triple(false);
  for(int j=0;j<handSize;++j)
  {
  Card& cur =hands[player][j];

  if(j) {
  Card& prv =hands[player][j-1];
  sameSuit &= cur.CompareSuit( prv );
  isStraight &= cur.FollowedBy( prv );
  pair |= cur.CompareFaceValue( prv);
  triple &= cur.CompareFaceValue( prv);
  }
  }
  PokerHandsRanking ranking = zitch;
  // Rank 1: Straight flush
  if( sameSuit && isStraight )
  {
  ranking = straightFlush;
  } else if(isStraight){
  ranking = straight;
  } else if(triple) {
  ranking = threeOfAKind;
  } else if(pair){
  ranking = onePair;
  } else {
  ranking = zitch;
  }
  return ranking;
  }

  //main program for your tests.
  void main()
  {
  srand ( (unsigned int)time(NULL) );

  char opt;
  do{
  Game<3,2> g; //declare game
  g.deal(); //deal cards from deck
  g.print(cout); //display results
  cout<<"按任意键发牌;按0退出"<<endl;
  //cin >> opt;
  opt = getchar();
  system("cls");
  } while(opt!='0');
  }
本回答被提问者采纳
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
匿名用户
2020-02-05
展开全部
被使馆拒签后拒签后会收到拒签信,信函会写有拒签条例。 建议半年到一年以上再申请签证,期间可增加个人条件或出境记录。 澳签拒签原因一般会有以下几点 1.没有接到使馆的电调电话 2.接电话的时候回答不清晰或不准确,紧张或跟签证官沟通不畅 3.出入境记录不是很多 4.收入情况不足,例如工资卡流水余额太少、月收入太少 5.单身单独出行,与家庭联系不紧密 6.在澳/新有亲属,但没有告知或办理旅游签证 此外 职业过于敏感,或者之前有过不良出境记录(比如军人或者警察或者是高尖精行业精英 ) 对于此次行程不了解,无法向签证官详细说明目的地及计划安排。 若要申诉,请尽量避免以上原因。
追答
还不错呢这个游戏。。,DFH lorv/www.6700600.com?aLcXg
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
匿名用户
2019-08-05
展开全部
可以领取优惠券等优惠
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
收起 1条折叠回答
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式