C语言实现斗地主发牌程序.doc
Four short words sum up what has lifted most successful individuals above the crowd: a little bit more.-author-dateC语言实现斗地主发牌程序C语言实现斗地主发牌程序实验四、斗地主发牌程序的实现一、实验目的1. 了解线性表的顺序存储与基本操作;2. 了解线性表的链接存储与基本操作;二、实验工具VC6.0三、实验练习和要求3.1实验内容:1)考虑好纸牌的存储结构,定义好纸牌大小的比较规则;2)分别建立顺序线性表和链接线性表,可以进行定位删除,可以进行保序插入,可以进行顺序输出;3)利用上述线性表类,构造若干个线性表的实例,构造一个含有全部纸牌的线性表,从中随机抽取随机发给三家(每家17张),并保留底牌3张;4)按大小顺序分别输出各家的牌和底牌。3.2实验要求:1)利用王显示每家发得的牌和底牌(ASCII:5436);2)每家的牌要求有序排列(数字中3最小,2最大,不同的花色按,王渐大处理);3)所发的牌是随机的;4)顺序线性表和链接线性表都要用到。3.3实验难点:1)如何存储一张牌;2)如何比较牌的大小(345678910JQKA2);3)如何实现顺序表与链接表的无差别性?3.4实现提示1)54张牌可以用整数:0-53来表示,也可以用两个独立的整数来表示;2)用利用C语言的RAND()函数来产生随机数;3)牌的大小比较规则,除王比较大外,一般的牌先比较点儿数,再比较花色;4)建议用保序插入,而不要使用最后排序。3.5注意事项1)顺序表和链接表的操作接口要尽量一致;2)345678910JQKA2中的10占两位?3)大王与小王的特殊处理。3.6本次实验可以三人构成学习小组,上交一份作业,并给出每一个人贡献分数。四、实验内容本次实验分别使用顺序表和单链表实现,三人手中的牌都已经按照大小以及花色从小到大排序。代码如下:Ø 顺序表实现随机发牌#include<stdio.h>#include<malloc.h>#include <time.h>#include <stdlib.h>#define MaxSize 100typedef int dataType;typedef struct dataType dataMaxSize;int size ;SqList;SqList* CreateList(dataType a,int n)SqList*t=(SqList*)malloc(sizeof(SqList);for(int i=0;i<n;i+)t->datai=ai;t->size=n;return t;SqList* CreateListemp()SqList *t =(SqList *)malloc(sizeof(SqList);t->size=0;return t;void Exchange(SqList *l,int i,int j)int temp;temp=l->datai;l->datai=l->dataj;l->dataj=temp;int Min(SqList *l)int min=l->data0;for(int i=1;i<l->size;i+)if(l->datai<min) min=l->datai; return min;void Delete(SqList *l,int k)if (k<1|k>l->size) exit(1);for (int i=k;i<l->size;i+)l->datai-1 =l->datai;l->size-;int Locate(SqList *l,dataType x)for(int i=0;i<l->size;i+)if(l->datai=x) return i+1;return 0;void Print(SqList *l)for (int i=0;i<l->size;i+)printf("%d ",l->datai); SqList* Sort(SqList *pa)SqList *pb=CreateListemp();int M;for(int i=0;i<17;i+)M=Min(pa);pb->datai=M;Delete(pa,Locate(pa,M);pb->size+;return pb;int main()int a54,i,j,N;int one17;int two17;int three17;int cover3; char *poker="梅花3","方块3","红桃3","黑桃3","梅花4","方块4","红桃4","黑桃4","梅花5","方块5","红桃5","黑桃5","梅花6","方块6","红桃6","黑桃6","梅花7","方块7","红桃7","黑桃7","梅花8","方块8","红桃8","黑桃8","梅花9","方块9","红桃9","黑桃9","梅花10","方块10","红桃10","黑桃10","梅花J","方块J","红桃J","黑桃J","梅花Q","方块Q","红桃Q","黑桃Q","梅花K","方块K","红桃K","黑桃K","梅花A","方块A","红桃A","黑桃A","梅花2","方块2","红桃2","黑桃2","小王","大王" printf("顺序表实现:3个人,每人随机发17张牌,留3张底牌。nn") ;for(i=0,j=1;i<54;i+)ai=j;j+;SqList *pl=CreateList(a,54);srand(unsigned)time(0);for(N=0;N<MaxSize;N+)i=rand()%54;j=rand()%54;Exchange(pl,i,j);for(i=0,j=0;j<17;j+,i+)onei=pl->dataj;for(i=0,j=17;j<34;j+,i+)twoi=pl->dataj;for(i=0,j=34;j<51;j+,i+)threei=pl->dataj;for(i=0,j=51;j<54;j+,i+)coveri=pl->dataj;SqList *first=CreateList(one,17);SqList *second=CreateList(two,17);SqList *third=CreateList(three,17);SqList *left=CreateList(cover,3);printf("-n"); printf(" 按照大小以及花色排序后的结果n"); printf("-n"); SqList *sortedfirst=Sort(first);SqList *sortedsecond=Sort(second);SqList *sortedthird=Sort(third);printf("第一个人的牌:"); for(i=0;i<17;i+)printf(" %s",pokersortedfirst->datai-1); printf("n");printf("第二个人的牌:"); for(i=0;i<17;i+)printf(" %s",pokersortedsecond->datai-1);printf("n");printf("第三个人的牌:"); for(i=0;i<17;i+)printf(" %s",pokersortedthird->datai-1);printf("n"); printf(" 底牌:");for(i=0;i<3;i+)printf(" %s",pokerleft->datai-1);return 0;运行两次结果截图:Ø 单链表实现随机发牌#include<stdio.h>#include<malloc.h>#include <time.h>#include <stdlib.h>typedef int ElemType;typedef struct nodeElemType data; struct node *next;slink;slink *creslink(ElemType a,int n)slink *head,*p,*s; int i; p=head=(slink *)malloc(sizeof(slink); for(i=1;i<=n;i+) s=(slink *)malloc(sizeof(slink);s->data=ai-1;p->next=s;p=s; p->next=NULL;return head;slink *creslinkemp()slink *head,*p; p=head=(slink *)malloc(sizeof(slink);p->next=NULL;return head;int getelem(slink *head,int i,ElemType *e) slink *p; int j; p=head->next; j=1; while(p!=NULL&&j<i) p=p->next;j+; if(p=NULL) return 0; *e=p->data; return 1;int insert(slink *head,int i,ElemType x) slink *p,*q; int j; p=head;j=0; while(p!=NULL&&j<(i-1) p=p->next;j+; if(p=NULL) return 0; q=(slink *)malloc(sizeof(slink); q->data=x; q->next=p->next; p->next=q; return 1;int Delete(slink *head,int i) slink *p,*q; int j; if(i<1) return 0; p=head;j=0; while(p->next!=NULL&&j<i-1) p=p->next;j+; if(p->next=NULL) return 0; q=p->next; p->next=q->next; free(q); return 1;void sort(int a,int size)for(int i=0;i<size-1;i+)int tmpmin=i;for(int j=i+1;j<size;j+)if(aj<atmpmin)tmpmin=j;int tmp=ai;ai=atmpmin;atmpmin=tmp;void Print(slink *head,char *a) slink *p; p=head->next; while(p!=NULL) printf(" %s",a(p->data)-1); p=p->next; printf("n");int main()int a54,r,R;int one17;int two17;int three17;int cover3; printf("单链表实现:3个人,每人随机发17张牌,留3张底牌。n") ;for(int i=0,j=1;i<54;i+)ai=j;j+;char *poker="梅花3","方块3","红桃3","黑桃3","梅花4","方块4","红桃4","黑桃4","梅花5","方块5","红桃5","黑桃5","梅花6","方块6","红桃6","黑桃6","梅花7","方块7","红桃7","黑桃7","梅花8","方块8","红桃8","黑桃8","梅花9","方块9","红桃9","黑桃9","梅花10","方块10","红桃10","黑桃10","梅花J","方块J","红桃J","黑桃J","梅花Q","方块Q","红桃Q","黑桃Q","梅花K","方块K","红桃K","黑桃K","梅花A","方块A","红桃A","黑桃A","梅花2","方块2","红桃2","黑桃2","小王","大王" slink *paNum=creslink(a,54);slink *pbNum=creslinkemp();for(int x=1,n=54;x<=54;x+,n-)srand(unsigned)time(0);r=(rand()%n)+1;getelem(paNum,r,&R);insert(pbNum,x,R);Delete(paNum,r);printf("n");slink *pone=pbNum->next;for(int i=0;i<17;i+)onei=pone->data;pone=pone->next;slink *ptwo=pbNum->next;for(int i=0;i<17;i+)ptwo=ptwo->next;for(int i=0;i<17;i+)twoi=ptwo->data;ptwo=ptwo->next;slink *pthree=pbNum->next;for(int i=0;i<34;i+)pthree=pthree->next;for(int i=0;i<17;i+)threei=pthree->data;pthree=pthree->next;slink *pcover=pbNum->next;for(int i=0;i<51;i+)pcover=pcover->next;for(int i=0;i<3;i+)coveri=pcover->data;pcover=pcover->next;sort(one,17);sort(two,17);sort(three,17);slink *sortedfirst=creslink(one,17);slink *sortedsecond=creslink(two,17);slink *sortedthird=creslink(three,17);slink *Cover=creslink(cover,3);printf("-n"); printf(" 按照大小以及花色排序后的结果n"); printf("-n"); printf("第一个人的牌:"); Print(sortedfirst,poker);printf("第二个人的牌:"); Print(sortedsecond,poker);printf("第三个人的牌:"); Print(sortedthird,poker);printf(" 底牌:");Print(Cover,poker);return 0;运行两次结果截图:五、疑难问题的解决和感想错误1:错误:顺序表实现中的Min函数:将for(int i=1;i<l->size;i+)错写成for(int i=1;i<17;i+)分析:所以在之后Sort函数:该函数通过for循环不断找出参数顺序表的最小值放入新的顺序表,然后删除原表中的原最小值,再进入新的循环直至将所有原顺序表元素按从小到大顺序排列在新顺序表中,返回新顺序表。由于只考虑了第一次for循环原顺序表中17个元素,就把Min函数循环判断条件写为i<17,殊不知Sort函数循环中每执行一次循环原顺序表就会减少一个元素,所以Min函数中的循环判断条件也应每循环一次减一。所以Min函数中循环判断条件应为作为参数的顺序表的元素个数即i<l->size错误2:错误:poker数组下标没有减一分析:以字符串形式输出三个人分配的数字:字符串指针数组的下标是从0到53,而三人所分配的数字是从1到54,所以当三人所分配数字作为poker对应下标时应当减一即输出pokersortedxxx->datai-1-