[原]用C#模拟实现扑克牌发牌、排序程序…

(1)52张扑克牌,四种花色(红桃、黑桃、方块和梅花),随机发牌给四个人。

(2)最后将四个人的扑克牌包括花色打印在控制台上。

其中:

    花色和点数用枚举类型实现

    每张扑克牌用结构实现

**************************************************************************************************


 

[csharp] view plaincopy在CODE上查看代码片派生到我的代码片
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Collections;  
  4. using System.Linq;  
  5. using System.Text;  
  6. using System.Threading.Tasks;  
  7. //用C#模拟实现扑克牌发牌、排序程序。  
  8. //(1)52张扑克牌,四种花色(红桃、黑桃、方块和梅花),随机发牌给四个人。  
  9. //(2)最后将四个人的扑克牌包括花色打印在控制台上。  
  10. //其中:  
  11. //  花色和点数用枚举类型实现  
  12. //  每张扑克牌用结构实现  
  13. //提示:可以用ArrayList初始化52张扑克牌,然后从这个链表中随机取牌发给四个玩家,直到链表为空为止。  
  14.   
  15. namespace Test5  
  16.  
  17.     enum Color {HongTao=-1,HeiTao=-2,MeiHua=-3,FangPian=-4}//花色  
  18.     enum Point {A,two,three,four,five,six,seven,eight,nine,ten,J,Q,K}//点数  
  19.     struct Poker  
  20.     {//扑克  
  21.         private string p1,p2;  
  22.         public Poker(string p1, string p2)  
  23.          
  24.             // TODO: Complete member initialization  
  25.             this.p1 p1;  
  26.             this.p2 p2;  
  27.          
  28.         public void Printp()  
  29.          
  30.             Console.Write("({0},{1}) "this.p1,this.p2);  
  31.          
  32.      
  33.     class Program  
  34.      
  35.           
  36.         static void Main(string[] args)  
  37.          
  38.             Poker po new Poker();  
  39.             ArrayList myPoker=new ArrayList();//实例化一个ArrayList存放所有的扑克牌  
  40.             ArrayList Person1 new ArrayList();  
  41.             ArrayList Person2 new ArrayList();  
  42.             ArrayList Person3 new ArrayList();  
  43.             ArrayList Person4 new ArrayList();  
  44.             Random new Random();  
  45.             for (int -4; <= -1; i++)  
  46.             {//外循环初始化扑克的花色  
  47.                 for (int 0; <= 12; j++)  
  48.                 {//外层循环初始化扑克的点数  
  49.                     myPoker.Add(new Poker(  
  50.                         Enum.GetName(typeof(Color),i),  
  51.                         Enum.GetName(typeof(Point),j)  
  52.                         ));  
  53.                  
  54.             }//for循环结束,52张扑克牌已经生成  
  55.             Console.WriteLine("打印所有的扑克牌:");  
  56.             for (int 0; 52; i++)  
  57.             {//打印52张生成的扑克牌  
  58.                 Poker poAll (Poker)myPoker[i];  
  59.                 poAll.Printp();  
  60.              
  61.             //开始发牌,一个人一个人的发,每发一张牌得从myPoker中RemoveAt掉扑克牌,Count数减小;第一个人Add到一张牌。  
  62.             for (int 0; 13; i++)  
  63.              
  64.                 int te r.Next(0,myPoker.Count);  
  65.                 Person1.Add(myPoker[te]);  
  66.                 myPoker.RemoveAt(te);  
  67.              
  68.             for (int 0; 13; i++)  
  69.              
  70.                 int te r.Next(0, myPoker.Count);  
  71.                 Person2.Add(myPoker[te]);  
  72.                 myPoker.RemoveAt(te);  
  73.              
  74.             for (int 0; 13; i++)  
  75.              
  76.                 int te r.Next(0, myPoker.Count);  
  77.                 Person3.Add(myPoker[te]);  
  78.                 myPoker.RemoveAt(te);  
  79.              
  80.             for (int 0; 13; i++)  
  81.              
  82.                 int te r.Next(0, myPoker.Count);  
  83.                 Person4.Add(myPoker[te]);  
  84.                 myPoker.RemoveAt(te);  
  85.              
  86.               
  87.             Console.WriteLine();  
  88.             Console.WriteLine("打印第一个人的扑克牌:");  
  89.             for (int 0; 13;i++  
  90.              
  91.                 Poker po1 (Poker)Person1[i];  
  92.                 po1.Printp();  
  93.              
  94.             Console.WriteLine();  
  95.             Console.WriteLine("打印第二个人的扑克牌:");  
  96.             for (int 0; 13; i++)  
  97.              
  98.                 Poker po2 (Poker)Person2[i];  
  99.                 po2.Printp();  
  100.              
  101.             Console.WriteLine();  
  102.             Console.WriteLine("打印第三个人的扑克牌:");  
  103.             for (int 0; 13; i++)  
  104.              
  105.                 Poker po3 (Poker)Person3[i];  
  106.                 po3.Printp();  
  107.              
  108.             Console.WriteLine();  
  109.             Console.WriteLine("打印第四个人的扑克牌:");  
  110.             for (int 0; 13; i++)  
  111.              
  112.                 Poker po4 (Poker)Person4[i];  
  113.                 po4.Printp();  
  114.              
  115.             Console.WriteLine();  
  116.          
  117.      
  118. }  
作者:gcy77 发表于2014-3-12 15:16:24 原文链接
阅读:86 评论:0 查看评论
原文地址:https://www.cnblogs.com/gcy77/p/4082494.html