c语言自动随机发牌给四个人(没有大小王)

在这里插入图片描述

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

#define PokeTotal 52                //扑克的总数
#define UserTotal 4                //用户数量
#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[13] = { 'A','2','3','4','5','6','7','8','9','0','J','Q','k' };
	char AllPokeFlower[4] = {3,4,5,6};

	//第一张牌
	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;
}

已标记关键词 清除标记
相关推荐
©️2020 CSDN 皮肤主题: 编程工作室 设计师:CSDN官方博客 返回首页
实付 19.90元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、C币套餐、付费专栏及课程。

余额充值