服务器之家:专注于VPS、云服务器配置技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|VB|R语言|JavaScript|易语言|vb.net|

服务器之家 - 编程语言 - C# - C#编写控制台程序纸牌游戏

C#编写控制台程序纸牌游戏

2022-08-03 15:26qq_24111631 C#

这篇文章主要为大家详细介绍了C#编写控制台程序纸牌游戏,适合C#初学者,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

本文实例为大家分享了C#编写纸牌游戏的具体代码,供大家参考,具体内容如下

?
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
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;
 
namespace ConsoleApplication2
{
 class Game
 {
  string[] str = new string[52];
  int[] number = new int[52];
  int i = 0;
  int[] a = new int[4];
  int[] b = new int[4];
  int[] c = new int[4];
  int[] d = new int[4];
  int[] e = new int[4];
  Random ra = new Random(unchecked((int)DateTime.Now.Ticks));
  public void getnum(int[] number, int minnum, int maxnum)
  {
 
   int flag;
 
   flag = number[i] = ra.Next(0, 52);
   int f = 0;
 
   while (f < i)
   {
    if (number[f] == flag)
     getnum(number, 0, 52);
    f++;
  }
 
 
  }
 
  public void GetRandomNum()//给number随机赋从0到51不同的值
  {
   while (i < 52)
   {
    int flag;
 
    flag = number[i] = ra.Next(0, 52);
    int f = 0;
    while (f < i)
    {
     if (number[f] == flag)
     {
      getnum(number, 0, 52);
      break;
     }
     f++;
    }
    i++;
   }
  }
 
  public void EnterCard()//给每个字符串数组赋上牌值
  {
   str[0] = "1 of the club";
   str[1] = "2 of the club";
   str[2] = "3 of the club";
   str[3] = "4 of the club";
   str[4] = "5 of the club";
   str[5] = "6 of the club";
   str[6] = "7 of the club";
   str[7] = "8 of the club";
   str[8] = "9 of the club";
   str[9] = "10 of the club";
   str[10] = "Jack of the club";
   str[11] = "queen of the club";
   str[12] = "king of the club";
 
   str[13] = "1 of the diamond";
   str[14] = "2 of the diamond";
   str[15] = "3 of the diamond";
   str[16] = "4 of the diamond";
   str[17] = "5 of the diamond";
   str[18] = "6 of the diamond";
   str[19] = "7 of the diamond";
   str[20] = "8 of the diamond";
   str[21] = "9 of the diamond";
   str[22] = "10 of the diamond";
   str[23] = "Jack of the diamond";
   str[24] = "queen of the diamond";
   str[25] = "king of the diamond";
 
 
   str[26] = "1 of the heart";
   str[27] = "2 of the heart";
   str[28] = "3 of the heart";
   str[29] = "4 of the heart";
   str[30] = "5 of the heart";
   str[31] = "6 of the heart";
   str[32] = "7 of the heart";
   str[33] = "8 of the heart";
   str[34] = "9 of the heart";
   str[35] = "10 of the heart";
   str[36] = "Jack of the heart";
   str[37] = "queen of the heart";
   str[38] = "king of the heart";
 
   str[39] = "1 of the spade";
   str[40] = "2 of the spade";
   str[41] = "3 of the spade";
   str[42] = "4 of the spade";
   str[43] = "5 of the spade";
   str[44] = "6 of the spade";
   str[45] = "7 of the spade";
   str[46] = "8 of the spade";
   str[47] = "9 of the spade";
   str[48] = "10 of the spade";
   str[49] = "Jack of the spade";
   str[50] = "queen of the spade";
   str[51] = "king of the spade";
 
 
  }
 
  public void FaPai(int PersonNum)//发牌
  {
   for (int j = 0; j < PersonNum; j++)
   {
 
    a[j] = number[j * 5];
    b[j] = number[j * 5 + 1];
    c[j] = number[j * 5 + 2];
    d[j] = number[j * 5 + 3];
    e[j] = number[j * 5 + 4];
   }
  }
 
  public void Print(string[] PersonName, int j)//显示玩家的牌
  {
   Console.WriteLine("玩家{0}的牌", PersonName[j]);
   Console.WriteLine("1. {0}", str[a[j]]);
   Console.WriteLine("2. {0}", str[b[j]]);
   Console.WriteLine("3. {0}", str[c[j]]);
   Console.WriteLine("4. {0}", str[d[j]]);
   Console.WriteLine("5. {0}", str[e[j]]);
  }
 
  public void Print1(string[] PersonName, int j)//显示玩家的牌
  {
   Console.WriteLine("玩家{0}剩下的牌", PersonName[j]);
   Console.WriteLine("1. {0}", str[a[j]]);
   Console.WriteLine("2. {0}", str[b[j]]);
   Console.WriteLine("3. {0}", str[c[j]]);
   Console.WriteLine("4. {0}", str[d[j]]);
   Console.WriteLine("5. {0}", str[e[j]]);
   Console.WriteLine("按ENTER建进入下一个玩家");
   Console.ReadLine();
  }
 
public bool CompareAns(int j, string[] PersonName, int n, bool bool2)//对玩家的牌与胜利的结果进行比较
  {
   int min = 0, max = 0;
   max = (a[j] > b[j]) ? a[j] : b[j];
   max = (c[j] > max) ? c[j] : max;
   max = (d[j] > max) ? d[j] : max;
   max = (e[j] > max) ? e[j] : max;
   min = (a[j] < b[j]) ? a[j] : b[j];
   min = (c[j] < min) ? c[j] : min;
   min = (d[j] < min) ? d[j] : min;
   min = (e[j] < min) ? e[j] : min;
 
   if (min >= 0 && max < 13 || min >= 13 && max < 26 || min >= 26 && max < 39 || min >= 39 && max < 52)
   {
    Console.Clear();
    Console.WriteLine("恭喜玩家{0}赢得游戏!", PersonName[j]);
    bool2 = false;
   }
   
   //判断是否还有底牌可供玩家拿取
   else if (n==52)
   {
    Console.Clear();
    Console.WriteLine("没有底牌了!!重新开始。");
    bool2 = false;
   }
   return bool2;
  }
 
  public void TakeCard(string[] PersonName, int PersonNum)//玩家打牌
  {
   GetRandomNum();
   int n = 1, botton=7;
 
   int f = 0;
   int up = 0, up1 = 0;
   bool bool2 = true;
   bool bool1 = false;
   // 发牌
   FaPai(PersonNum);
   //玩家打牌
   n = 5 * PersonNum;
 
   while (bool2)
   {
    for (int j = 0; j < PersonNum; j++)
    {
     //显示玩家的牌
     Console.Clear();
     if (bool1)
     {
      Console.Write("上一玩家打出的牌是");
      Console.WriteLine("{0}", str[up]);
     }
     bool1 = true;
     Print(PersonName, j);
 
 //玩家从剩下的牌中拿牌或拿上一玩家扔出来的牌
 
     char D='f';
     if (n == 5 * PersonNum)
     {
      try
      {
       Console.WriteLine("按D从剩下的牌中拿一张牌");
       D = Convert.ToChar(Console.ReadLine());
      }
      catch (Exception e)
      {
       Console.WriteLine(e.Message);
       Thread.Sleep(1000);
       System.Environment.Exit(0);
      }
      
      if (D == 'D' || D == 'd')
      {
 
       f = number[n];
       n++;
       Print(PersonName, j);
       Console.WriteLine("6. {0}", str[f]);
 
 
      }
 
 
     }
     else
     {
      Console.WriteLine("按D从剩下的牌中拿一张牌或者按T拿被上一玩家打的牌");
      D = Convert.ToChar(Console.ReadLine());
      if (D == 'D' || D == 'd')
      {
 
       f = number[n];
       n++;
       Print(PersonName, j);
       Console.WriteLine("6. {0}", str[f]);
 
 
      }
      else if (D == 't' || D == 'T')
      {
       Print(PersonName, j);
       Console.WriteLine("6. {0}", str[up]);
 
      }
 
     }
 
     //打牌
     try
     {
      Console.WriteLine("输入你想要打出牌的序号:");
      botton = Convert.ToInt32(Console.ReadLine());
     }
     catch (Exception e)
     {
      Console.WriteLine(e.Message);
      Thread.Sleep(1000);
      System.Environment.Exit(0);
     }
     switch (botton)
     {
    case 1:
       {
        Console.WriteLine("被打出的牌是{0}", str[a[j]]);
        up1 = up; up = a[j]; a[j] = b[j]; b[j] = c[j]; c[j] = d[j]; d[j] = e[j];
        if (D == 'd' || D == 'D')
         e[j] = f;
        else if (D == 't' || D == 'T')
         e[j] = up1;
        Print1(PersonName, j);
 
        break;
       }
 
    case 2:
       {
        Console.WriteLine("被打出的牌是{0}", str[b[j]]);
        up1 = up; up = b[j]; b[j] = c[j]; c[j] = d[j]; d[j] = e[j];
        if (D == 'd' || D == 'D')
         e[j] = f;
        else if (D == 't' || D == 'T')
         e[j] = up1;
        Print1(PersonName, j);
 
        break;
       }
    
    case 3:
       {
        Console.WriteLine("被打出的牌是{0}", str[c[j]]);
        up1 = up; up = c[j]; c[j] = d[j]; d[j] = e[j];
        if (D == 'd' || D == 'D')
         e[j] = f;
        else if (D == 't' || D == 'T')
         e[j] = up1;
        Print1(PersonName, j);
 
        break;
 
       }
 
    case 4:
       {
        Console.WriteLine("被打出的牌是{0}", str[d[j]]);
        up1 = up; up = d[j]; d[j] = e[j];
        if (D == 'd' || D == 'D')
         e[j] = f;
        else if (D == 't' || D == 'T')
 
         e[j] = up1;
        Print1(PersonName, j);
 
        break;
       }
    case 5:
       {
        Console.WriteLine("被打出的牌是{0}", str[e[j]]);
        up1 = up; up = e[j];
        if (D == 'd' || D == 'D')
         e[j] = f;
        else if (D == 't' || D == 'T')
         e[j] = up1;
        Print1(PersonName, j);
 
        break;
 
       }
 
    case 6:
       {
        Console.WriteLine("被打出的牌是{0}", str[f]);
        up = f;
        Print1(PersonName, j);
        break;
       }
      default:
       Console.WriteLine("输入错误"); break;
     }
     //判断结果
     bool2 = CompareAns(j,PersonName,n,bool2);
     if (bool2 == false) break;
 
    }
   }
  }
 
 
 }
 
 class ExeMain
 {
    static void Main(string[] args)
  {
 
   int PersonNum=0;
   Game obj = new Game();
   Console.WriteLine("第一个输入的名字将作为第一个开始拿牌的人:");
   Console.WriteLine("游戏的人数:");
   try
   {
    PersonNum = Convert.ToInt32(Console.ReadLine());
   }
   catch (Exception e)
   {
    Console.WriteLine(e.Message);
    Thread.Sleep(1000);
    System.Environment.Exit(0);
   }
   string[] PersonName = new string[PersonNum];
   for (int i = 0; i < PersonNum; i++)
   {
    Console.WriteLine("输入玩家{0}的名字", i + 1);
    PersonName[i] = Console.ReadLine();
   }
   obj.EnterCard();//给每个字符串数组赋值
 
   obj.TakeCard(PersonName, PersonNum);//给每个玩家发牌
   Console.ReadLine();
 
 
  }
 }

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。

延伸 · 阅读

精彩推荐
  • C#C#实现分页组件的方法

    C#实现分页组件的方法

    这篇文章主要为大家详细介绍了C#实现分页组件的方法,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...

    风雪幻林6422022-01-20
  • C#C#实现多选项卡的浏览器控件

    C#实现多选项卡的浏览器控件

    这篇文章主要为大家详细介绍了C#实现多选项卡的浏览器控件的相关资料,感兴趣的小伙伴们可以参考一下...

    C#教程网8092021-11-15
  • C#C#使用LINQ查询表达式的基本子句总结

    C#使用LINQ查询表达式的基本子句总结

    这篇文章主要介绍了C#使用LINQ查询表达式的基本子句总结,在C#程序中我们可以使用LINQ基本查询表达式模式来查询和转换SQL数据库、ADO.NET数据集、XML文档和...

    DebugLZQ7442021-11-15
  • C#详解C#中三个关键字params,Ref,out

    详解C#中三个关键字params,Ref,out

    本文主要讨论params关键字,ref关键字,out关键字。非常不错,具有参考借鉴价值,需要的朋友参考下吧...

    RynerLute8982022-01-06
  • C#C# FileStream实现大文件复制

    C# FileStream实现大文件复制

    这篇文章主要为大家详细介绍了C# FileStream实现大文件复制,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...

    kone6667902022-07-21
  • C#详解C# 网络编程系列:实现类似QQ的即时通信程序

    详解C# 网络编程系列:实现类似QQ的即时通信程序

    本篇主要介绍了c#实现类似QQ的即时通信程序 ,具有一定的参考价值,感兴趣的小伙伴们可以参考一下。...

    Learning hard3732021-12-16
  • C#C#调用python文件执行

    C#调用python文件执行

    这篇文章主要为大家详细介绍了C#调用python文件执行的相关方法,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...

    tuohaibei8852022-01-04
  • C#微信开放平台之网站授权微信登录功能

    微信开放平台之网站授权微信登录功能

    本文通过.net实现的微信开放平台之网站授权微信登录功能,需要的小伙伴一起看看吧...

    小 鹏11672021-10-27