代码改变世界

第十章 数组与集合 发牌程序 实例代码

2015-08-13 18:40  fanhongshuo  阅读(352)  评论(0编辑  收藏  举报

解决方案目录:

 

Suit.cs四种花色

namespace Cards
{
    enum Suit { Clubs, Diamonds, Hearts, Spades }
}

Value.cs扑克牌的13中面值

namespace Cards
{
    enum Value { Two, Three, Four, Five, Six, Seven, Eight, Nine, Ten, Jack, Queen, King, Ace }
}

Pack.cs发牌程序核心类

namespace Cards
{
	using System;
	using System.Collections;

	class Pack
	{
        public const int NumSuits = 4;//花色
        public const int CardsPerSuit = 13;//点数
        private Hashtable cardPack;//存储扑克牌对象的数组
        //private PlayingCard[,] cardPack;
        private Random randomCardSelector = new Random();//用来生成随机数,目的是随机发牌

        /// <summary>
        /// 构造方法初始化扑克牌数组
        /// </summary>
		public Pack()
		{
            cardPack = new Hashtable();
            //生成扑克牌数组的实例
            //this.cardPack = new PlayingCard[NumSuits, CardsPerSuit];
            //将扑克牌数组中的每个元素都实例化,指向一个扑克牌的对象
            for (Suit suit = Suit.Clubs; suit <= Suit.Spades; suit++)
            {
                SortedList cardsInSuit = new SortedList();
                //SortedList()这个类的作用是?
                for (Value value = Value.Two; value <= Value.Ace; value++)
                {
                    cardsInSuit.Add(value, new PlayingCard(suit, value)); 
                    //this.cardPack[(int)suit, (int)value] = new PlayingCard(suit, value);
                }
                this.cardPack.Add(suit, cardsInSuit);
            }
            //双重for循环对每一个元素进行赋值。
		}
        /// <summary>
        /// 发牌程序
        /// </summary>
        /// <returns></returns>
        public PlayingCard DealCardFromPack()
        {
            Suit suit = (Suit)randomCardSelector.Next(NumSuits);
            //返回一个小于NumSuits的非负随机数。因为NumSuits为4,所以小于4.
            //随机产生四种花色中的一种花色
            //循环判断该花色是否为空
            while (this.IsSuitEmpty(suit))
            {
                //随机产生四种花色中的一种花色
                suit = (Suit)randomCardSelector.Next(NumSuits);
                
            }
            //随机产生一个牌面值
            Value value = (Value)randomCardSelector.Next(CardsPerSuit);
            //随机产生一张扑克牌,这张牌在数组里面的值是不为空的。
            while (this.IsCardAlreadyDealt(suit, value))
            {
                value = (Value)randomCardSelector.Next(CardsPerSuit);
            }

            SortedList cardsInSuit = (SortedList)cardPack[suit];
            PlayingCard card = (PlayingCard)cardsInSuit[value];
            cardsInSuit.Remove(value);
            return card; 
        }
        /// <summary>
        /// 判断随机产生的牌是否已经存在
        /// </summary>
        /// <param name="suit"></param>
        /// <returns></returns>
        private bool IsSuitEmpty(Suit suit)
        {
            bool result = true;
            //判断具有suit花色的每张牌(2-A),如果有一章牌不是null的result值为false,否则为true;
            for (Value value = Value.Two; value <= Value.Ace; value++)
            {
                if (!IsCardAlreadyDealt(suit, value))
                {
                    result = false;
                    break;
                }
            }

            return result;
        }
        /// <summary>
        /// 判断扑克牌数组中的具有suit花色,value面值的这张牌的元素是否为null
        /// </summary>
        /// <param name="suit"></param>
        /// <param name="value"></param>
        /// <returns></returns>
        private bool IsCardAlreadyDealt(Suit suit, Value value)
        {
            SortedList cardsInSuit = (SortedList)this.cardPack[suit];
            return (!cardsInSuit.ContainsKey(value)); 
        }
	}
}

PalyCard.cs

 1 namespace Cards
 2 {
 3     class PlayingCard
 4     {
 5         private readonly Suit suit;
 6         private readonly Value value;
 7 
 8         public PlayingCard(Suit s, Value v)
 9         {
10             this.suit = s;
11             this.value = v;
12         }
13 
14         public override string ToString()
15         {
16             string result = string.Format("{0} of {1}", this.value, this.suit);
17             return result;
18         }
19 
20         public Suit CardSuit()
21         {
22             return this.suit;
23         }
24 
25         public Value CardValue()
26         {
27             return this.value;
28         }
29     }
30 }
View Code