扑克牌发牌操作(两个月前做的)

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


namespace playcards
{
    enum CardType
    {
        红桃, 金花, 方块, 黑桃
    }
    public class Cards
    {
        /// <summary>
        /// 卡牌的花色
        /// </summary>
        public string c_Type;
        /// <summary>
        /// 牌的大小
        /// </summary>
        public int c_Name;


        /// <summary>
        /// 空构造
        /// </summary>
        public Cards() { }


        /// <summary>
        /// 非空构造
        /// </summary>
        /// <param name="c_Type">花色</param>
        /// <param name="c_Name">牌的大小</param>
        public Cards(string c_Type, int c_Name)
        {
            this.c_Name = c_Name;
            this.c_Type = c_Type;
        }


        /// <summary>
        /// 获取string类型的值
        /// </summary>
        /// <returns></returns>
        public override string ToString()
        {
            if (c_Name == 1)
                return c_Type + "A";
            else if (c_Name == 11)
                return c_Type + "J";
            else if (c_Name == 12)
                return c_Type + "Q";
            else if (c_Name == 13)
                return c_Type + "K";
            else
                return c_Type + c_Name.ToString();
        }
    }

}



using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;


namespace playcards
{
    public delegate Cards TakeCardHandler();
    class People
    {
        /// <summary>
        /// 人的名字
        /// </summary>
        private string p_Name;
        /// <summary>
        /// 人手中牌的数组
        /// </summary>
        public Cards[] myCards;
        /// <summary>
        /// 手中牌的数量
        /// </summary>
        public int count;


        /// <summary>
        /// 写入和读取姓名
        /// </summary>
        public string P_Name
        {
            get { return p_Name; }
            set
            {
                Regex nameRegex = new Regex(@"([\u4e00-\u9fa5]{2,10}|[a-zA-Z]+)");
                while (true)
                {
                    if (nameRegex.IsMatch(value))
                        break;
                    else
                    {
                        Console.Write("姓名输入错误,请重新输入:");
                        value = Console.ReadLine();
                    }
                }
                p_Name = value;
            }
        }


        public event TakeCardHandler TakeCardEvent;


        /// <summary>
        /// 玩家抽牌
        /// </summary>
        /// <param name="index">牌的数量</param>
        public void GetCard(int index)
        {
            count = index;
            Cards c=new Cards();
            if (TakeCardEvent != null)
                c=TakeCardEvent();
            this.myCards[count] = new Cards(c.c_Type, c.c_Name);
            this.count++;
        }


        /// <summary>
        /// 打印
        /// </summary>
        public void Show()
        {
            for (int i = 0; i < count; i++)
                Console.Write(myCards[i].ToString() + "\t");
            Console.WriteLine();
        }
    }

}



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


namespace playcards
{
    class TotalCards
    {
        /// <summary>
        /// 整副牌
        /// </summary>
        public Cards[] cards;
        /// <summary>
        /// 当前牌的数量
        /// </summary>
        public int cardsCount;
        /// <summary>
        /// 随机数
        /// </summary>
        public Random r;


        /// <summary>
        /// 构造函数
        /// </summary>
        public TotalCards()
        {
            r = new Random();
            this.cards = new Cards[52];


            #region 一副牌
            for (int i = 0; i < cards.Length; i++)
            {
                //实例化
                cards[i] = new Cards();
                cards[0].c_Name = 1;
                if (i > 0)
                {
                    if (i % 4 == 0)
                        cards[i].c_Name = cards[i - 1].c_Name + 1;
                    else
                        cards[i].c_Name = cards[i - 1].c_Name;
                }
                if (i % 4 == 0)
                    cards[i].c_Type = CardType.红桃.ToString();
                else if (i % 4 == 1)
                    cards[i].c_Type = CardType.金花.ToString();
                else if (i % 4 == 2)
                    cards[i].c_Type = CardType.方块.ToString();
                else
                    cards[i].c_Type = CardType.黑桃.ToString();
            }
            #endregion
        }


        /// <summary>
        /// 随机数
        /// </summary>
        /// <returns>随机数对应的下标对应的数组中的值</returns>
        public Cards RandomIndex()
        {
            int index = r.Next(0, this.cardsCount);
            Cards c = new Cards(cards[index].c_Type, cards[index].c_Name);
            for (int i = index; i < cardsCount - 1; i++)
                cards[i] = cards[i + 1];
            this.cardsCount--;
            return c;
        }


        /// <summary>
        /// 绑定玩家玩牌
        /// </summary>
        /// <param name="p">玩家类</param>
        public void Play(People p)
        {
            p.TakeCardEvent += p_TakeCardEvent;
        }


        /// <summary>
        /// 随机的卡牌
        /// </summary>
        /// <returns></returns>
        public Cards p_TakeCardEvent()
        {
            Cards c = RandomIndex();
            return c;
        }
    }

}



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


namespace playcards
{
    class Program
    {
        static void Main(string[] args)
        {
            #region 输入人数和人名
            Console.Write("请输入玩牌的人数:(2-4)");
            int num = IsNumber();
            People[] person = new People[num];
            for (int i = 0; i < person.Length; i++)
            {
                person[i] = new People();
                Console.Write("请输入第{0}个玩家的姓名:", i + 1);
                person[i].P_Name = Console.ReadLine();
                while (true)
                {
                    int j = 0;
                    for (; j < i; j++)
                        if (person[j].P_Name == person[i].P_Name)
                            break;
                    if (j < i)
                    {
                        Console.Write("名字重复,请重新输入:");
                        person[i].P_Name = Console.ReadLine();
                    }
                    else
                        break;
                }
            }
            #endregion


            TotalCards t = new TotalCards();


            #region 由于人数问题决定红桃2是否存在,先将其和最后一位换位置
            Cards temp = t.cards[4];
            t.cards[4] = t.cards[t.cards.Length - 1];
            t.cards[t.cards.Length - 1] = temp; 
            #endregion


            #region 判断要随机多少张牌  52  51。
            if (num == 3)            
                t.cardsCount = 51;
            else
                t.cardsCount = 52;
            #endregion


            #region 人进行随机抽牌
            int length = t.cards.Length / num;
            int k = 0;
            for (int i = 0; i < person.Length; i++)
            {
                t.Play(person[i]);
                person[i].myCards = new Cards[length];
            }
            //玩家轮流抽牌
            while (k < length)
            {
                for (int i = 0; i < person.Length; i++)
                    person[i].GetCard(k);
                k++;
            }
            #endregion


            #region 打印每个人手中的牌
            for (int i = 0; i < person.Length; i++)
            {
                Console.WriteLine();
                Console.WriteLine(person[i].P_Name + "手中的牌为:");
                person[i].Show();
            } 
            #endregion


            Console.ReadKey(true);
        }


        /// <summary>
        /// 判断人数是否为2-4的数字
        /// </summary>
        /// <returns>int型</returns>
        private static int IsNumber()
        {
            string str = "";
            while (true)
            {
                str = Console.ReadLine();
                if (str == "2" || str == "3" || str == "4")
                    break;
                else
                    Console.Write("人数选择错误,请重新输入:");
            }
            return int.Parse(str);
        }
    }
}

猜你喜欢

转载自blog.csdn.net/fffssso/article/details/80528951
今日推荐