只能给一个人发牌?
程序代码:
//Poker.h
#ifndef __POKER_H__
#define __POKER_H__
typedef struct _poker
{
char m;
char f;
char i;
}Poker,*pPoker,&HPoker;
//显示使用
static Poker NormalPoker[] =
{
{'A','@', 0x00},
{'A','*', 0x01},
{'A','&', 0x02},
{'A','#', 0x03},
{'2','@', 0x04},
{'2','*', 0x05},
{'2','&', 0x06},
{'2','#', 0x07},
{'3','@', 0x08},
{'3','*', 0x09},
{'3','&', 0x0a},
{'3','#', 0x0b},
{'4','@', 0x0c},
{'4','*', 0x0d},
{'4','&', 0x0e},
{'4','#', 0x0f},
{'5','@', 0x10},
{'5','*', 0x11},
{'5','&', 0x12},
{'5','#', 0x13},
{'6','@', 0x14},
{'6','*', 0x15},
{'6','&', 0x16},
{'6','#', 0x17},
{'7','@', 0x18},
{'7','*', 0x19},
{'7','&', 0x1a},
{'7','#', 0x1b},
{'8','@', 0x1c},
{'8','*', 0x1d},
{'8','&', 0x1e},
{'8','#', 0x1f},
{'9','@', 0x20},
{'9','*', 0x21},
{'9','&', 0x22},
{'9','#', 0x23},
{'0','@', 0x24},
{'0','*', 0x25},
{'0','&', 0x26},
{'0','#', 0x27},
{'J','@', 0x28},
{'J','*', 0x29},
{'J','&', 0x2a},
{'J','#', 0x2b},
{'Q','@', 0x2c},
{'Q','*', 0x2d},
{'Q','&', 0x2e},
{'Q','#', 0x2f},
{'K','@', 0x30},
{'K','*', 0x31},
{'K','&', 0x32},
{'K','#', 0x33},
{'G','-', 0x34},
{'G','+', 0x35}
};
#define AllPokerNum 54
class APoker
{
private:
unsigned char arPoker[54];
unsigned char currentlen;
public:
APoker();
//洗牌
void Shuffle();
//发牌
unsigned char GetOnePoker();
private:
int Rand();
};
#endif
程序代码:
//poker.cpp
#include "Poker.h"
#include <stdlib.h>
#include <time.h>
APoker::APoker()
{
srand(time(NULL));
Shuffle();
}
void APoker::Shuffle()
{
currentlen = AllPokerNum;
for(unsigned char i= 0;i<AllPokerNum;i++)
{
arPoker[i] = i;
}
}
unsigned char APoker::GetOnePoker()
{
int r = Rand();
unsigned char v = 0;
if(r<0)return 100;
v = arPoker[r];
arPoker[r] = arPoker[--currentlen];
return v;
}
int APoker::Rand()
{
if(currentlen>0)
{
int r = rand();
return r%currentlen;
}
return -1;
}
程序代码:
//main.cpp
#include <stdlib.h>
#include <stdio.h>
#include "Poker.h"
int main()
{
APoker ap;
for(int i=0;i<54;i++)
{
int a = ap.GetOnePoker();
if(a<54)
{
Poker p = NormalPoker[a];
printf("-->value: [ %c %c ] ; index: %02d\n",p.m,p.f,p.i);
}
}
system("pause");
return 0;
}
程序代码:
编译
g++ main.cpp Poker.cpp Poker.h
运行 a.exe
输出:
-->value: [ 8 @ ] ; index: 28
-->value: [ A * ] ; index: 01
-->value: [ 0 # ] ; index: 39
-->value: [ K * ] ; index: 49
-->value: [ 7 @ ] ; index: 24
-->value: [ 4 @ ] ; index: 12
-->value: [ 6 @ ] ; index: 20
-->value: [ 9 & ] ; index: 34
-->value: [ J @ ] ; index: 40
-->value: [ 2 * ] ; index: 05
-->value: [ 0 * ] ; index: 37
-->value: [ K # ] ; index: 51
-->value: [ J & ] ; index: 42
-->value: [ 8 # ] ; index: 31
-->value: [ 7 & ] ; index: 26
-->value: [ 2 # ] ; index: 07
-->value: [ Q * ] ; index: 45
-->value: [ 4 # ] ; index: 15
-->value: [ 2 & ] ; index: 06
-->value: [ 4 & ] ; index: 14
-->value: [ J # ] ; index: 43
-->value: [ 9 * ] ; index: 33
-->value: [ 7 * ] ; index: 25
-->value: [ 3 * ] ; index: 09
-->value: [ 4 * ] ; index: 13
-->value: [ 5 * ] ; index: 17
-->value: [ 5 & ] ; index: 18
-->value: [ 5 # ] ; index: 19
-->value: [ 3 & ] ; index: 10
-->value: [ 0 @ ] ; index: 36
-->value: [ 6 * ] ; index: 21
-->value: [ 6 & ] ; index: 22
-->value: [ 8 * ] ; index: 29
-->value: [ 9 # ] ; index: 35
-->value: [ 2 @ ] ; index: 04
-->value: [ 9 @ ] ; index: 32
-->value: [ G - ] ; index: 52
-->value: [ 6 # ] ; index: 23
-->value: [ 7 # ] ; index: 27
-->value: [ 8 & ] ; index: 30
-->value: [ K @ ] ; index: 48
-->value: [ K & ] ; index: 50
-->value: [ J * ] ; index: 41
-->value: [ Q & ] ; index: 46
-->value: [ 0 & ] ; index: 38
-->value: [ Q @ ] ; index: 44
-->value: [ A # ] ; index: 03
-->value: [ 3 # ] ; index: 11
-->value: [ Q # ] ; index: 47
-->value: [ A @ ] ; index: 00
-->value: [ G + ] ; index: 53
-->value: [ 5 @ ] ; index: 16
-->value: [ A & ] ; index: 02
-->value: [ 3 @ ] ; index: 08