Card Game Beta07

/*——————————————————————————card.h———————————————————————*/

#ifndef _CARD_H
#define _CARD_H
#define TypeValue short

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

using namespace std;

class Card
{
public:
 Card(); 
 Card& Swap(Card& a);//和另一张牌交换
 const bool operator > ( const Card& a );
 const bool operator < ( const Card& a );
 const bool operator == ( const Card& a );
 const bool operator != ( const Card& a );
 void operator = ( const Card& a );
 void SetCard();//随机设置一张牌
 void SetCard(TypeValue value,TypeValue suit);//设置为一张指定的牌
 void ShowCard();//输出牌面及花色
 TypeValue GetValue();//获得牌值
 const char GetSuit();//获得花色
 void ClearCard();//清空一张牌
 const bool IsClear();//判断一张牌是否为空
private:
 TypeValue Value;
 char Suit;
 char *Face;
};
Card& Card::Swap( Card& a )
{
 Card T = *this ;
   *this = a;
   a = T ;
 return *this ;
}
Card::Card()
{
 Value = 0 ;
 Suit = ' ' ;
 Face = "" ;
}
void Card::operator = ( const Card& a )
{
 Value = a.Value;
 Suit = a.Suit;
 Face = a.Face;
}
const bool Card::IsClear()
{
 if( Value == 0 && Suit == ' ' && Face == "")
  return true;
 return false;
}
void Card::SetCard( TypeValue value , TypeValue suit )
{
 switch(value)
 {
  case 13: Face = "A" , Value = 13 ; break ;
  case 1: Face = "2" , Value = 1 ; break ;
  case 2: Face = "3" , Value = 2 ; break ;
  case 3: Face = "4" , Value = 3 ; break ;
  case 4: Face = "5" , Value = 4 ; break ;
  case 5: Face = "6" , Value = 5 ; break ;
  case 6: Face = "7" , Value = 6 ; break ;
  case 7: Face = "8" , Value = 7 ; break ;
  case 8: Face = "9" , Value = 8 ; break ;
  case 9: Face = "10" , Value = 9 ; break ;
  case 10: Face = "J" , Value = 10 ; break ;
  case 11: Face = "Q" , Value = 11 ; break ;
  case 12: Face = "K" , Value = 12 ; break ;
  default:
   Value = 0 , Face = "" ; break ;
 }
 switch(suit)
 {
  case 0:Suit=3;break;
  case 1:Suit=4;break;
  case 2:Suit=5;break;
  case 3:Suit=6;break;
  default:
   Suit=' ';break;
 }
}
const bool Card::operator > (const Card &a)
{
 if(this->Value>a.Value) return true;
 return false;
}
const bool Card::operator < (const Card &a)
{
 if(this->Value<a.Value) return true;
 return false;
}
const bool Card::operator == (const Card &a)
{
 if(this->Value==a.Value&&this->Suit==a.Suit) return true;
 return false;
}
const bool Card::operator !=(const Card &a)
{
 if(!(this->Value==a.Value&&this->Suit==a.Suit)) return true;
 return false;
}
void Card::ShowCard()
{
 cout<<Face<<Suit;
}
const char Card::GetSuit()
{
 return this->Suit ;
}
TypeValue Card::GetValue()
{
 return this->Value ;
}
void Card::ClearCard()
{
 Suit =' ';
 Value=0;
 Face ="";
}
void Card::SetCard()
{//随机生成一张牌
 srand((unsigned)time(NULL));
 switch(rand()%13)
 {
  case 0:Face="A",Value=13;break;
  case 1:Face="2",Value=1;break;
  case 2:Face="3",Value=2;break;
  case 3:Face="4",Value=3;break;
  case 4:Face="5",Value=4;break;
  case 5:Face="6",Value=5;break;
  case 6:Face="7",Value=6;break;
  case 7:Face="8",Value=7;break;
  case 8:Face="9",Value=8;break;
  case 9:Face="10",Value=9;break;
  case 10:Face="J",Value=10;break;
  case 11:Face="Q",Value=11;break;
  case 12:Face="K",Value=12;break;
  default:break;
 }
 switch(rand()%4)
 {
  case 0:Suit=3;break;
  case 1:Suit=4;break;
  case 2:Suit=5;break;
  case 3:Suit=6;break;
  default:break;
 }
}
#endif

/*———————————————————cardbox.h——————————————————————————*/

#ifndef _CARDBOX_H
#define _CARDBOX_H

#include "Card.h"

class CardBox
{
private:
 Card Box[52];
protected:
 TypeValue CardAmount;
 Card Send;
public:
 CardBox();
 TypeValue& GetCardAmount();
 Card& SendCard();
 Card& PickCard(const TypeValue& n);
 void Display();
};
CardBox::CardBox()
{//初始化盒子中的牌
 CardAmount=52;
 for(TypeValue i=0;i<CardAmount;++i)
  Box[i].SetCard(i%13+1,i/13);
}
Card& CardBox::PickCard(const TypeValue& n)
{//选择盒子中指定位置的牌
 if(n<0||n>GetCardAmount()-1)
 {
  cout<<"非法下标!";
  Send.ClearCard();
  return Send;
 }
 else
 {
  Send=Box[n];
  for(TypeValue i=n;i<CardAmount-1;++i)
   Box[i]=Box[i+1];
  Box[i].ClearCard();//将盒子中最后一张牌清空
  --CardAmount;//盒中牌数减1
 }
 return Send;
}
Card& CardBox::SendCard()
{//选择盒子中随机位置的牌
 srand( ( unsigned ) time ( NULL ) );
 const TypeValue n = rand() % GetCardAmount() ;
 if( n < 0 || n > CardAmount - 1 )
 {
  cout<<"非法下标!";
  Send.ClearCard();
  return Send;
 }
 else
 {
  Send=Box[n];
  for(TypeValue i=n;i<CardAmount-1;++i)
   Box[i]=Box[i+1];
  Box[i].ClearCard();//将盒子中最后一张牌清空
  --CardAmount;//盒中牌数减1
 }
 return Send;
}
TypeValue& CardBox::GetCardAmount()
{//返回盒子中剩余的牌数
 return CardAmount;
}
void CardBox::Display()
{//显示盒子中现有的牌
 if(!CardAmount)
  cout<<"牌已经发完!"<<endl;
 else
  for(TypeValue i=0;i<CardAmount;++i )
  {
   Box[i].ShowCard();
   cout<<"/t";
  }
}

#endif

/*—————————————————————cardplayer.h—————————————————————————*/

#ifndef _CARDPLAYER_H
#define _CARDPLAYER_H

#include "Card.h"
#include "CardBox.h"

class CardPlayer
{
private:
 char *name;//例如选手名字为LuciferDragon
 Card *card;
protected:
 Card *temporary;//临时牌
 TypeValue CardAmount ;//玩家拥有的牌数
 TypeValue NameSize;
 int score;
 int money;
 bool drop;
public:
 CardPlayer();
 void DisplayPlayerName();
 void SetPlayerName();
 void Sort();
 const bool IsSort();
 const bool IsEmpty();
 const bool SendCard(CardBox& Box);
 const Card& GetCard(const TypeValue &n);
 void DisplayCard();
 const TypeValue& GetCardAmount();
};
void CardPlayer::DisplayPlayerName()
{
 cout<<name;
}
void CardPlayer::SetPlayerName()
{//最多读取size-1个字符或size/2个汉字,遇到回车则终止输入
 char *Str=new char[NameSize];
 int count=0;
 cout<<"请输入选手姓名:";
 cout.flush();//清空缓冲区
 cin.ignore(1);//忽略一个字符,否则会将上一行的回车符号读进去
 cin.get(Str,NameSize,'/n');
 name=Str; 
}
void CardPlayer::Sort()
{//对没有排序好的牌进行排序
 if( IsSort() );//排序好则直接返回*this
 else
 {//否则进行冒泡排序
  for( TypeValue i = CardAmount-1 ; i > -1 ; --i )
   for( TypeValue j = 0 ; j < i ; ++j )
    if( card[j] > card[j+1] )
     card[j].Swap( card[j+1] ) ;
 }
}
const bool CardPlayer::IsSort()
{//是否已排序好,升序排序
 return ( card[2].GetValue() >= card[1].GetValue() ) &
     ( card[1].GetValue() >= card[0].GetValue() ) ;
}
const bool CardPlayer::IsEmpty()
{//判断选手是否已经发牌
 //return card[0].IsClear();//此条语句已经足够了
 bool flag=true;
 for(TypeValue i = 0 ; i < CardAmount ; ++i )
  flag &= card[i].IsClear() ;
 return flag;
}
const TypeValue& CardPlayer::GetCardAmount()
{
 return CardAmount;
}
CardPlayer::CardPlayer()
{
 NameSize = 15;//定义用户名长度
 name = new char[NameSize];
 name="计算机";//默认选手姓名
  CardAmount=7;//初始化牌数
 card=new Card[CardAmount];//初始化玩家手中的牌
 score=0;//初始化游戏积分
 money=1000;//初始化玩家金钱
 drop=false;//开始时默认没有放弃比赛
}
const bool CardPlayer::SendCard(CardBox& Box)
{//判断是否发了一张牌
 if( Box.GetCardAmount() < CardAmount)
 {//若盒中的牌少于玩家应发的牌数返回false,
  cout<<"Out Of Bounds!";
  return false;
 }
 else
 {//否则从盒子Box中取CardAmount张牌发给选手返回true
  for(TypeValue i=0;i<CardAmount;++i)
   card[i] = Box.SendCard();
  return true;
 }
}
const Card& CardPlayer::GetCard(const TypeValue &n)
{
 if(n<0||n>CardAmount-1)
 {
  cout<<"Out Of Bounds!";
  temporary=new Card;
  return *temporary;
 }
 return card[n];
}
void CardPlayer::DisplayCard()
{
 for(TypeValue i=0;i<CardAmount;++i)
 {
  card[i].ShowCard();
  cout<<"/t";
 }
}

#endif

/*——————————————————————————main.cpp————————————————————*/

#include "Card.h"
#include "CardBox.h"
#include "CardPlayer.h"

using namespace std;

int main()

 CardBox box;
 CardPlayer a;
 box.Display();
 int n;//玩家数量
 int pos;//你的位置
 cout<<"请输入玩家数量:";
 cin>>n;
 while( n<0||n > box.GetCardAmount()/a.GetCardAmount())
 {
  cout<<"PLAYER AMOUNT ERROR!"<<endl;
  cout<<"请输入玩家数量:";
  cin>>n;
 }
 cout<<"请选择你的位置:";
 cin>>pos;
 while( pos<1||pos>n )
 {
  cout<<"PLAYER POSITION ERROR!"<<endl;
  cout<<"请选择你的位置:";
  cin>>pos;
 }

 
 //初始化玩家数据
 CardPlayer *player=new CardPlayer[n];
  
 player[pos-1].SetPlayerName();


 for(int i=0;i<n;++i)
 {
  cout<<"玩家"<<i+1<<'/t';
  if(i==pos-1)
  { 
   player[i].DisplayPlayerName();
   cout<<endl;
   player[i].SendCard(box);
   player[i].Sort();
   player[i].DisplayCard();
   cout<<endl;
  }
  else
  {
   player[i].DisplayPlayerName();
   cout<<i+1<<endl;
   player[i].SendCard(box);
   player[i].Sort();
   player[i].DisplayCard();
   cout<<endl;
  }
 }

 /*CardPlayer a;
 a.SetPlayerName();
 a.DisplayPlayerName();
 cout<<endl;*/
 //Player[pos-1].SetPlayerName();
 //Player[pos-1].DisplayPlayerName();

 return 0;
}

 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值