码迷,mamicode.com
首页 > 编程语言 > 详细

自己设置的纸牌游戏(简单数组栈和队列的设计)

时间:2017-07-22 18:29:09      阅读:336      评论:0      收藏:0      [点我收藏+]

标签:ret   钓鱼   扑克牌   win   log   通过   str   ace   规则   

星期天小白与小婷约在一起玩桌游,他们正在玩一个非常古怪的扑克游戏——“小猫钓鱼”。游戏的规则是这样的:将一副扑克牌平均分成俩份,每人拿一份。小白先拿出手中的第一张扑克牌放在桌上,然后小婷也拿出手中的第一张扑克牌,并放在小白刚打出的扑克牌上面,就像这样俩人交替出牌。出牌时,如果某人打出的牌与桌上某张牌的牌面相同,即可将俩张相同的牌以及中间所夹着的牌全部取走,并依次放到自己手中牌的末尾。当任意一个人的牌全部出完时,游戏结束,对手获胜!

现在,我为了方便测试只涉及一组数据,因为数据如果不对的话将永远循环下去。

现在假设小白的6张牌顺序为2 4 1 2 5 6,小婷的为3 1 3 5 6 4,最终谁会获胜呢?

自我心得:很有趣的小游戏,通过这个游戏,自己设置了一些符合自己需要的栈和队列,模拟的时候需保持全局观念,不然很容易出错。

最终情况为:
小婷 Win
小婷手中牌为:1 6 5 2 3 4 1
桌上牌为:3 4 5 6 2(注意桌上此时是从第一张开始显示的)

  1 #include<iostream>
  2 #include<vector>
  3 #include<iostream>
  4 using namespace std;
  5 struct myqueue
  6 {
  7     int date[100];
  8     int top;
  9     int tail;
 10     myqueue()
 11     {
 12         top=-1;
 13         tail=-1;
 14 
 15     }
 16     int myempty()
 17     {
 18         return top==tail;
 19 
 20 
 21     }
 22    int mytop()
 23     {
 24         if(myempty())
 25             return 0;
 26         return date[top];
 27 
 28 
 29     }
 30     void myinsert(int x)
 31     {
 32         if(top==-1)
 33         {
 34             date[++top]=x;
 35             tail=1;
 36         }
 37         else
 38         {
 39             date[tail]=x;
 40             tail++;
 41 
 42 
 43         }
 44     }
 45     void mypop()
 46     {
 47         if(top<tail)
 48         top++;
 49         else
 50         {
 51             cout<<"空队列"<<endl;
 52         }
 53     }
 54     void print()
 55     {
 56         for(int i=top;i<tail;i++)
 57             cout<<date[i]<<" ";
 58             cout<<endl;
 59 
 60     }
 61 
 62 
 63 };
 64 struct mystack
 65 {
 66     int date[100];
 67     int top;
 68 
 69     mystack()
 70     {
 71         top=-1;
 72     }
 73     int mytop()
 74     {
 75         if(top==-1) return 0;
 76         return date[top];
 77     }
 78     void mypush(int x)
 79     {
 80         date[++top]=x;
 81     }
 82     void mypop()
 83     {
 84         if(top!=-1)
 85             top--;
 86     }
 87     int findx(int x)
 88     {
 89         int ok=-1;
 90         for(int i=top-1;i>=0;i--)
 91         if(date[i]==x) {ok=i;break;}
 92         return ok;
 93 
 94 
 95     }
 96     void popok(int x,myqueue &y)
 97     {
 98         for(int i=top;i>=x;i--)
 99                 y.myinsert(date[i]);
100         top=x-1;
101 
102     }
103     void print()
104     {
105         for(int i=0;i<=top;i++)
106             cout<<date[i]<<" ";
107             cout<<endl;
108 
109     }
110 
111 
112 
113 };
114 
115 int main()
116 {
117 
118     myqueue x1,x2;
119     mystack x;
120     int a[6]={2,4,1,2,5,6};
121     int b[6]={3,1,3,5,6,4};
122     for(int i=0;i<6;i++)
123     {
124         x1.myinsert(a[i]);
125         x2.myinsert(b[i]);
126 
127     }
128 
129     while(!x1.myempty()&&!x2.myempty())
130     {
131 
132         x.mypush(x1.mytop());
133         int t=x1.mytop();
134         x1.mypop();
135         if(x.findx(t)>=0)
136         {
137             x.popok(x.findx(t),x1);
138 
139         }
140          if(x1.myempty()) break;
141         x.mypush(x2.mytop());
142         int m=x2.mytop();
143         x2.mypop();
144          if(x.findx(m)>=0)
145         {
146             x.popok(x.findx(m),x2);
147 
148         }
149     }
150     if(x1.myempty()) {cout<<"小婷 Win"<<endl;
151     cout<<"小婷手中牌为:";
152     x2.print();
153     cout<<"桌子上牌为:";
154     x.print();
155 
156 
157     }
158     else {cout<<"小白 Win"<<endl;
159     cout<<"小白手中牌为:";
160     x1.print();
161     cout<<"桌子上牌为:";
162     x.print();
163     }
164     return 0;
165 
166 }

 

自己设置的纸牌游戏(简单数组栈和队列的设计)

标签:ret   钓鱼   扑克牌   win   log   通过   str   ace   规则   

原文地址:http://www.cnblogs.com/blvt/p/7221917.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!