一,创建1个 4X13的2维数组(牌), 4个 一维数,长度13(用户得到的牌)
二,给二维数组赋值,要随机数且不能重复(洗牌)
三,将二维数组的值分别赋与4个一维数(发牌)
[此贴子已经被作者于2005-4-8 11:41:07编辑过]
using System;
namespace ConsoleApplication12 { /// <summary> /// Class1 的摘要说明。 /// </summary> class card { enum huase { 红桃,黑桃,方块,草花 }; //创建牌,创建一个4*13的空矩阵用来以后存储52张牌 //这里用矩阵,而不直接用一维数组,目的是为了初始化方便 string[,] card52 = new string[4,13]; string[] tempC = new string[52]; string[] xiguohou = new string[52]; //创建4个玩家 string[] p1 = new string[13]; string[] p2 = new string[13]; string[] p3 = new string[13]; string[] p4 = new string[13]; //初始化牌,将52张牌依次存入矩阵中 //为了后面随机数只取1个,建立一个临时的长度为52的一维数组 //将矩阵中牌复制到一维数组tempC中 public void creatcard() { for(int i=0;i<=3;i++) { for(int j=0;j<=12;j++) { card52[i,j]=Convert.ToString((huase)i)+Convert.ToString(j+1); int n =i*13+j; tempC[n] = card52[i,j]; //Console.WriteLine("{0}--{1}",n,tempC[n]); } } } //洗牌,利用随机数,将52张牌的顺序随机排列 //重新排列后的牌放在一维数组xiguohou中 public void xipai() { int m=0; int[] y =new int[52]; y[0]=100; while (m<52) { Random rdm = new Random(); int x =rdm.Next(0,52); int z=0; for(int j=0;j<m;j++) { if (y[j] == x) { z++; } } if (z==0) { y[m] = x; xiguohou[m] = tempC[x]; //Console.WriteLine("{0}--{1}",m,xiguohou[m]); m++; } } } //轮流发牌并且显示每个玩家手中的牌 //把一维数组xiguohou分别发给p1,p2,p3,p4 public void fapai() { for (int i=0;i<=12;i++) { p1[i] = xiguohou[i]; p2[i]= xiguohou[i+13]; p3[i] = xiguohou[i+26]; p4[i]= xiguohou[i+39]; Console.WriteLine("p1--{0}--{1}",i+1,p1[i]); Console.WriteLine("p2--{0}--{1}",i+1,p2[i]); Console.WriteLine("p3--{0}--{1}",i+1,p3[i]); Console.WriteLine("p4--{0}--{1}",i+1,p4[i]); }
} [STAThread] static void Main(string[] args) { //调用建立,洗牌,发牌 3个方法 card card1 = new card(); card1.creatcard(); card1.xipai(); card1.fapai(); Console.ReadLine(); } } }
下面是另一种方法,大家交流一下啊!以后,各位多多关照小弟啊! using System;
namespace Test { /// <summary> /// Cards 的摘要说明。 /// </summary> file://枚举扑克牌的四种花色 public enum CardStyle { RedTriangle, file://红桃 RedDiamonds, file://方板 BlackTriangle, file://黑桃 BlackClub file://梅花 }
public class Cards { public CardStyle Style; public int Number; public Cards(CardStyle style,int number) { // // TOD 在此处添加构造函数逻辑 //
this.Style = style; this.Number = number; } public override string ToString() { string str = ""; switch (this.Style) { case CardStyle.RedTriangle: str = "红桃"; break; case CardStyle.RedDiamonds: str = "方板"; break; case CardStyle.BlackTriangle: str = "黑桃"; break; case CardStyle.BlackClub: str = "梅花"; break; } return str + this.Number.ToString(); } }
public class Playing { public Cards[] card = new Cards[52]; public Playing() { file://定义52张扑克牌 for (int i = 0; i <= 12;i++) { this.card[i] = new Cards(CardStyle.RedTriangle,i + 1); this.card[i + 13] = new Cards(CardStyle.RedDiamonds,i + 1); this.card[i + 26] = new Cards(CardStyle.BlackTriangle,i + 1); this.card[i + 39] = new Cards(CardStyle.BlackClub,i + 1); this.card[i + 39] = new Cards(CardStyle.BlackClub,i + 1); } }
file://洗牌 public Cards[] Shuffle() { Cards temp; Random rand = new Random(); int newIndex = 0; int randIndex; for (int i = 0;i < 52;i++) file://打乱52张牌。 { randIndex = rand.Next(newIndex,52); temp = this.card[randIndex]; this.card[randIndex] = this.card[newIndex]; this.card[newIndex++] = temp; } return this.card;
} } }