下面是另一种方法,大家交流一下啊!以后,各位多多关照小弟啊! 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;
} } }