编程语言   发布时间:2022-06-26  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了队列和栈实现纸牌游戏大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
  1 #include <stdio.h>
  2 struct stack{
  3     int top;
  4     int data[100];
  5 };
  6 struct queue{
  7     int head;
  8     int tail;
  9     int paper[1000];
 10 };
 11 int book[10];//代表十种牌面
 12 int main()
 13 {
 14     struct stack desk;
 15     struct queue q1,q2;
 16     int t;
 17     q1.head=q1.tail=1;//初始化手中的牌
 18     q2.head=q2.tail=1;//初始化手中的牌
 19     desk.top=0;//桌面牌清空
 20     printf("Please input Player 1 cards:");
 21     for(int i=1;i<=10;i++)
 22     {
 23         scanf("%d",&q1.paper[q1.tail]);//输入玩家1手中的牌
 24         q1.tail++;
 25     }
 26     printf("Please input Player 2 cards:");
 27     for(int i=1;i<=10;i++)
 28     {
 29         scanf("%d",&q2.paper[q2.tail]);//输入玩家2手中的牌
 30         q2.tail++;
 31     }
 32     while(q1.head<q1.tail&&q2.head<q2.tail)
 33     {
 34         t=q1.paper[q1.head];
 35         if(book[t]==0)//如果此轮玩家1没有赢牌
 36         {
 37             q1.head++;
 38             desk.top++;//桌子上的牌数增加1张
 39             desk.data[desk.top]=t;//把牌放入桌子
 40             book[t]=1;//桌子上已经存在此牌
 41         }
 42         else//如果此轮可以赢牌
 43         {
 44             q1.head++;
 45             q1.paper[q1.tail]=t;
 46             q1.tail++;
 47             while(desk.data[desk.top]!=t)
 48             {
 49                 book[desk.data[desk.top]]=0;//取消桌面排的标记
 50                 q1.paper[q1.tail]=desk.data[desk.top];//把桌面上的牌放入玩家1手中
 51                 q1.tail++;
 52                 desk.top--;
 53             }
 54             book[desk.data[desk.top]]=0;
 55             q1.paper[q1.tail]=desk.data[desk.top];
 56             q1.tail++;
 57             desk.top--;
 58         }
 59         if(q1.head==q1.tail)//如果玩家1的牌已经出完了
 60             break;
 61         t=q2.paper[q2.head];
 62         if(book[t]==0)//如果此轮玩家2没有赢牌
 63         {
 64             q2.head++;
 65             desk.top++;//桌子上的牌数增加1张
 66             desk.data[desk.top]=t;//把牌放入桌子
 67             book[t]=1;//桌子上已经存在此牌
 68         }
 69         else//如果此轮玩家2可以赢牌
 70         {
 71             q2.head++;
 72             q2.paper[q2.tail]=t;
 73             q2.tail++;
 74             while(desk.data[desk.top]!=t)
 75             {
 76                 book[desk.data[desk.top]]=0;//取消桌面排的标记
 77                 q2.paper[q2.tail]=desk.data[desk.top];//把桌面上的牌放入玩家1手中
 78                 q2.tail++;
 79                 desk.top--;
 80             }
 81             book[desk.data[desk.top]]=0;
 82             q2.paper[q2.tail]=desk.data[desk.top];
 83             q2.tail++;
 84             desk.top--;
 85         }
 86         if(q2.head==q2.tail)
 87             break;
 88     }
 89     if(q1.head==q1.tail)
 90     {
 91         printf("Winner is 1!n");
 92         printf("Player 2 have:");
 93         while(q2.head!=q2.tail)//打印玩家二手中的牌
 94         {
 95             printf("%d ",q2.paper[q2.head]);
 96             q2.head++;
 97         }
 98         printf("n");
 99         if(desk.top>0)//如果此时桌子上还有牌,则输出
100         {
101             printf("Desk have:");
102             while(desk.top>0)
103             {
104                 printf("%d ",desk.data[desk.top]);
105                 desk.top--;
106             }
107         }
108         else
109         {
110             printf("Desk have no cards!n");
111         }
112     }
113     else
114     {
115         printf("Winner is 2!n");
116         printf("Player 1 have:");
117         while(q1.head!=q1.tail)//打印玩家二手中的牌
118         {
119             printf("%d ",q1.paper[q1.head]);
120             q1.head++;
121         }
122         printf("n");
123         if(desk.top>0)//如果此时桌子上还有牌,则输出
124         {
125             printf("Desk have:");
126             while(desk.top>0)
127             {
128                 printf("%d ",desk.data[desk.top]);
129                 desk.top--;
130             }
131         }
132         else
133         {
134             printf("Desk have no cards!n");
135         }
136     }
137 }

 

大佬总结

以上是大佬教程为你收集整理的队列和栈实现纸牌游戏全部内容,希望文章能够帮你解决队列和栈实现纸牌游戏所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。
标签:while