CardBox 类

来源:互联网 发布:网络摄像机烧刻软件 编辑:程序博客网 时间:2024/04/29 11:53

#ifndef _CARDBOX_H
#define _CARDBOX_H

#include "Card.h"

class CardBox
{
private:
 Card *Box ;//盒中的牌
protected:
 mutable TypeValue CardAmount ;//盒中牌数
public:
 CardBox() ;//盒子的构造函数
 
 bool IsEmpty() const ;//判断盒中是否还有牌
 TypeValue& GetCardAmount() const ;//获得当前盒中牌数 
 CardBox& SendCardTo( Card& a ) ;//随机发一张牌赋值给牌a
 CardBox& PickCard( const TypeValue& n , Card& a ) ;//取出指定位置的牌赋值给牌a

 void Display() ;//显示盒子中现有的牌
 ~CardBox() ;//析构盒子
};

CardBox::CardBox()
{//初始化盒子中的牌
 CardAmount =   52 ;//初始化盒中牌数
 Box = new Card[ CardAmount ] ;
 for( TypeValue  i = 0 ; i < CardAmount ; ++i )
  Box[i].SetCard( i%13 + 1 , i/13 ) ;
}

bool CardBox::IsEmpty() const
{ return CardAmount == 0 ; }

TypeValue& CardBox::GetCardAmount() const
{ return CardAmount ;     }

CardBox& CardBox::SendCardTo( Card& a )
{
 srand( ( unsigned ) time ( NULL ) );
 const TypeValue n = rand() % GetCardAmount() ;
 a = Box[ n ] ;
 for( TypeValue i = n ; i < CardAmount - 1 ; ++i )
 Box[ i ] = Box[ i + 1 ] ;
 Box[i].ClearCard() ;//将盒子中最后一张牌清空
 --CardAmount ;//盒中牌数减1
 return *this ;
}

CardBox& CardBox::PickCard( const TypeValue& n , Card& a )
{
 if( n < 0 || n > GetCardAmount() - 1 )
 { cout << "非法下标!/a/n" ;  }
 else
 {
  a = Box[ n ] ;
  for( TypeValue i = n ; i< CardAmount - 1 ; ++i )
   Box[ i ] = Box[ i + 1 ] ;
  Box[ i ].ClearCard() ;//将盒子中最后一张牌清空
  --CardAmount ;//盒中牌数减1
 }
 return *this ;
}

void CardBox::Display()
{
 if( !CardAmount )
  cout<< "牌已经发完!/a/n" ;
 else
  for( TypeValue i = 0 ; i < CardAmount ; ++i  )
  {
   Box[i].ShowCard();
   cout << "/t" ;
  }
}

CardBox::~CardBox()
{ delete []Box ; }

#endif