洗牌的一个C++类!

类别:编程语言 点击:0 评论:0 推荐:

这是一个洗牌的类!
提供了几个简单的借口!
bool __fastcall Refresh();洗牌
int __fastcall Getbyindex(int _index);根据顺序参数来取牌;
GRefresh_Card(int _CardNum=54);初始化,_CardNum是牌的张数,默认的事54;
void free();用来释放一些必要的东西;
AnsiString Getusefully(int _index);真实取牌,带色!Code如下:

//---------------------------------------------------------------------------


#pragma hdrstop

#include "RefC.h"
#include <vector>
#include <algorithm>
#include <stdlib.h>
#include <Classes.hpp>
//---------------------------------------------------------------------------

#pragma package(smart_init)
GRefresh_Card::~GRefresh_Card()
{
        //Îö¹¹
        free();

}

bool __fastcall GRefresh_Card::Refresh()
{
        //Ï´ÅÆ
        bool Card_Result=false;
        srand((unsigned)time(NULL));            //+ Ôö¼ÓÕâÒ»ÐУ¬ÒÔʱ¼ä×÷ΪËæ»úÊýÖÖ×Ó
        std::random_shuffle(Card->begin(),Card->end());
        return Card_Result;
}

int __fastcall GRefresh_Card::Getbyindex(int _index)
{
        //ÄÇÅÆ£»
        return (_index>=0 && _index<Card_Num)? Card->operator [](_index):0;

}

GRefresh_Card::GRefresh_Card(int _CardNum)
{
        //³õʼ»¯£»
        Card_Num=_CardNum;
        Card=new std::vector<int>;
        for(int i=0;i<54;i++)
        Card->push_back(i);
        Card_color=new TStringList();
        Card_color->Add("·½¿é");
        Card_color->Add("÷»¨");
        Card_color->Add("ºìÌÒ");
        Card_color->Add("ºÚÌÒ");
}

void GRefresh_Card::free()
{
        delete  Card;
        Card=NULL;
        delete Card_color;
        Card_color=NULL;
}

AnsiString GRefresh_Card::Getusefully(int _index)
{
        int _color=Getbyindex(_index)%13;
        int _value=Getbyindex(_index)/13;
        if(_value>3)
        {
                AnsiString result;
                _color? result="´óÍõ":result="СÍõ";
                return result;
        }
        else
        {
                AnsiString result=Card_color->operator [](_value)+IntToStr(_color+1);
                return result;
        }
}

//---------------------------------------------------------------------------

#ifndef RefCH
#define RefCH
#include <vector>
#include <Classes.hpp>
//---------------------------------------------------------------------------
class GRefresh_Card {
protected:
        int Card_Num;
        std::vector<int> * Card;
        TStringList * Card_color;
public:
        ~GRefresh_Card();
        bool __fastcall Refresh();
        int __fastcall Getbyindex(int _index);
        GRefresh_Card(int _CardNum=54);
        void free();
        AnsiString Getusefully(int _index);};
#endif

本文地址:http://com.8s8s.com/it/it29102.htm