C语言实现自动发牌

C语言实现自动发牌(每步基本有注释)

1.初始化54张扑克
2.随机洗牌
3.依次分发给三个人
4.显示每个人手里的牌

注:
1.主要利用随机数、结构体和数组来实现
2.常量均以宏替换
3.花色用3,4,5,6 ,1的ASCII码表示

程序运行效果:C语言实现自动发牌_第1张图片
代码部分 :

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

#define PokeTotal 54                //扑克的总数
#define UserTotal 3                 //用户数量
#define UserPokeTotal 18            //用户可持有的最大牌数
#define PokeNumberTotal 13          //扑克的全部牌号
#define PokeFlowerTotal 4           //扑克的全部花色
#define ShuffleCardsTotal 5000      //洗牌次数
#define DisplayRowTotal 6           //每行显示牌的个数
#define PokeFlowerType {3,4,5,6}    //花色种类
#define PokeKingFlower 1            //大王和小王的花色
#define PokeBigKingNumber 'B'       //大王的牌号
#define PokeSmallKingNumber 'S'     //小王的牌号

//牌号种类
#define PokeNmberType  { 'A','2','3','4','5','6','7','8','9','0','J','Q','k' }

///////////////////////////////////////////
//定义一张牌
struct aPoke
{
	char PokeNumber;        //扑克的牌号      
	char PokeFlower;        //扑克的花色
}AllPoke[PokeTotal],User[UserTotal][UserPokeTotal];  

///////////////////////////////////////////
//初始化牌组
void InitalizaPoke()
{
	int i, j, contentNumber;
	char AllPokeNumber[PokeNumberTotal] = PokeNmberType;
	char AllPokeFlower[PokeFlowerTotal] = PokeFloweType;

	//第一张牌
	contentNumber = 0;

	//按顺序储存每张牌号
	for (i = 0; i < PokeNumberTotal; i++)
	{

		//储存四张牌号相同,花色不同的牌
		for (j = 0; j < PokeFlowerTotal; j++)
		{
			//先储存牌号,再储存花色
			AllPoke[contentNumber].PokeNumber = AllPokeNumber[i];
			AllPoke[contentNumber].PokeFlower = AllPokeFlower[j];

			//储存下一张牌
			++contentNumber;
		}

	}

	//设置大王和小王
	AllPoke[52].PokeNumber = PokeSmallKingNumber;
	AllPoke[52].PokeFlower = PokeKingFlower;
	AllPoke[53].PokeNumber = PokeBigKingNumber;
	AllPoke[53].PokeFlower = PokeKingFlower;

}

///////////////////////////////////////////
//洗牌
void ShuffleCards()
{
	int i,RandNumber1, RandNumber2;
	char pNumber, pFlower;

	srand((unsigned)time(NULL));

	//洗牌
	for (i = 0; i < ShuffleCardsTotal; i++)
	{
		//随机抽两张牌
		RandNumber1 = rand() % PokeTotal;
		RandNumber2 = rand() % PokeTotal;

		//交换位置
		pFlower=AllPoke[RandNumber1].PokeFlower;
		pNumber = AllPoke[RandNumber1].PokeNumber;
		AllPoke[RandNumber1].PokeFlower = AllPoke[RandNumber2].PokeFlower;
		AllPoke[RandNumber1].PokeNumber = AllPoke[RandNumber2].PokeNumber;
		AllPoke[RandNumber2].PokeFlower = pFlower;
		AllPoke[RandNumber2].PokeNumber = pNumber;
	}
}

///////////////////////////////////////////
//发牌
void DealCards()
{
	int i,j,ContentPoke=0;

	//依次发牌
	for (i = 0; i < UserPokeTotal; i++)
	{
		for (j = 0; j < UserTotal; j++)
		{
			User[j][i].PokeFlower = AllPoke[ContentPoke].PokeFlower;
			User[j][i].PokeNumber = AllPoke[ContentPoke].PokeNumber;
			++ContentPoke;
		}
	}

}


///////////////////////////////////////////
//显示每个人的牌
void LookCards()
{
	int i, j, ContentPoke = 0,cNumber=0;

	//依次显示每个人的牌
	for (i = 0; i < UserTotal; i++)
	{
		printf("第%d个人的牌\n",i+1);

		for (j = 0; j < UserPokeTotal; j++)
		{
       
			printf("%c",User[i][j].PokeFlower);
			
            //替换牌号为0的牌,正确显示牌号为10的牌
			if (User[i][j].PokeNumber == '0')
			{
				printf("10 ");
			}
			else
			{
				printf("%c  ", User[i][j].PokeNumber);
			}
			++ContentPoke;
			++cNumber;
			
            //每行显示的牌数
			if (cNumber % DisplayRowTotal == 0)
			{
				printf("\n");
			}
		}

		printf("\n\n");
	}
}

int main()
{
	int Judge;

	printf("1为开始\n0为结束\n");
	scanf("%d", &Judge);

	while (Judge)
	{
		//初始化牌组
		InitalizaPoke();

		//洗牌
		ShuffleCards();

		//发牌
		DealCards();

		//显示每个人的牌
		LookCards();

		printf("1为开始\n0为结束\n");
		scanf("%d", &Judge);

	}
	return 0;
}

你可能感兴趣的:(C语言,扑克,发牌)