#include <stdio.h>
#include <stdlib.h>
int Rand(int min, int max)
{
int rnd;
int range = abs(max - min);
rnd = (rand() % range) + min;
return(rnd);
}
void InitCard(char *card, int num)
{
int i;
for( i = 0; i < num; i ++ ) {
card[i] = i % 52;
}
}
void ShuffleCard(char *card, int num)
{
int i, rnd;
char tmp;
for( i = 0; i < num; i ++ ) {
rnd = Rand(0, num);
tmp = card[i];
card[i] = card[rnd];
card[rnd] = tmp;
}
}
void ShowCard(char *card, int num)
{
int i, rank, suit;
char *szSuit[] = { "黑桃", "紅桃", "方塊", "梅花" };
for( i = 0; i < num; i ++ ) {
rank = card[i] % 13;
suit = card[i] / 13;
printf("%s%2d, ", szSuit[suit], rank+1);
}
printf("\n");
}
int main(void)
{
int TotalCards = 52*4; // 使用4副牌
int Players = 4; // 4個玩家
int Cards = 5; // 每人5張牌
char card[TotalCards];
int i, j, ci;
char player[Players][Cards];
InitCard(card, TotalCards); // 初始牌值
ShuffleCard(card, TotalCards); // 洗牌
// 發牌, ci 紀錄目前發到第幾張牌
ci = 0;
for( j = 0; j < Cards; j ++ ) {
for( i = 0; i < Players; i ++ ) {
player[i][j] = card[ci++];
}
}
// 顯示各家手上的牌
for( i = 0; i < Players; i ++ ) {
ShowCard(player[i], Cards);
}
system("PAUSE");
return 0;
}