我可不是为了被全人类喜欢才活着的,只要对于某一个|

王陸

园龄:6年11个月粉丝:2052关注:178

21点游戏(套模板的魔改板)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
#include<cstring>
#include<iostream>
#include<ctime>
#include<cstdlib>
#include<stdio.h>
using namespace std;
int gCard[52];
int num_card;
struct sCard
{
    int naPip[5];           //一共5张牌
    int nNumber;            //发了多少张牌
    int nDollar;            //有多少钱
    int nGamble;            //赌注
    int nWin;               //赢局数
    int nLose;              //输局数
    int nDraw;              //平局数
};
void initcards(int a[]); //初始化牌:a[] = {1,2,3,4,5,6,7,8,9,10,11,12,13,...}
void shuffle(int a[]);   //洗牌
void DisplayRule();      //printf游戏规则
void init(sCard &);      //初始化结构体sCard
int GetMoney(sCard sc);  //返回nDollar
int SetGamble(sCard &,int);    //设置赌本,赌本不够返回-1
void DisplayInfo(sCard);       //打印必要的信息
void FirstPlayTwo(sCard &cpu, sCard &player);  //最初两张牌
void DisplayPip(sCard);       //依次全部显示牌面点数
void DisplayPip_C(sCard);     //除了第一张牌,依次全部显示牌面点数(针对计算机牌的显示)
void PlayTurn(sCard &,sCard &);   //开始一局游戏
int GetNumber(sCard);        //返回牌数
void TurnPlay(sCard &cpu, sCard &player, bool flag);  //要一张牌,flag为true时player要牌,flag为false时cpu要牌
int GetCurrentCard(sCard);   //返回当前牌点
void Draw(sCard &);         //平局
void Win(sCard &);          //嬴
void Lose(sCard &);         //输
int GetPip(sCard &);        //返回点数
void Judge(sCard &,sCard &);
int main()
{
    srand((unsigned)time(NULL));
    sCard cpu, player;
    init(cpu);///玩家初始化
    init(player);///电脑初始化
    int blLogic;
    int nMoney;
    DisplayRule();///游戏规则
    char chChoice;
    cout<<"是否现在开始游戏(Y/N)?\n";
    cin>>chChoice;
    while(chChoice=='Y'||chChoice=='y')
    {
        num_card=0;
        do
        {
            cout<<"您现在有赌本:$"<<GetMoney(player);
            cout<<"\n请下注(赌注不能超过赌本):";
            cin>>nMoney;
            blLogic = SetGamble(player, nMoney);
            if(blLogic)
                cout<<"您的赌本不够,请重新下注!\n";
        }
        while(blLogic);///直到输入正确的赌本为止
        PlayTurn(cpu,player);
        cout<<"是否继续21点游戏(Y/N)?\n";
        cin>>chChoice;
    }
    DisplayInfo(player);
    cout<<"\n\n您是明智的,赌博是不好的!回去好好学习去~!\n"<<endl<<endl;
    cout<<"欢迎再次使用本程序,该程序由蒲大人抄录!\n\n";
    return 0;
}
 
void initcards(int a[52])///初始化牌
{
    int i,j,k;
    for(i=0; i<4; i++)
    {
        for(j=0; j<13; j++)
        {
            gCard[k++]=j+1;
        }
    }
}
 
void shuffle(int a[])///洗牌函数
{
    int x[4][13],i,j,num,k;
    int b[52]= {0};
    srand(time(NULL));
    for(i=0; i<4; i++)//用来储存扑克牌
    {
        for(j=0; j<13; j++)
        {
            x[i][j]=j+1;
        }
    }
    num=0;
    while(1)//产生52张扑克牌
    {
        if(num>=52)
        {
            break;
        }
        k=rand()%52;
        for(j=0; j<num; j++)//之前产生的扑克牌将不会再次出现,在这里只会出现0~52
        {
            if(k==b[j])
            {
                break;
            }
        }
        if(j==num)
        {
            b[num]=k;
            gCard[num]=x[k/13][k%13];
            num++;
        }
    }
    return ;
}
 
void init(sCard &sc)
{
    sc.nNumber = 0;
    sc.nDollar = 100;//设赌注为100块钱
    for(int i = 0; i<5; i++)
        sc.naPip[i] = 0;
    sc.nGamble = 0;
    sc.nWin = 0;
    sc.nLose = 0;
    sc.nDraw = 0;
}
 
void DisplayRule()
{
    printf("澳门皇家赌场正式上线了\n");
    printf("先推出21点小游戏\n");
    printf("游戏规则如下:\n");
    printf("玩家最多可以有5张牌\n");
    printf("如果牌点总数大于21则爆点,失败\n");
    printf("新手,祝你好运!\n");
}
 
int GetMoney(sCard sc)///返回赌资
{
    return sc.nDollar;
}
 
int SetGamble(sCard &sc,int gamble)//设置赌本
{
    if((sc.nDollar - gamble<0)||gamble<0)///不够赌本
    {
        return -1;
    }
    sc.nGamble=gamble;
    sc.nDollar -= sc.nGamble;///玩一局都会将赌注当做押金
    return 0;
}
 
void PlayTurn(sCard &cpu,sCard &player)
{
 
    char chChoice;
    int blCpu = 1,blPlayer = 1;
    initcards(&gCard[52]);
    shuffle(&gCard[52]);
    FirstPlayTwo(cpu,player);
    do
    {
        cout<<"您的牌点:\t";
        DisplayPip(player);
        cout<<"计算机牌点:\t";
        DisplayPip_C(cpu);
        cout<<"您的牌点数是:"<<GetPip(player)<<endl;
        if(blPlayer)
        {
            cout<<"\n\n您是否继续要牌(Y/N)\n";
            cin >>chChoice;
            if((chChoice == 'Y'||chChoice == 'y'))
            {
                if(GetNumber(player)<5)
                {
                    TurnPlay(cpu,player,true);
                    cout<<"这是您想要的牌:"<<GetCurrentCard(player)<<endl;
                    if(GetPip(player)>21)
                    {
                        blPlayer = 0;///爆牌
                    }
                }
                else
                {
                    cout<<"对不起,您已经要了5张牌了,不能在要牌了!";
                    blPlayer = 0;
                }
            }
        }
        if((chChoice =='N') ||(chChoice == 'n'))//按n退出
        {
            blPlayer = 0;
        }
        if(GetPip(cpu)<16 && GetNumber(cpu)<5)///对计算机要牌的限定
        {
            TurnPlay(cpu,player,false);
            cout<<"计算机要牌,牌点是:"<<GetCurrentCard(cpu)<<endl;
        }
        else
        {
            blCpu = 0;
        }
        if(blCpu&&GetNumber(player)<5&&GetPip(player)<21)
            blPlayer = 1;
 
    }
    while(blCpu||blPlayer);
    Judge(cpu,player);//判定胜负
}
 
void DisplayInfo(sCard sc)
{
    cout<<"您一共玩了"<<sc.nWin+sc.nLose+sc.nDraw<<"局,"<<"赢了"<<sc.nWin<<"局,"<<"输了"<<sc.nLose<<"局,"<<"平局"<<sc.nDraw<<"局"<<endl;
    cout<<"您的赌资共计有:$"<<sc.nDollar<<".\n";
}
void FirstPlayTwo(sCard &cpu, sCard &player)
{
    player.naPip[0] = gCard[num_card++];
    player.naPip[1] = gCard[num_card++];
    player.nNumber = 2;
    cpu.naPip[0] = gCard[num_card++];
    cpu.naPip[1] = gCard[num_card++];
    cpu.nNumber = 2;
}
 
void DisplayPip(sCard sc)
{
    int i;
    for(i = 0; i<sc.nNumber; i++)
        cout<<sc.naPip[i]<<'\t';
    cout<<endl;
}
void DisplayPip_C(sCard sc)
{
    int i;
    cout<<"[*]"<<'\t';
    for(i = 1; i<sc.nNumber; i++)
        cout<<sc.naPip[i]<<'\t';
    cout<<endl;
}
 
int GetPip(sCard &sc)
{
    int a = 0;
    int i;
    for(i = 0; i<sc.nNumber; i++)
    {
        if(sc.naPip[i]>=10)///10以上的控制在10
        {
            a +=10;
        }
        else
        {
            a+=sc.naPip[i];
        }
    }
    return a;
}
 
int GetNumber(sCard sc)//返回已经发的牌
{
    return sc.nNumber;
}
 
void TurnPlay(sCard &cpu, sCard &player, bool flag)
{
    if(flag==true)
    {
        player.nNumber++;
        player.naPip[player.nNumber-1] = gCard[num_card++];
    }
    else if(flag==false)
    {
        cpu.nNumber++;
        cpu.naPip[player.nNumber-1] = gCard[num_card++];
    }
}
 
int GetCurrentCard(sCard sc)//返回当前的牌点
{
    return sc.naPip[sc.nNumber-1];
}
 
void Judge(sCard &cpu,sCard &player)
{
    printf("\n");
    if((GetPip(cpu)>21&&GetPip(player)>21) || GetPip(cpu) == GetPip(player))///平局局面
    {
        printf("\n\n双方平局!\n");
        printf("计算机数据:\t");
        DisplayPip(cpu);
        cout<<"牌面点数:"<<GetPip(cpu)<<endl;
        cout<<"\n您的数据\t";
        Draw(player);
        cout<<endl;
    }
    else if((GetPip(cpu)>21)||(GetPip(player)>GetPip(cpu) && GetPip(player)<=21))///胜利
    {
        printf("\n\n恭喜您,您赢了!\n");
        cout<<"计算机数据:\t";
        DisplayPip(cpu);
        cout<<"牌面点数:"<<GetPip(cpu)<<endl;
        cout<<"\n您的数据\t";
        Win(player);
        cout<<endl;
 
    }
    else//失败
    {
        cout<<"\n\n很遗憾,您输了!\n";
        cout<<"计算机数据:\t";
        DisplayPip(cpu);
        cout<<"牌面点数:"<<GetPip(cpu)<<endl;
        cout<<"\n您的数据\t";
        Lose(player);
        cout<<endl;
 
    }
 
}
 
void Draw(sCard &sc)///平局局面
{
    sc.nDraw++;
    sc.nDollar+=sc.nGamble;
    DisplayPip(sc);
    if(GetPip(sc)>21)
    {
        cout<<"爆了! \n";
        cout<<"牌面点数:"<<GetPip(sc)<<endl;
    }
    else
    {
        cout<<"牌面点数:"<<GetPip(sc)<<endl;
    }
    cout<<"赌本:$"<<sc.nDollar<<" 赢了"<<sc.nWin<<"次"<<"输了"<<sc.nLose<<"次 "<<"平局 "<<sc.nDraw<<"次 "<<endl;
    cout<<endl<<endl;
}
 
void Win(sCard &sc)
{
    sc.nWin++;
    DisplayPip(sc);
    cout<<"牌面点数: "<<GetPip(sc)<<endl;
    sc.nDollar = sc.nDollar +2*sc.nGamble;
    cout<<"赌本:$"<<sc.nDollar<<" 赢了"<<sc.nWin<<"次"<<"输了"<<sc.nLose<<"次 "<<"平局"<<sc.nDraw<<"次"<<endl;
    cout <<endl<<endl;
 
}
 
void Lose(sCard &sc)
{
    sc.nLose ++;
    DisplayPip(sc);
    if(GetPip(sc)>21)
    {
        cout<<"爆了!\n";
        cout<<"牌面点数: "<<GetPip(sc)<<endl;
    }
    else
    {
        cout<<"牌面点数: "<<GetPip(sc)<<endl;
    }
    cout<<"赌本:$"<<sc.nDollar<<" 赢了"<<sc.nWin<<"次"<<"输了"<<sc.nLose<<"次 "<<"平局 "<<sc.nDraw<<"次 "<<endl;
    cout<<endl<<endl;
}

  

本文作者:王陸

本文链接:https://www.cnblogs.com/wkfvawl/p/9234856.html

版权声明:本作品采用知识共享署名-非商业性使用-禁止演绎 2.5 中国大陆许可协议进行许可。

posted @   王陸  阅读(209)  评论(0编辑  收藏  举报
点击右上角即可分享
微信分享提示
评论
收藏
关注
推荐
深色
回顶
收起