一个纸牌发牌的完整类

参照http://www.cnblogs.com/hawking106123/archive/2007/06/14/783202.html,对其中一些繁琐的内容进行删节,整理后发布这个完整的代码:

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

namespace CPoker
{
    /// <summary>
    /// 纸牌类
    /// </summary>
    public class CPoker
    {
        public int Value;//牌面数字
    }

    /// <summary>
    /// 游戏准备类
    /// </summary>
    public class playPoker
    {
        //0-12表示黑桃A到K   13-25表示红桃A到K   26-38表示梅花A到K   39-51表示方块A到K    52表示小鬼    53表示大鬼
        protected CPoker[] poker = new CPoker[54];//构造出所有纸牌
        public CPoker[] poker1 = new CPoker[17];//玩家1手中的纸牌
        public CPoker[] poker2 = new CPoker[17];//玩家2手中的纸牌
        public CPoker[] poker3 = new CPoker[17];//玩家3手中的纸牌
        public CPoker[] pokerHole = new CPoker[3];//底牌

        /// <summary>
        /// 创建纸牌
        /// </summary>
        public void buyPoker()
        {
            //分别为纸牌每个牌赋初始值
            for (int i = 0; i < poker.Length; i++)
            {
                poker[i] = new CPoker();
                poker[i].Value = i;
            }
        }

        /// <summary>
        /// 洗牌(重新随机排列)
        /// </summary>
        public void rufflePoker()
        {
            int m, n;
            Random ram = new Random();
            for (int i = 0; i < 1000; i++)
            {
                m = ram.Next(0, 54);
                n = ram.Next(0, 54);
                if (m != n)
                {
                    //置换纸牌
                    CPoker temp;
                    temp = poker[m];
                    poker[m] = poker[n];
                    poker[n] = temp;
                }
            }
        }

        /// <summary>
        /// 发牌
        /// </summary>
        public void dealPoker()
        {
            //分别为三个玩家分牌
            for (int i = 0; i < 17; i++)
            {
                poker1[i] = poker[i * 3];
                poker2[i] = poker[i * 3 + 1];
                poker3[i] = poker[i * 3 + 2];
            }
            //为底牌分牌
            pokerHole[0] = poker[51];
            pokerHole[1] = poker[52];
            pokerHole[2] = poker[53];
        }
    }
}

引用:

using System;
using System.Collections.Generic;
using System.Text;
using CPoker;//

namespace Demo
{
    class Program
    {
        static void Main(string[] args)
        {
            playPoker ppk = new playPoker();
            ppk.buyPoker();//买牌
            ppk.rufflePoker();//洗牌
            ppk.dealPoker();//发牌
            for (int i = 0; i < 17; i++)
            {
                Console.WriteLine("玩家1的牌为:" + ppk.poker1[i].Value.ToString());
            }
            for (int i = 0; i < 17; i++)
            {
                Console.WriteLine("玩家2的牌为:" + ppk.poker2[i].Value.ToString());
            }
            for (int i = 0; i < 17; i++)
            {
                Console.WriteLine("玩家2的牌为:" + ppk.poker3[i].Value.ToString());
            }
            for (int i = 0; i < 3; i++)
            {
                Console.WriteLine("底牌为:" + ppk.pokerHole[i].Value.ToString());
            }
            Console.ReadKey();
           
        }
    }
}

原文地址:https://www.cnblogs.com/mane/p/2005711.html