C# -- 模拟扑克牌发牌

C# -- 模拟扑克牌发牌

1.  User 类: 玩家

public class User
{
    private List<PaperCard> listCard = new List<PaperCard>();

    public string Name { get; set; }

    public User(string strName)
    {
        this.Name = strName;
    }
    public void Add(PaperCard p)
    {
        listCard.Add(p);
    }

    public void Reset()
    {
        listCard.Clear();
    }

    public void Introduce()
    {
        string strCards = "";

        for (int i = 0; i < listCard.Count; i++)
        {
            if (i == 0)
            {
                strCards += listCard[i].Name;
            }
            else
            {
                strCards += ", " + listCard[i].Name;
            }
        }
        Console.WriteLine("\r\n--------------------------------------------");
        Console.Write("我是{0},", Name);
        Console.Write("我手中的扑克牌有{0}张:\r\n{1}\r\n", listCard.Count,strCards);
    }
}
View Code

 

2. PaperCard类:纸牌

public class PaperCard
{
    public string Type { get; set; }
    public string Number { get; set; }
    public PaperCard(string strType, string strNumber)
    {
        this.Type = strType;
        this.Number = strNumber;
    }

    public string Name
    {
        get
        {
            return string.Format("{0}{1}", this.Type, this.Number);
        }

        private set { }
    }
}
View Code

 

3. 模拟发牌过程

public void TestPlay()
{
    //产生扑克牌
    Console.WriteLine("正在生成扑克牌...");
    List<PaperCard> myCards = new List<PaperCard>();
    string[] strType = { "红桃", "黑桃", "梅花", "方块" };
    string[] strNumber = { "A", "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K" };
    PaperCard[] OtherCard = { new PaperCard("大王", ""), new PaperCard("小王", "") };

    for (int i = 0; i < strNumber.Length; i++)
    {
        for (int j = 0; j < strType.Length; j++)
        {
            PaperCard p = new PaperCard(strType[j], strNumber[i]);
            myCards.Add(p);
        }
    }

    for (int j = 0; j < OtherCard.Length; j++)
    {
        myCards.Add(OtherCard[j]);
    }

    //洗牌
    Console.WriteLine("正在洗牌...");
    Stack<PaperCard> stackCard = new Stack<PaperCard>();
    Random r = new Random();

    while (myCards.Count>0)
    {
        int iIndex = r.Next(0, myCards.Count);
        stackCard.Push(myCards[iIndex]);
        myCards.RemoveAt(iIndex);
    }



    //发牌
    Console.WriteLine("开始发牌...");
    List<User> listUser = new List<User>() {
        new User("大师兄"),
        new User("二师兄"),
        new User("三师弟")
    };

    while (stackCard.Count > 0)
    {
        for (int s = 0; s < listUser.Count; s++)
        {
            listUser[s].Add(stackCard.Pop());
        }
    }
    Console.WriteLine("发牌完成!");


    //看牌
    foreach (var item in listUser)
    {
        item.Introduce();
    }
}

 

测试:

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值