C#:扑克牌游戏(1):规划CardLib类库开发扑克牌游戏

(一):前言

        这是《C#入门经典(第七版)》的学习笔记,接下来会通过一个扑克牌程序学习关于C#相关知识。

(二):前期思考过程

        创建一个类库,命名应该为CardLib,不过按照书上的安排,该类库是在第十章第一次使用,在此命名为Ch10CardLib。

        容易想到,我们平时玩扑克牌的时候,总是会说,一副两副,因此我们考虑创建一个类Deck代表“一副牌”。

        如果不考虑大小王,一副牌中共有52张牌,因此我们创建一个类Card代表”一张牌”。

        每张牌有”花色”和“数字”,显然花色和数字都可以使用枚举来表示,因此创建一个代表花色的枚举Suit,一个代表数字的枚举Rank。

        Card类代表着“一张牌”,而一张牌又有着花色和数字,花色和数字是牌的特性,因此我们可以在Card类中添加字段rank和suit,这样就建立起了Card类和枚举Suit,枚举Rank之间的关系。

        一副牌里有52张牌,我们可以在Deck类中添加Card数组类型的字段cards,这样就建立了Deck类和Card类之间的关系。

(三):初步实现

        1:在枚举Suit中输入代码

public enum Suit
    {
        Club,
        Diamond,
        Heart,
        Spade
    }

        2:在枚举Rank中输入代码

 public enum Rank
    {
        Ace = 1,
        Deuce,
        Three,
        Four,
        Five,
        Six,
        Seven,
        Eight,
        Nine,
        Ten,
        Jack,
        Queen,
        King
    }

        3:在Card类中添加字段rank和suit

 public Suit suit;
 public Rank rank;

        4:在Deck类中添加Card数组类型的字段cards

 private Card[] cards;

(四):对于具体功能的进一步思考

        1:Card类的构造函数

        这里使用了新的构造函数

public Card(Suit newSuit, Rank newRank)
        {
            suit = newSuit;
            rank = newRank;
            //throw new System.NotImplementedException();
        }

        2:Card类的ToString()函数

        当我们希望得到一个Card对象的消息时,我们希望知道它的花色和数字,因此我们重写ToString()函数

public override string ToString()
        {
            return "The " + rank + " of " + suit + "s";
            
        }

        3:Deck类的创建函数

        一副牌内有52张牌,因此我们需要创建51个Card对象,在这里,我们暂且使用一个数组来存储这51个对象

 public Deck()
        {
            cards = new Card[52];
            for(int suitVal=0;suitVal<4;suitVal++)
            {
                for (int rankVal = 1; rankVal < 14; rankVal++)
                    cards[suitVal * 13 + rankVal - 1] = new Card((Suit)suitVal, (Rank)rankVal);
            }            
        }

        4:Deck类的洗牌函数

        作为一个纸牌类游戏,当然要有洗牌的功能了。

        /// <summary>
        /// 洗牌
        /// </summary>
        public void Shuffle()
        {
            Card[] newDeck = new Card[52];
            bool[] assigned = new bool[52];
            Random sourceGen = new Random();
            for(int i=0;i<52;i++)
            {
                int destCard = 0;
                bool foundCard = false;
                while(foundCard==false)
                {
                    destCard = sourceGen.Next(52);
                    if(assigned[destCard]==false)
                        foundCard = true;
                }
                assigned[destCard] = true;
                newDeck[destCard] = cards[i];
            }
            newDeck.CopyTo(cards, 0);            
        }

(五):验证

        上面写出来的是一个类库,需要使用一个客户程序来验证是否正确,因此使用一个新的客户程序

        首先在资源管理器试图下右击解决方案,选择新建项目,创建一个新的控制台程序命名为Ch10CardClient。创建完成后右击项目名称,选择“添加-引用”然后选择Ch10CardLib后点确定。再然后右击项目名称,选择“设为启动项目”,在主函数中输入如下代码。

static void Main(string[] args)
        {
            Deck myDeck = new Deck();
            myDeck.Shuffle();
            for(int i=0;i<52;i++)
            {
                Card tempCard = myDeck.GetCard(i);
                Write(tempCard.ToString());
                if (i != 51)
                    Write(",");
                else
                    WriteLine();

            }
            ReadKey();
        }

(六):效果

        

(七):源代码

        Ch10CardLib类库中

        Suit.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Ch10CardLib
{
    public enum Suit
    {
        Club,
        Diamond,
        Heart,
        Spade
    }
}

        Rank.cs

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
    }
}

        Card.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Ch10CardLib
{
    public class Card
    {
        public Suit suit;
        public Rank rank;

        private Card()
        {
            //throw new System.NotImplementedException();
        }

        public Card(Suit newSuit, Rank newRank)
        {
            suit = newSuit;
            rank = newRank;
            //throw new System.NotImplementedException();
        }

        public override string ToString()
        {
            return "The " + rank + " of " + suit + "s";
            //throw new System.NotImplementedException();
        }
    }
}

        Deck.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Ch10CardLib
{
    public class Deck
    {
        private Card[] cards;

        public Deck()
        {
            cards = new Card[52];
            for(int suitVal=0;suitVal<4;suitVal++)
            {
                for (int rankVal = 1; rankVal < 14; rankVal++)
                    cards[suitVal * 13 + rankVal - 1] = new Card((Suit)suitVal, (Rank)rankVal);
            }            
        }

        public Card GetCard(int cardNum)
        {
            if (cardNum >= 0 && cardNum <= 51)
                return cards[cardNum];
            else
                throw (new System.ArgumentOutOfRangeException("cardNum", cardNum,
                    "Value must be between 0 and 51"));
            //throw new System.NotImplementedException();
        }

        /// <summary>
        /// 洗牌
        /// </summary>
        public void Shuffle()
        {
            Card[] newDeck = new Card[52];
            bool[] assigned = new bool[52];
            Random sourceGen = new Random();
            for(int i=0;i<52;i++)
            {
                int destCard = 0;
                bool foundCard = false;
                while(foundCard==false)
                {
                    destCard = sourceGen.Next(52);
                    if(assigned[destCard]==false)
                        foundCard = true;
                }
                assigned[destCard] = true;
                newDeck[destCard] = cards[i];
            }
            newDeck.CopyTo(cards, 0);            
        }
    }
}

        Ch10CardClient项目中

        Program.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Ch11CardLib;
using static System.Console;

namespace Ch10CardClient
{
    class Program
    {
        static void Main(string[] args)
        {
            Deck myDeck = new Deck();
            myDeck.Shuffle();
            for(int i=0;i<52;i++)
            {
                Card tempCard = myDeck.GetCard(i);
                Write(tempCard.ToString());
                if (i != 51)
                    Write(",");
                else
                    WriteLine();

            }
            ReadKey();
        }
    }
}

猜你喜欢

转载自blog.csdn.net/buaazyp/article/details/82965408