码迷,mamicode.com
首页 > 其他好文 > 详细

21点游戏(链表操作)

时间:2018-07-05 23:21:51      阅读:237      评论:0      收藏:0      [点我收藏+]

标签:free   cpu   puts   bsp   结构体   log   als   and   end   

  1 // 21P.cpp : 定义控制台应用程序的入口点。
  2 //
  3 #include<stdio.h>
  4 #include<string.h>
  5 #include<time.h>
  6 #include<algorithm>
  7 #include<stack>
  8 #include<map>
  9 #include<iostream>
 10 using namespace std;
 11 int gCard[52];
 12 void initcards(); //初始化牌:a[] = {1,2,3,4,5,6,7,8,9,10,11,12,13,...}
 13 void shuffle(struct sCard *);   //洗牌
 14 void DisplayRule();      //printf游戏规则
 15 void init(sCard &);      //初始化结构体sCard
 16 int GetMoney(sCard sc);  //返回nDollar
 17 int SetGamble(sCard &,int);    //设置赌本,赌本不够返回-1
 18 void DisplayInfo(sCard);       //打印必要的信息
 19 void FirstPlayTwo(sCard &cpu, sCard &player);  //最初两张牌
 20 void DisplayPip(sCard);       //依次全部显示牌面点数
 21 void DisplayPip_C(sCard);     //除了第一张牌,依次全部显示牌面点数(针对计算机牌的显示)
 22 void PlayTurn(sCard &,sCard &);   //开始一局游戏
 23 int GetNumber(sCard);        //返回牌数
 24 void TurnPlay(sCard &cpu, sCard &player, bool flag);  //要一张牌,flag为true时player要牌,flag为false时cpu要牌
 25 int GetCurrentCard(sCard);   //返回当前牌点
 26 void Draw(sCard &);         //平局
 27 void Win(sCard &);          //
 28 void Lose(sCard &);         //
 29 int GetPip(sCard &);        //返回点数
 30 void Judge(sCard &,sCard &);
 31 class sCard
 32 {
 33     public:
 34     int naPip[5];           //一共5张牌
 35     int nNumber;            //发了多少张牌
 36     int nDollar;            //有多少钱
 37     int nGamble;            //赌注
 38     int nWin;               //赢局数
 39     int nLose;              //输局数
 40     int nDraw;              //平局数
 41     int card;
 42     struct sCard *next;
 43     struct sCard *before;
 44     struct sCard *ppq;
 45     void initcards()
 46     {
 47         int m,l=0,kk=0;
 48         struct sCard *head,*tail,*p;
 49         head=(struct sCard*)malloc(sizeof(struct sCard));
 50         head->before=NULL;
 51         head->next=NULL;
 52         tail=head;
 53         m=52;
 54         while(m--)
 55         {
 56             p=(struct sCard*)malloc(sizeof(struct sCard));
 57             p->next=NULL;
 58             p->before=NULL;
 59             p->card=(l++)%13+1;
 60             tail->next=p;
 61             p->before=tail;
 62             tail=p;
 63         }
 64         p=head;
 65         shuffle(p);
 66     }
 67     void shuffle(struct sCard *p)  //洗牌
 68     {
 69         srand((unsigned)time(NULL));
 70         struct sCard *head,*tail,*q,*t;
 71         int m=52,kk=1,ll=0;
 72         while(m)
 73         {
 74             q=p;
 75             int flag=1,k;
 76             k=rand()%m;
 77             while((q->next)!=NULL)
 78             {
 79                 tail=q->next;
 80                 if(flag==k)
 81                 {
 82                     ll++;
 83                     q->next=tail->next;
 84                     gCard[kk++]=tail->card;
 85                     free(tail);
 86                 }
 87                 q=q->next;
 88                 flag++;
 89             }
 90             if(k==m||k==1)
 91             {
 92                 if(k==1)
 93                 {
 94                     tail=q->next;
 95                     q->next=tail;
 96                     free(tail);
 97                 }
 98                 else
 99                 {
100                     q=q->before;
101                     q->next=NULL;
102                 }
103                 gCard[kk++]=q->card;
104             }
105 
106             m--;
107         }
108         m=1;
109         t=(struct sCard*)malloc(sizeof(struct sCard));
110         t->next=NULL;
111         tail=t;
112         while(m<=kk)
113         {
114             p=(struct sCard*)malloc(sizeof(struct sCard));
115             p->next=NULL;
116             tail->card=gCard[m];
117             tail->next=p;
118             tail=p;
119 
120             m++;
121 
122         }
123         ppq=t;
124 
125     }
126     void init(sCard &sc)
127     {
128         sc.naPip[0]=0;
129         sc.nNumber=0;
130         sc.nDollar=990;
131         sc.nGamble=0;
132         sc.nWin=0;
133         sc.nLose=0;
134         sc.nDraw=0;
135     }
136 
137     void DisplayRule()
138     {
139         puts("游戏双方的总积分若都小于21,则积分多的胜利,若一方爆牌则比输,若双方都爆牌或有效分数内积分分数则平局");
140         puts("");
141     }
142     int GetMoney(sCard sc)
143     {
144         return sc.nDollar;
145     }
146     int SetGamble(sCard &sc,int gamble)
147     {
148         if(gamble>sc.nDollar)
149             return -1;
150         return 1;
151     }
152     void PlayTurn(sCard &cpu,sCard &player)
153     {
154         int blPlayer,blCpu;
155         char chChoice;
156         FirstPlayTwo(cpu,player);
157         do
158         {
159             printf("您的牌点为:");
160             DisplayPip(player);
161             printf("计算机的牌点为:");
162             DisplayPip_C(cpu);
163             printf("您的牌面点数为:%d\n",GetPip(player));
164             if(blPlayer)
165             {
166                 cout<<"\n\n您是否继续要牌(Y/N)?";
167                 scanf(" %c",&chChoice);
168                 if((chChoice==Y||chChoice==y))
169                 {
170 
171                     if(player.nNumber<5&&GetPip(player)<21)
172                     {
173                         TurnPlay(cpu,player,1);
174                         cout<<"您要的这张牌是:"<<player.naPip[player.nNumber-1]<<endl;
175                     }
176                     else
177                     {
178                         blPlayer=0;
179                         if(player.nNumber>=5)
180                             cout<<"对不起,您已经要了5次牌了。不能再要牌了!";
181                         else
182                             cout<<"对不起,您的点数达到上限。不能再要牌了!";
183 
184                     }
185                 }
186             }
187             if((chChoice==N||chChoice==n))
188                 blPlayer=0;
189             if(cpu.nNumber<5&&GetPip(cpu)<16)   //cpu点数小于16点且牌数小于5张
190             {
191                 TurnPlay(cpu,player,0);
192                 printf("计算机要牌是:%d\n",GetCurrentCard(cpu));
193             }
194             else
195                 blCpu=0;
196             if(blCpu&&GetNumber(player)<5&&GetPip(player)<21)
197                 blPlayer=1;
198 
199         }
200         while(blCpu||blPlayer);
201         Judge(cpu,player);
202         return;
203     }
204     void DisplayInfo(sCard sc)
205     {
206         cout<<"你的赌本共计有$"<<sc.nDollar<<endl;
207     }
208     void FirstPlayTwo(sCard &cpu, sCard &player)
209     {
210         cpu.naPip[0]=ppq->card;
211         ppq=ppq->next;
212         player.naPip[0]=ppq->card;
213         ppq=ppq->next;
214         cpu.naPip[1]=ppq->card;
215         ppq=ppq->next;
216         player.naPip[1]=ppq->card;
217         ppq=ppq->next;
218         cpu.nNumber=2;
219         player.nNumber=2;
220 
221     }
222     void DisplayPip(sCard sc)
223     {
224         int i;
225         for(i=0; i<sc.nNumber; i++)
226         {
227             printf("%d",min(sc.naPip[i],10));
228             printf(" ");
229         }
230         printf("\n");
231     }
232     void DisplayPip_C(sCard sc)
233     {
234         printf("*");
235         int i;
236         for(i=1; i<sc.nNumber; i++)
237         {
238             printf(" %d",min(sc.naPip[i],10));
239         }
240         printf("\n");
241     }
242 
243     int GetPip(sCard &sc)
244     {
245         int sum=0;
246         for(int i=0; i<sc.nNumber; i++)
247             sum+=min(sc.naPip[i],10);
248         return sum;
249     }
250     int GetNumber(sCard sc)
251     {
252         return sc.nNumber;
253     }
254     void TurnPlay(sCard &cpu, sCard &player, bool flag)
255     {
256         if(flag)
257         {
258             player.naPip[player.nNumber]=ppq->card;
259             player.nNumber++;
260         }
261         else
262         {
263             cpu.naPip[cpu.nNumber]=ppq->card;
264             cpu.nNumber++;
265         }
266         ppq=ppq->next;
267     }
268     int GetCurrentCard(sCard sc)
269     {
270         return sc.naPip[sc.nNumber-1];
271 
272     }
273     void Judge(sCard &cpu,sCard &player)
274     {
275         if((GetPip(cpu)==GetPip(player))||(GetPip(cpu)>=21&&GetPip(player)>=21))
276         {
277             player.nDraw++;
278             printf("本局游戏双方平局\n");
279         }
280         else if((GetPip(cpu)<GetPip(player)&&GetPip(player)<=21)||(GetPip(player)<=21&&GetPip(cpu)>21))
281         {
282             printf("本局闲家胜\n");
283             cpu.nDollar-=cpu.nGamble;
284             player.nDollar+=player.nGamble;
285             player.nWin++;
286         }
287         else
288         {
289             printf("本局庄家胜\n");
290             cpu.nDollar+=cpu.nGamble;
291             player.nDollar-=player.nGamble;
292             player.nLose++;
293         }
294     }
295 
296     void Draw(sCard &sc)
297     {
298         printf("平局%d局.",sc.nDraw);
299     }
300     void Win(sCard &sc)
301     {
302         printf("在本轮游戏中您一共赢了%d局,",sc.nWin);
303     }
304     void Lose(sCard &sc)
305     {
306         printf("输了%d局,",sc.nLose);
307     }
308 };
309 
310 int main()
311 {
312 
313     sCard cpu, player,t;
314     t.init(cpu);
315     t.init(player);
316     int blLogic;
317     int nMoney;
318     t.DisplayRule();
319     char chChoice;
320     cout<<"是否现在开始游戏(Y/N)?\n";
321     cin>>chChoice;
322     while(chChoice==Y||chChoice==y)
323     {
324         t.initcards();
325         do
326         {
327             cout<<"您现在有赌本:$"<<t.GetMoney(player);
328             cout<<"\n请下注(赌注不能超过赌本):";
329             cin>>nMoney;
330             blLogic = t.SetGamble(player, nMoney);
331             cpu.nGamble=nMoney;
332             player.nGamble=nMoney;
333             if(blLogic==-1)
334                 cout<<"您的赌本不够,请重新下注!\n";
335         }
336         while(blLogic!=1);
337         cpu.nGamble=nMoney;
338         player.nGamble=nMoney;
339         t.PlayTurn(cpu,player);
340         printf("庄家点数为%d,闲家点数为%d\n",t.GetPip(cpu),t.GetPip(player));
341         printf("闲家余额为%d\n",player.nDollar);
342         cout<<"是否继续21点游戏(Y/N)?\n";
343         cin>>chChoice;
344     }
345     t.DisplayInfo(player);
346     t.Win(player);
347     t.Lose(player);
348     t. Draw(player);
349     cout<<"\n\n您是明智的,赌博是不好的!回去好好学习去~!\n"<<endl<<endl;
350     cout<<"欢迎再次使用本程序,该程序由富哥所写!\n\n";
351     return 0;
352 }

 

21点游戏(链表操作)

标签:free   cpu   puts   bsp   结构体   log   als   and   end   

原文地址:https://www.cnblogs.com/moomcake/p/9270546.html

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