这个是《C#入门经典》第六版 中的例子,但其中没有包含大小王。我略作改动把54张扑克牌都补全。
包含一个类库和一个命令行程序。
如果要去掉大小王,
枚举类可以不变动(也可以Suit中删除“王”,同时Rank中删除“大王”、“小王”)
Card中,重写的ToString方法中,直接返回return,不做任何判断。
Deck中,删除cards数组的最后2个元素赋值,同时修改数组大小及判断数组大小的地方-2
PS:客户端只包含洗牌,未包含多人发牌,可以自己添加。
类库的类图如下:
枚举类Suit:
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace Ch10CardLib { //public enum Suit //{ // Club, // Diamond, // Heart, // Spade, //} /// <summary> /// 牌的花色 包含4种花色 /// </summary> public enum Suit { 梅花, 方片, 红桃, 黑桃, 王, } }
枚举Rank:
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace Ch10CardLib { //public enum Rank //{ // Ace = 1, // Deuce, // Three, // Four, // Five, // Six, // Seven, // Eight, // Nine, // Ten, // Jack, // Queen, // King, //} /// <summary> /// 牌的大小 范围:A、2、3 ...10、J、Q、K /// </summary> public enum Rank { A = 1, 二, 三, 四, 五, 六, 七, 八, 九, 十, J, Q, K, 小王, 大王, } }
Card容器类:
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace Ch10CardLib { public class Card { public readonly Rank rank; public readonly Suit suit; private Card() { } public Card(Suit newSuit, Rank newRank) { suit = newSuit; rank = newRank; } public override string ToString() { // return "The " + rank + " of " + suit + " s"; if (suit==(Suit)4) { return string.Format("{0}", rank); } else { return string.Format("{0}{1}", suit, rank); } } } }
Deck类:
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace Ch10CardLib { public class Deck { private Card[] cards; /// <summary> /// 生成一副有序的牌 /// </summary> public Deck() { cards = new Card[54]; for (int suitVal = 0; suitVal < 4; suitVal++) //四种花色 { for (int rankVal = 1; rankVal < 14; rankVal++) //每个花色各13张牌 { cards[suitVal * 13 + rankVal - 1] = new Card((Suit)suitVal,(Rank)rankVal); } } cards[52] = new Card((Suit)4, (Rank)14); cards[53] = new Card((Suit)4, (Rank)15); } /// <summary> /// 获取牌号 /// </summary> /// <param name="cardNum">牌号</param> /// <returns></returns> public Card GetCard(int cardNum) { if (cardNum >= 0 && cardNum <= 53) { return cards[cardNum]; } else { throw (new System.ArgumentOutOfRangeException("cardNum", cardNum, "值必须在0~51之间。")); } } /// <summary> /// 洗牌 /// </summary> public void Shuffle() { Card[] newDeck = new Card[54]; bool[] assigned = new bool[54]; Random sourceGen = new Random(); for (int i = 0; i < 54; i++) { int destCard = 0; bool foundCard = false; while (foundCard == false) { destCard = sourceGen.Next(54); if (assigned[destCard] == false) { foundCard = true; } } assigned[destCard] = true; newDeck[destCard] = cards[i]; } newDeck.CopyTo(cards,0); //将打乱顺序的一副牌组newDeck从0开始复制给cards } } }
测试客户端:
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using Ch10CardLib; namespace Ch10CardClient { class Program { static void Main(string[] args) { Deck myDeck = new Deck(); //调用Deck类中默认构造函数,生成一副有序的扑克牌 myDeck.Shuffle(); for (int i = 0; i < 54; i++) { Card tempCard = myDeck.GetCard(i); Console.WriteLine(tempCard.ToString()); if (i != 54) { } else { Console.WriteLine("发牌结束!"); } } Console.ReadKey(); } } }