c语言实现扑克牌斗地主自动发牌功能

大家好,今天给大家聊一聊用c语言实现斗地主自动发牌的程序

1、问题描述

一副完整的扑克牌54张,斗地主时将牌发给三个用户,留三张底牌,请设计一个程序完成自动发牌的工作。(要求写清花色)

2、问题分析

将54张牌用二维数组存储起来,51张牌(留三张)发给三个人,用随机函数生成要发的牌的编号,再创建一个数组判断是否重复,保证每一张牌都能发到。大小王这里用W和w代替,使用三维数组表示玩家手中的牌。

3、代码实现

#define _CRT_SECURE_NO_WARNINGS 1
#define ROW 5
#define COLUMN 13
#define PLAYER 3
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
//函数声明
void sendCard(char(*card)[COLUMN], char player[PLAYER][17][138], int repeat[ROW][COLUMN]);
void getcolor(int color, int* L_pointNum, char* cardColor);
void show(char player[PLAYER][17][138]);
void Bottomshow(char(*card)[COLUMN], int repeat[ROW][COLUMN]);

int main()
{
	//存储54张牌,二维数组存储5行13列
	char cards[ROW][COLUMN] = { {'A','2','3','4','5','6','7','8','9','T','J','Q','K'},
	 {'A','2','3','4','5','6','7','8','9','T','J','Q','K'} ,
	 {'A','2','3','4','5','6','7','8','9','T','J','Q','K'} ,
	 {'A','2','3','4','5','6','7','8','9','T','J','Q','K'} ,
	 {'W','w'} };
	//三个用户,每人17张牌, 任意数
	char player[PLAYER][17][138] = { 0 };
	//创建一个数组判断是否重复
	int repeat[ROW][COLUMN] = { 0 };
	//发牌
	sendCard(cards, player, repeat);//传二维数组的数组名=首元素的地址
	//打印
	show(player);
	//打印三张底牌
	Bottomshow(cards, repeat);
	return 0;
}

//发牌,传cards(数组),类型(char(*card)[COLUMN]=cards)
void sendCard(char (*card)[COLUMN], char player[PLAYER][17][138],int repeat[ROW][COLUMN])//数组指针
{
	//1、建立随机数
	//随机播种
	srand((unsigned int)time(NULL));
	for (int i = 0; i < 51;)//留三张底牌
	{
		//随机发牌
		int color = rand() % 5;//随机花色
		int pointNum = rand() % 13;//随机点数
		//使用字符串函数
		char cardColor[128] = { 0 };//保存花色
		getcolor(color, &pointNum, cardColor);//获取花色
		if (repeat[color][pointNum] == 0)//判断并删除重复生成
		{
			//生成牌,使用字符串函数
			char num[2] = { *(*(card + color) + pointNum) ,'\0' };//数组名偏移花色解引用偏移点数解引用
			//将颜色和点数拼接到
			strcat(cardColor, num);
			//将牌发给用户
			strcpy(player[i / 17][i % 17], cardColor);//第几个用户,发的第几张牌
			repeat[color][pointNum] = 1;//已经生成过了
			i++;
		}
	}
}
//获取花色
void getcolor(int color,int* L_pointNum,char* cardColor)//需要传地址,改变外部函数中的值
{
	switch (color)
	{
	case 0:
		strcpy(cardColor, "梅花");
		break;
	case 1:
		strcpy(cardColor, "红桃");
		break;
	case 2:
		strcpy(cardColor, "黑桃");
		break;
	case 3:
		strcpy(cardColor, "方块");
		break;
	case 4:
		*L_pointNum = *L_pointNum % 2;//解决随机拼接出来有空的问题
		strcpy(cardColor, "");//大小王
		break;

	}
}
//打印
void show(char player[PLAYER][17][138])
{
	for (int i = 0; i < PLAYER; i++)
	{
		printf("玩家%d:\n", i + 1);
		for (int j = 0; j < 17; j++)
		{
			printf("%6s", *(player + i) + j);
		}
		printf("\n");
	}
}
//打印底牌
void Bottomshow(char(*card)[COLUMN], int repeat[ROW][COLUMN])
{
	printf("打印底牌:\n");
	int count = 1;
	for (int i = 0; i < ROW; i++)
	{
		for (int j = 0; j < COLUMN; j++)
		{
			if (count > 3)
			{
				return;
			}
			if (*(*(repeat+i)+j) == 0)
			{
				char cards[128] = { 0 };
				int pointNum = j;
				getcolor(i, &pointNum, cards);
				//生成牌,使用字符串函数
				char num[2] = { *(*(card + i) + pointNum) ,'\0' };//数组名偏移花色解引用偏移点数解引用
				//将颜色和点数拼接到
				strcat(cards, num);
				printf("%6s", cards);
				count++;
			}
		}
	}
}

4、运行结果 

总结:仔细钻研,有些问题还是能迎刃而解的

(大家有什么问题可以指出来)

  • 8
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值