洗牌、发牌模拟程序:可用二维数组,行标表示花色,列标表示是什么牌。牌的先后顺序由表示牌的二维数组元素的值决定。1表示第一张,2表示第二张,……
所谓的发牌您想怎样体现?输出么?
以玩"七鬼五二三"为例,洗牌并输出玩家的5张牌:
#include<stdio.h>
#include<string.h>
#include<time.h>
#include<stdlib.h>
struct CARD
{
char suit;
char face[10];
};
typedef struct CARD puke;
puke card[54],a[5],b[5];
void riffle(void)
{
char Suit[4]={"\4\3\5\6"};
char *Face[]={"4","6","8","9","10","Jack","Queen","King","A","3","2","5","7"};
int i,j;
puke temp;
strcpy(card[53].face,"BigJoke");
card[53].suit=' ';
strcpy(card[52].face,"SmallJoke");
card[52].suit=' ';
for(i=0;i<52;i++)
{
card[i].suit=Suit[i%4];
strcpy(card[i].face,Face[i/4]);
}
srand(time(NULL));
for(i=0;i<54;i++)
{
j=rand()%54;
temp=card[i];
card[i]=card[j];
card[j]=temp;
}
}
void putcard(void)
{
int i,counter=0;
for(i=0;i<10;i++)
{
if((i%2)==0)
a[i/2]=card[counter++];
else
b[(i-1)/2]=card[counter++];
}
}
void Print(void)
{
int i;
puts("player's cards:");
for(i=0;i<5;i++)
printf("%c %s\n",a[i].suit,a[i].face);
}
int main()
{
riffle();
putcard();
Print();
getch();
}
#include <conio.h>
#include <stdio.h>
#include <stdlib.h>
struct poker * Createpoker(int n);
void Initpoker(struct poker * p);
void Washpoker(struct poker *p);
void Handout(struct poker *allpoker, struct poker *mypoker, int no);
void Printcard(struct poker * p, int n);
struct poker
{ int type;
int num;
struct poker * next;
}
main()
{ int i;
struct poker * allpoker;
struct poker * mypoker[4];
allpoker = Createpoker(52);
for(i=0; i<4; i++)
{
mypoker[i] = Createpoker(13);
}
Initpoker( allpoker ) ;
Washpoker( allpoker );
for(i=0; i<4; i++)
{
Handout(allpoker ,mypoker[i],i+1);
printf("\n\npleayer[%d]:\n",i+1);
Printcard(mypoker[i],13);
}
getch(); /* 请不要删除此行 */
}
struct poker * Createpoker(int n)
{
struct poker *p;
struct poker *q;
int i ;
q=NULL;
for(i=1; i<=n; i++)
{
p=(struct poker *)malloc(sizeof(struct poker));
p->next=q;
q=p;
}
return q;
}
void Initpoker(struct poker * p)
{
int i=13;
int j=4;
struct poker *q=p;
for(j=1; j<=4; j++)
{
for(i=1; i<=13; i++)
{
q->type=j;
q->num=i;
q=q->next;
}
}
}
void Washpoker(struct poker *p)
{
int i;
int j;
int temptype;
int tempnum;
struct poker *q;
struct poker *r;
q=p;
randomize();
while( q )
{
j=1+random(52);
r=p;
for(i=1; i<j; i++)
{
r=r->next;
}
temptype=r->type;
tempnum=r->num;
r->type=q->type;
r->num=q->num;
q->type=temptype;
q->num=tempnum;
q=q->next;
}
}
void Handout(struct poker *allpoker, struct poker *mypoker,int no)
{
int i;
struct poker * card=allpoker;
struct poker * mycard=mypoker;
int temptype;
int tempnum;
for(i=1; i<no; i++)
{
card=card->next;
}
for(i=1; i<=13; i++)
{
temptype=card->type;
tempnum=card->num;
card->type=mycard->type;
card->num=mycard->num;
mycard->type=temptype;
mycard->num=tempnum;
mycard=mycard->next;
card=card->next;
card=card->next;
card=card->next;
card=card->next;
}
}
void Printcard(struct poker * p, int n)
{
int i;
struct poker *q=p;
for(i=1; i<=n; i++)
{
if (q->num<=10 && q->num>=2)
{
printf("[%2d]: %c%2d ", i, q->type+2, q->num);
}
else if ( q->num == 1 )
{
printf("[%2d]: %c%2c ", i, q->type+2, 'A');
}
else if (q->num == 11)
{
printf("[%2d]: %c%2c ", i, q->type+2, 'J');
}
else if (q->num == 12)
{
printf("[%2d]: %c%2c ", i, q->type+2, 'Q');
}
else if (q->num == 13)
{
printf("[%2d]: %c%2c ", i, q->type+2, 'K');
}
q=q->next;
delay(60000);
}
}
[此贴子已经被作者于2006-6-14 0:35:15编辑过]
#include <dos.h>
#include <time.h>
#include <math.h>
#include <stdio.h>
#include <conio.h>
#define MAX 52
#define NUMBER 10
#define N 2
main()
{
int i,j,k,n,order,count,num[MAX];
int data[4][NUMBER],adjust=3;
int left=20,right=70,top=1,bottom=20,xrate=4,yrate=4,wide=2,high=2;
float m=1.0,lumda=1.0,x;
void initiate(float *,float *,float *,long);
float randequ(float,float,float);
void output(int);
system("cls");
initiate(&m,&lumda,&x,time(NULL));
for (i=0;i<MAX;i++)
num[i]=i;
for (n=MAX-1,count=0;n>=0;n--,count++)
{
x=randequ(lumda,x,m); /*x between 0 and 1;*/
order=x*(MAX-count);
x*=m;
if (4*NUMBER>count)
data[count%4][count/4]=num[order];
num[order]=num[n];
}
for (i=0;i<3;i++)
{
if (i%2==0)
for (j=0;j<NUMBER;j++)
{
window(left+j*xrate,top,left+wide+j*xrate,top+high);
textbackground(7);
clrscr();
if (data[i][j]%4<2)
textcolor(RED); /*red color=4*/
else
textcolor(BLACK); /*black color=0*/
cprintf("%c",data[i][j]%4+3); /* char's ASCII is start from 3*/
output(data[i][j]%13);
}
else
{
for (j=0;j<NUMBER/N;j++)
{
for (k=0,n=0;k<N;k++,n+=2)
{
window(1+k*wide+n,top+(j+1)*yrate-adjust,1+(k+1)*wide+n,top+high+(j+1)*yrate-adjust);
textbackground(7);
clrscr();
if (data[i][j+k*NUMBER/N]%4<2)
textcolor(RED);
else
textcolor(BLACK);
cprintf("%c",data[i][j+k*NUMBER/N]%4+3);
output(data[i][j+k*NUMBER/N]%13);
}
for (k=0,n=0;k<N;k++,n+=2)
{
window(right+k*wide+n,top+(j+1)*yrate-adjust,right+(k+1)*wide+n,top+high+(j+1)*yrate-adjust);
textbackground(7);
clrscr();
if (data[i+2][j+k*NUMBER/N]%4<2)
textcolor(RED);
else
textcolor(BLACK);
cprintf("%c",data[i+2][j+k*NUMBER/N]%4+3);
output(data[i+2][j+k*NUMBER/N]%13);
}
}
top=bottom;
}
}
getch();
}
/*============initiate()=============*/
void initiate(float *m,float *lumda,float *x,long t)
{
int i;
for (i=0;i<5;i++)
*lumda*=5;
for (i=0;i<35;i++)
*m*=2;
*m-=31;
*x=t%1000;
}
/*============randequ()==============*/
float randequ(float lumda,float x0,float M)
{
return fmod(lumda*x0,M)/M;
}
/*============output()============*/
void output(int i)
{
if (i==0)
cprintf(" A");
else if (i<9)
cprintf(" %c",i+'1');
else if (i==9)
cprintf("10");
else if (i==10)
cprintf(" J");
else if (i==11)
cprintf(" Q");
else if (i==12)
cprintf(" K");
}