程序免看了,这我里有一个类似的,你有兴趣的话参照一下
#include <stdlibh>
#include <stdioh>
#include <timeh>
int main()
{
int i,j,temp,count=0; //控制变量 此处的count没有实际意义,用于统计随机共进行了几次
int card[52]={0}; //将抽到的牌号按顺序放
int color[52]={0}; //将抽到的牌花色按顺序放
int JUD[4][13]={0}; //要据其花色和牌号在数组中定位,可以确定其唯一性
srand(time(NULL)); //根据时间的随机
i=j=0; //初始化
while(i<52) //需要52轮有效的产生数
{
count++;
j=rand()%13; //产生牌号
temp=rand()%4; //产生花色
if(JUD[temp][j]==-1) //根据数组确定其惟一性,若已产生则作废,重新开始
continue;
JUD[temp][j]=-1; //若有效,标记为-1
card[i]=j; //记下此轮中的牌号
color[i]=temp; //记下此轮中牌的花色
i++; //进入下一次分牌
}
i=j=0; //重新初始化
while(i<4) //此处为分给四个人
{
j=0; //必须置1
while(j<52) //小于总数量
{
if(j%4==i) //分给第i个人
{
if(color[j]==0) //找出花色
printf("红");
else if(color[j]==1)
printf("黑");
else if(color[j]==2)
printf("方");
else
printf("梅");
if(card[j]==0) //找出牌号
printf("K ");
else if(card[j]==1)
printf("A ");
else if(card[j]==11)
printf("J ");
else if(card[j]==12)
printf("Q ");
else
printf("%d ",card[j]);
}
j++;
}
i++;
printf("\n");
}
printf("%d\n",count);
return 0;
}
#include
#include
#include
#include
#define SUIT 52
#define HALF 26
#define COLOR ((char)13)
// 扑克牌的编码是一组13进制的数据,从0编到51
// 函数tell用于解释编码的内容
int tell(char card, char description)
{
char i=0, j=0;
char suit[]={"Spades","Hearts","Clubs","Diamonds"};
char face[] = {"A","2","3","4","5","6","7","8","9",
"10","Jack","Queen","King"};
//题干比较奇怪,要求扣8张底牌,但是含大小王。正确的规则应该是不含大小王的情况下扣8张牌,每人11张牌就对了
//否则应该扣10张牌或者6张牌,否则每人发牌得到的牌数不相同(不是4的倍数)
//如需要,可自行定义JB=52,JA=53,SUIT=54,HALF=27即可。
//if (card > JB || card < 0 || !description) return 0;
//if (card == JB) { sprintf(description, "Big Joker"); return 1; }
//if (card == JA) { sprintf(description, "Little Joker"); return 1; }
i = card / COLOR; j = card % COLOR;
sprintf(description, "%s %s", suit[i], face[j]);
return 1;
}
//初始化牌,形成一个顺序牌
int init(char poker)
{
char i = 0;
if (!poker) return 0;
for (i=0; i<SUIT; i++) poker[i]=i;
srand((unsigned)time(0));
return 1;
}
//洗牌,将当前牌分成上下两组,然后交叉,有右手先和左手先两种可能,这一个是左手先交换法
int wash_left_hand(char poker)
{
char i=0, j=0, k=0;
char boker[SUIT];
if(!poker) return 0;
for(i=0; i<HALF; i++) {
j = i 2; //上半组
boker[j] = poker[i];
k = i + HALF; //下半组
boker[j+1]=poker[k];
}//next i
memcpy(poker, boker, SUIT);
return 1;
}
//洗牌,将当前牌分成上下两组,然后交叉,有右手先和左手先两种可能,这一个是右手先交换法
//注意左右手先后顺序的区别在于变量j的奇偶变化
int wash_right_hand(char poker)
{
char i=0, j=0, k=0;
char boker[SUIT];
if(!poker) return 0;
for(i=0; i<HALF; i++) {
j = i 2; //上半组
boker[j+1] = poker[i];
k = i + HALF; //下半组
boker[j]=poker[k];
}//next i
memcpy(poker, boker, SUIT);
return 1;
}
//在当前牌的基础上洗一次牌,主要用随机函数的奇偶性确定用左手洗还是右手洗
int wash_once(char poker)
{
return rand()%2wash_left_hand(poker):wash_right_hand(poker);
}
//在当前牌的基础上随机洗若干次牌,至少8次,最多18次
int wash_full(char poker)
{
int i = 0, j = 0, k = 0;
i = rand() % 10 + 8;
printf("\nTry to wash %d times\n", i);
for (j=0; j < i ; j++) k+=wash_once(poker);
return k;
}
int main(void)
{
char choice = 0, i = 0, j = 0; char poker[SUIT], card[16];
init(poker);
do {
printf("\n\tWelcome to My Poker Game!\n");
printf("\t0-Wash\n");
printf("\t1-Show A\n");
printf("\t2-Show B\n");
printf("\t3-Show C\n");
printf("\t4-Show D\n");
printf("\tany other number-quit\n");
printf("Your Choice");
scanf("%d", &choice);
if(choice 4) break;
if(choice ==0) {
wash_full(poker);
continue;
}//end if
i = (choice - 1) 11; //注意用11张牌,扣8张底牌
printf("His card is\n");
for(j = i; j<i+11; j++) {
tell(poker[j], card);
printf("%s\t", card);
}//next
printf("\n");
}while(1);
return 0;
}
#include <stdioh>
#include <stdlibh>
#include <timeh>
const int TOTAL = 52;
const int PLAYERS = 4;
const int MAXSIZE = TOTAL/PLAYERS;
const int TYPES = 4;
typedef struct card {
int type; // type 0:梅花,1:方片,2:红桃,3:黑桃
int point; // point = 2 -- 14
}CARD;
void sort(int a[][TYPES][MAXSIZE]) {
int i,j,k,m,n,t,size;
for(m = 0; m < PLAYERS; ++m) {
for(n = 0; n < TYPES; ++n) {
for(size = 0; a[m][n][size]; ++size);
for(i = 0; i < size - 1; ++i) {
k = i;
for(j = i + 1; j < size; ++j) {
if(a[m][n][k] < a[m][n][j]) k = j;
}
if(k != i) {
t = a[m][n][k];
a[m][n][k] = a[m][n][i];
a[m][n][i] = t;
}
}
}
}
}
void show(int a[][TYPES][MAXSIZE]) {
int i,j,k;
char type[TYPES] = {'\5','\4','\3','\6'};
char player[PLAYERS][6] = {"EAST","SOUTH","WEST","NORTH"};
char point[] = "JQKA";
for(k = 0; k < PLAYERS; ++k) {
printf("<%s>\n",player[k]);
for(i = 0; i < TYPES; ++i) {
printf(" %c ",type[i]);
for(j = 0; j < MAXSIZE && a[k][i][j];++j) {
if(a[k][i][j] < 11) printf("%d ",a[k][i][j]);
else printf("%c ",point[a[k][i][j] - 11]);
}
printf("\n");
}
printf("\n");
}
}
int Has(int a[][TYPES][MAXSIZE],CARD Card) {
int i,j;
for(i = 0; i < PLAYERS; ++i) {
for(j = 0; j < MAXSIZE && a[i][Cardtype][j]; ++j)
if(a[i][Cardtype][j] == Cardpoint)
return 1;
}
return 0;
}
int isFull(int a[][MAXSIZE]) {
int n = 0,i,j;
for(i = 0; i < TYPES; ++i) {
for(j = 0; a[i][j] && j < MAXSIZE; ++i)
++n;
}
return n == MAXSIZE;
}
int addCard(int a[PLAYERS][TYPES][MAXSIZE],CARD Card) {
int i = 0;
if(Has(a,Card)) return 0;
int who = rand()%PLAYERS;
while(isFull(a[who])) who = (who + 1)%PLAYERS;
while(a[who][Cardtype][i]) ++i;
a[who][Cardtype][i] = Cardpoint;
return 1;
}
int main() {
int i,playingCard[PLAYERS][4][MAXSIZE] = {0};
CARD t;
srand((unsigned)time(NULL));
for(i = 0; i < TOTAL; ++i) {
ttype = rand()%4;
tpoint = rand()%13 + 2;
if(addCard(playingCard,t) == 0) --i;
}
sort(playingCard);
show(playingCard);
return 0;
}
#include <timeh>
#include <stdioh>
#include <conioh>
#include <stdlibh>
#define PLAYER 4//玩家人数
#define NUM 13//玩家拿牌数
#define SIZE 52//所有牌数
//声明函数
void PokerRand(int pokerRand);
void Palyer(int pokerRand);
void Process(int countA, int countB, int countC, int countD);
void Output(int poker, int countA, int countB, int countC, int countD);
struct PokerGame
{
int A[NUM];//记录玩家手中的黑桃牌
int B[NUM];//记录玩家手中的红桃牌
int C[NUM];//记录玩家手中的梅花牌
int D[NUM];//记录玩家手中的方片牌
int manNum[NUM];//记录玩家手里所有的牌
}man[PLAYER];
//随机产生52张牌
void PokerRand(int pokerRand)
{
int i, j;
srand((unsigned)time(NULL));
for (i=0; i<SIZE; i++)
{
MARK: pokerRand[i] = rand()%52;
for (j=0; j<i; j++)
{
if (pokerRand[i] == pokerRand[j])
{
goto MARK;
}
}
}
}
//给4个玩家发牌
void Palyer(int pokerRand)
{
int i, j;
int count = 0;
for (j=0; j<NUM; j++)
{
for (i=0; i<PLAYER; i++)//轮流发牌
{
man[i]manNum[j] = pokerRand[count++];
}
}
}
//统计玩家手中的牌
void Process(int countA, int countB, int countC, int countD)
{
int i, j;
for (i=0; i<PLAYER; i++)
{
countA[i] = 0;
countB[i] = 0;
countC[i] = 0;
countD[i] = 0;
for (j=0; j<NUM; j++)//统计四个玩家手中的牌
{
if ((man[i]manNum[j] >= 0) && (man[i]manNum[j] < 13))//黑桃
{
man[i]A[ countA[i]++ ] = man[i]manNum[j];
}
else if (man[i]manNum[j] < 26)//红桃
{
man[i]B[ countB[i]++ ] = man[i]manNum[j];
}
else if (man[i]manNum[j] < 39)//梅花
{
man[i]C[ countC[i]++ ] = man[i]manNum[j];
}
else//方片
{
man[i]D[ countD[i]++ ] = man[i]manNum[j];
}
}
}
}
//输出
void Output(int poker, int countA, int countB, int countC, int countD)
{
int i, j;
printf("扑克牌自动发牌 %c(黑) %c(红) %c(梅) %c(方):\n", 6, 3, 5, 4);
for (i=0; i<PLAYER; i++)
{
printf("\n第%d人 :\n", i+1);//开始输出第i个玩家
printf("%c:\t", 6);//输出第i个玩家的黑桃牌
for (j=0; j<countA[i]; j++)
{
if (poker[ man[i]A[j] ] == 10)//假如等于10,以%d格式输出
{
printf("%4d", poker[ man[i]A[j] ]);
}
else//否则以%c格式输出
{
printf("%4c", poker[ man[i]A[j] ]);
}
}
printf("\n");
printf("%c:\t", 3);//输出第i个玩家的红桃牌
for (j=0; j<countB[i]; j++)
{
if (poker[ man[i]B[j] ] == 10)
{
printf("%4d", poker[ man[i]B[j] ]);
}
else
{
printf("%4c", poker[ man[i]B[j] ]);
}
}
printf("\n");
printf("%c:\t", 5);//输出第i个玩家的梅花牌
for (j=0; j<countC[i]; j++)
{
if (poker[ man[i]C[j] ] == 10)
{
printf("%4d", poker[ man[i]C[j] ]);
}
else
{
printf("%4c", poker[ man[i]C[j] ]);
}
}
printf("\n");
printf("%c:\t", 4);//输出第i个玩家的方片牌
for (j=0; j<countD[i]; j++)
{
if (poker[ man[i]D[j] ] == 10)
{
printf("%4d", poker[ man[i]D[j] ]);
}
else
{
printf("%4c", poker[ man[i]D[j] ]);
}
}
printf("\n");
}
}
void main(void)
{
int countA[PLAYER] = { 0 };//记录4个玩家持黑桃牌数
int countB[PLAYER] = { 0 };//记录4个玩家持红桃牌数
int countC[PLAYER] = { 0 };//记录4个玩家持梅花牌数
int countD[PLAYER] = { 0 };//记录4个玩家持方片牌数
int pokerRand[SIZE] = { 0 };//存放随机产生52张牌
int poker[SIZE] = {65, 50, 51, 52, 53, 54, 55, 56, 57, 10, 74, 81, 75,
65, 50, 51, 52, 53, 54, 55, 56, 57, 10, 74, 81, 75,
65, 50, 51, 52, 53, 54, 55, 56, 57, 10, 74, 81, 75,
65, 50, 51, 52, 53, 54, 55, 56, 57, 10, 74, 81, 75,};
PokerRand(pokerRand);//洗牌
Palyer(pokerRand);//发牌
Process(countA, countB, countC, countD);//整牌
Output(poker, countA, countB, countC, countD);//亮牌
printf("\n\n\n");
system("pause");
}
以上就是关于c语言 洗牌问题!全部的内容,包括:c语言 洗牌问题!、C语言编程——发牌洗牌模拟,求帮助、扑克牌洗牌发牌过程模拟等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)