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


    解决方案目录:

    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

     

     

      

  • 相关阅读:
    为蓝桥杯准备
    Java里子类调用父类构造方法问题
    boost库的Singleton的实现以及static成员的初始化问题
    VS2005调试小记
    <转载>程序员每天该做的事
    vs2005, 2008 使用了未初始化的msg变量
    转载 vs2005 快捷键大全
    VS2005右键点击转到定义后出现“未定义符号”的提示及其解决
    软件工程配置规范(VC2005) 第二版(转载)
    匆忙赶路的时候别忘了适时停下来回头看看
  • 原文地址:https://www.cnblogs.com/fanhongshuo/p/4728162.html
Copyright © 2020-2023  润新知