C语言中用结构体设计一个可以显示花色和编号的扑克牌,并实现对这副扑克牌洗牌、整牌和发牌三个功能

C语言中用结构体设计一个可以显示花色和编号的扑克牌,并实现对这副扑克牌洗牌、整牌和发牌三个功能,第1张

#include <iostream>

#include <ctime>

using namespace std

//全局变量,一副牌

//*****************************************************************************************

int g_cards[54] = {

0, 1, 2, 3, // 3

10, 11, 12, 13, // 4

20, 21, 22, 23, // 5

30, 31, 32, 33, // 6

40, 41, 42, 43, // 7

50, 51, 52, 53, // 8

60, 61, 62, 63, // 9

70, 71, 72, 73, // 10

80, 81, 82, 83, // J

90, 91, 92, 93, // Q

100, 101, 102, 103, // K

110, 111, 112, 113, // A

120, 121, 122, 123, // 2

130, //小王

140 //大王

}

void xipai()//洗牌全局函数

{

srand( time(NULL) ) //设置种子

for(int i=0i<54 i++)

{

int position = rand()%54

int temp = g_cards[i]//与牌的下标进行互换

g_cards[i] = g_cards[position]

g_cards[position] = temp

}

}

void showCards() //显示全局函数

{

for(int i=0i<54 i++)

{

if( g_cards[i] != 130 &&g_cards[i] !=140 )

{

switch(g_cards[i]%10)

{

case 0:

cout<<"黑桃:"

break

case 1:

cout<<"红桃:"

break

case 2:

cout<<"梅花:"

break

case 3:

cout<<"方片:"

break

}

}

switch(g_cards[i]/10)

{

case 0:

cout<<"3:"<<endl

break

case 1:

cout<<"4:"<<endl

break

case 2:

cout<<"5:"<<endl

break

case 3:

cout<<"6:"<<endl

break

case 4:

cout<<"7:"<<endl

break

case 5:

cout<<"8:"<<endl

break

case 6:

cout<<"9:"<<endl

break

case 7:

cout<<"10:"<<endl

break

case 8:

cout<<"J:"<<endl

break

case 9:

cout<<"Q:"<<endl

break

case 10:

cout<<"K:"<<endl

break

case 11:

cout<<"A:"<<endl

break

case 12:

cout<<"2:"<<endl

break

case 13:

cout<<"小王:"<<endl

break

case 14:

cout<<"大王:"<<endl

break

}

}

}

//******************************************************************************************

struct CARDMESSAGE

{

//牌型, 牌的信息

int cardType //牌型 0 单牌 1 双牌 2 三带一/二 3 四带一/二 4 顺子 5 连对 6 飞机 7 炸d

int size //牌的张数

int value //牌值

int num //连对或者飞机的顺数, -1为无限制

bool isLord //是否是地主出的牌

void show()

{

switch(cardType)

{

case 0:

cout<<"单张:"<<endl

break

case 1:

cout<<"两张:"<<endl

break

case 2:

{

if(size==4)

cout<<"三带一:"<<endl

else

cout<<"三带二:"<<endl

}

case 3:

{

if(size == 5)

cout<<"四带一"<<endl

else

cout<<"四带二"<<endl

}

case 4:

{

cout<<"顺子"<<endl

}

case 5:

{

cout<<"连对"<<endl

}

case 6:

{

cout<<"飞机"<<endl

}

case 7:

{

cout<<"炸d"<<endl

}

}

break

}

switch(value%10)

{

case 0:

cout<<"黑桃:"<<endl

break

case 1:

cout<<"红桃:"<<endl

break

case 2:

cout<<"梅花:"<<endl

break

case 3:

cout<<"方片:"<<endl

break

}

switch(value/10)

{

case 0:

cout<<"3:"<<endl

break

case 1:

cout<<"4:"<<endl

break

case 2:

cout<<"5:"<<endl

break

case 3:

cout<<"6:"<<endl

break

case 4:

cout<<"7:"<<endl

break

case 5:

cout<<"8:"<<endl

break

case 6:

cout<<"9:"<<endl

break

case 7:

cout<<"10:"<<endl

break

case 8:

cout<<"J:"<<endl

break

case 9:

cout<<"Q:"<<endl

break

case 10:

cout<<"K:"<<endl

break

case 11:

cout<<"A:"<<endl

break

case 12:

cout<<"2:"<<endl

break

case 13:

cout<<"小王:"<<endl

break

case 14:

cout<<"大王:"<<endl

break

}

}

}

//******************************************************************************************

class Ccard

{

public:

int cardArray[20] //保存所有牌的信息

int cardNum //有效牌的张数

public:

Ccard()

~Ccard()

void sortCard()

void getCards() //获得多张牌

void getOneCard(int card , int index ) //在发牌过程中获得一张牌

void show()

}

void Ccard::getOneCard(int card , int index )

{

cardArray[index] = card

}

void Ccard::show()

{

for(int i=0i<cardNum i++)

{

if( cardArray[i] != 130 &&cardArray[i] !=140 )

{

switch(cardArray[i]%10)

{

case 0:

cout<<"黑桃:"

break

case 1:

cout<<"红桃:"

break

case 2:

cout<<"梅花:"

break

case 3:

cout<<"方片:"

break

}

}

switch(cardArray[i]/10)

{

case 0:

cout<<"3:"

break

case 1:

cout<<"4:"

break

case 2:

cout<<"5:"

break

case 3:

cout<<"6:"

break

case 4:

cout<<"7:"

break

case 5:

cout<<"8:"

break

case 6:

cout<<"9:"

break

case 7:

cout<<"10:"

break

case 8:

cout<<"J:"

break

case 9:

cout<<"Q:"

break

case 10:

cout<<"K:"

break

case 11:

cout<<"A:"

break

case 12:

cout<<"2:"

break

case 13:

cout<<"小王:"

break

case 14:

cout<<"大王:"

break

}

}

}

Ccard::Ccard()

{

for( int j = 0j<20 j++)

{

cardArray[j] = -1

}

}

Ccard::~Ccard()

{

}

void Ccard::getCards()

{

for(int i=0i<17 i++)

{

cardArray[i] = g_cards[i]

}

cardNum = 17

}

void Ccard::sortCard()

{

for( int j = 0j<cardNum-1 j++)

{

for(int i = 0i<cardNum-1-j i++)

{

if( cardArray[i]<cardArray[i+1] )

{

int temp

temp = cardArray[i]

cardArray[i] = cardArray[i+1]

cardArray[i+1] = temp

}

}

}

}

//******************************************************************************************

void fapai(Ccard&player1,Ccard&player2,Ccard&player3)

{

for(int i=0i<17i++)

{

player1.getOneCard( g_cards[i*3] , i)

player2.getOneCard( g_cards[i*3 + 1] , i)

player3.getOneCard( g_cards[i*3 + 2] , i)

player1.cardNum = 17

player2.cardNum = 17

player3.cardNum = 17

}

}

int main()

{

Ccard a

Ccard b

Ccard c

xipai() //第一步 洗牌

fapai(a,b,c) //第二步 发牌

//第三步 发地主牌

a.show()

cout<<"******************************************************"<<endl

b.show()

cout<<"******************************************************"<<endl

c.show()

return 0

}

以前学的时候写的不知道有没有用

洗牌的程序

#include <stdio.h>

#include <conio.h>

#include <stdlib.h>

#include <time.h>

#define N 52

int main(void)

{

int poker[N + 1]

int i, j, tmp, remain

/* 初始化阵列 */

for (i = 1i <= Ni++)

{

poker[i] = i

printf("%d ", poker[i])

}

printf("\n")

srand(time(0))

/* 洗牌 */

for (i = 1i <= Ni++)

{

j = rand() % N + 1

tmp = poker[i]

poker[i] = poker[j]

poker[j] = tmp

}

for (i = 1i <= Ni++)

{

printf("%d ", poker[i])

}

/* 判断花色 */

for (i = 1i <= Ni++)

{

switch((poker[i] - 1) / 13)

{

case 0:

printf("桃")break

case 1:

printf("心")break

case 2:

printf("砖")break

case 3:

printf("梅")break

}

/* 数字 */

remain = poker[i] % 13

switch(remain)

{

case 0:

printf("K ")break

case 12:

printf("Q ")break

case 11:

printf("J ")break

default:

printf("%d ", remain)break

}

if (i % 13 == 0)

printf("\n")

}

return 0

}


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/bake/11855413.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-19
下一篇 2023-05-19

发表评论

登录后才能评论

评论列表(0条)

保存