斗地主(二)———创建扑克牌基类枚举, 牌库,出牌规则

分析:

扑克牌有规律可循

花色4种:**梅花❀、方块♦、黑桃♠、红桃♥**

牌由 1到13(J、Q、K),加花色加大小猫,共有54张

牌还有大小之分,主和非主(权重)

身份:**地主、农民**

出牌类型 单牌、对、连对、顺子、飞机、炸弹等

接下来定义枚举Enum

/// <summary>
/// 卡牌花色
/// </summary>
public enum CardColors
{
    /// <summary>
    /// 无
    /// </summary>
    None =0,
    /// <summary>
    /// 梅花
    /// </summary>
    Club =1,
    /// <summary>
    /// 红桃
    /// </summary>
    Heart =2,
    /// <summary>
    /// 黑桃
    /// </summary>
    Spade=3,
    /// <summary>
    /// 方片
    /// </summary>
    Square=4
}


/// <summary>
/// 卡牌的权值
/// 大小,毛 主?
/// </summary>
public enum CardWeight
{
    /// <summary>
    /// 3
    /// </summary>
    Three,
    /// <summary>
    /// 4
    /// </summary>
    Four,
    /// <summary>
    /// 5
    /// </summary>
    Five,
    Six,
    Seven,
    Eight,
    Nine,
    Ten,
    Jack,
    Queen,
    King,
    One,
    /// <summary>
    /// 2主
    /// </summary>
    Two,
    SJoker,
    LJoker
}


/// <summary>
/// 出牌类型
/// 单、双、3带1-带2、顺、飞机
/// </summary>
public enum CardType
{
    None,
    /// <summary>
    /// 单
    /// </summary>
    Single,
    /// <summary>
    /// 对儿
    /// </summary>
    Double,
    /// <summary>
    /// 顺子
    /// </summary>
    Straight,
    /// <summary>
    /// 双顺
    /// </summary>
    DoubleStraight,
    /// <summary>
    /// 飞机
    /// </summary>
    TripleStraight,
    /// <summary>
    /// 三不带
    /// </summary>
    Three,
    /// <summary>
    /// 三带一
    /// </summary>
    ThreeAndOne,
    /// <summary>
    /// 三代二
    /// </summary>
    ThreeAndTwo,
    /// <summary>
    /// 炸弹
    /// </summary>
    Boom,
    /// <summary>
    /// 王炸
    /// </summary>
    JokerBoom
}


/// <summary>
/// 身份——地主、农民
/// </summary>
public enum Identity
{
    Farmer,//农民
    Landlord//地主
}

牌基类

/// <summary>
/// 牌基类
/// </summary>
public class BaseCard {

    private string cardName;
    private CardColors cardColor;
    private CardWeight cardWeight;
    private CharacterType belongTo;

    /// <summary>
    /// 卡牌名字
    /// </summary>
    public string CardName
    {
        get { return cardName; }
    }

    /// <summary>
    /// 卡牌花色
    /// </summary>
    public CardColors CardColor
    {
        get { return cardColor; }
    }

    /// <summary>
    /// 卡牌权值
    /// </summary>
    public CardWeight CardWeight
    {
        get { return cardWeight; }
    }

    /// <summary>
    /// 卡牌所属的角色
    /// </summary>
    public CharacterType BelongTo
    {
        get { return belongTo; }
        set { belongTo = value; }
    }

    /// <summary>
    /// 构造函数
    /// 花色,牌数 设定后不能修改
    /// </summary>
    /// <param name="name">牌名</param>
    /// <param name="color">花色</param>
    /// <param name="weight">权重</param>
    /// <param name="belongTo">角色类型</param>
    public BaseCard(string name, CardColors color, CardWeight weight, CharacterType belongTo)
    {
        this.cardName = name;
        this.cardColor = color;
        this.cardWeight = weight;
        this.belongTo = belongTo;
    }
}

接下来写模拟牌库

功能:发牌、收牌、洗牌

创建牌库类

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
/// <summary>
/// 牌库
/// </summary>
public class CardsLibrary{
 

    /// <summary>
    /// 牌库队列
    /// </summary>
    public Queue<BaseCard> cardQueue = new Queue<BaseCard>();

    /// <summary>
    /// 创建---初始化牌库(54张牌)
    /// </summary>
	public void InitCardLibrayr()
    {
        //填充牌库
        CardColors colors = CardColors.None;
        CardWeight weight = CardWeight.One;

        for (int i = 1; i <= 13; i++)
        {
            for (int j = 0; j <= 4; j++)
            {      
                colors = (CardColors)j;
                weight = (CardWeight)i;
                BaseCard card = new BaseCard(colors.ToString() + weight.ToString(),colors,weight,CharacterType.Library);
                cardQueue.Enqueue(card);//入队
            }
        }
        //大小王
        BaseCard SJokerCard = new BaseCard("SJoker", CardColors.None, CardWeight.SJoker, CharacterType.Library);
        BaseCard LJokerCard = new BaseCard("LJoker", CardColors.None, CardWeight.LJoker, CharacterType.Library);
        cardQueue.Enqueue(SJokerCard);
        cardQueue.Enqueue(LJokerCard);
    }
	
    /// <summary>
    /// 发牌
    /// </summary>
	public BaseCard DealCard()
    {
        return cardQueue.Dequeue();
    }

    /// <summary>
    /// 回收牌
    /// </summary>
    /// <param name="card"></param>
    public void RecycleCard(BaseCard card)
    {
        cardQueue.Enqueue(card);
    }

    /// <summary>
    /// 洗牌
    /// </summary>
    public void ShuffleCards()
    {
        List<BaseCard> newList = new List<BaseCard>();
        foreach (BaseCard BaseCard in cardQueue)
        {
            int index = Random.Range(0, newList.Count + 1);
            newList.Insert(index, BaseCard);
        }

        cardQueue.Clear();  //清空队列

        //重新填充队列
        foreach (BaseCard BaseCard in newList)
        {
            cardQueue.Enqueue(BaseCard);
        }
        newList.Clear();
    }
}

还有判断出牌类型

这个比较复杂,也不难,就是繁琐一些

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

/// <summary>
/// 出牌规则类,判定出牌是否合法
/// </summary>
public static class CardsRule{

    /*
     * 单牌 :
     * 1 
     * 5 
     * 8
     * 
     * 对子:
     * 2-2
     * 4-4
     * 
     * 顺子:
     * 5-6-7-8-9-10
     * 9-10-11-12-13
     * 
     * 双顺子:
     * 55-66-77-88
     * 88-99-10 10-11 11
     * 
     * 飞机:
     * 555-666-777
     * 999-10 10 10 
     * 
     * 三不带:
     * 555
     * 666
     * 777
     * 
     * 三带一
     * 555-9
     * 999-8
     * 
     * 三带二
     * 555-99
     * 777-55
     * 
     * 炸弹:
     * 5555
     * 9999
     * 8888
     * 
     * 王炸:
     * 大猫 小猫
     */

    /// <summary>
    /// 是否单牌
    /// </summary>
    /// <returns></returns>
	public static bool IsSingle(List<BaseCard> cards)
    {
        //牌数不为1,则不是单牌
        return cards.Count == 1;    
    }

    /// <summary>
    /// 是否对子
    /// </summary>
    /// <returns></returns>
    public static bool IsDouble(List<BaseCard> cards)
    {
        //牌数不为2,和第一张牌跟第二张是否相同
        if(cards.Count == 2)
        {
            if(cards[0].CardWeight == cards[1].CardWeight)
            {
                return true;
            }
        }
        return false;
    }

    /// <summary>
    /// 是否顺子
    /// </summary>
    /// <param name="cards"></param>
    /// <returns></returns>
    public static bool IsStraight(List<BaseCard> cards)
    {
        //出牌数量 不能小于5 或大于12
        if(cards.Count < 5 || cards.Count > 12)
        {
            return false;
        }

        //每个相减都为1
        for (int i = 0; i < cards.Count - 1; i++)
        {
            CardWeight tempWeight = cards[i].CardWeight;
            if (cards[i + 1].CardWeight - tempWeight != 1)
                return false;
            //不能超过A
            if (tempWeight > CardWeight.One || cards[i + 1].CardWeight > CardWeight.One)
                return false;
        }
        return true;
    }

    /// <summary>
    /// 是否是双顺
    /// </summary>
    /// <param name="cards"></param>
    /// <returns></returns>
    public static bool IsDoubleStraight(List<BaseCard> cards)
    {
        //牌数大于6 及除2为0
        if (cards.Count < 6 || cards.Count % 2 != 0)
            return false;
        //第一张牌不等于第三张牌 切相减为1
        for (int i = 0; i < cards.Count - 2; i += 2)
        {
            if (cards[i].CardWeight != cards[i + 1].CardWeight)
                return false;
            if (cards[i + 2].CardWeight - cards[i].CardWeight != 1)
                return false;
            //不能超过A
            if (cards[i].CardWeight > CardWeight.One || cards[i + 2].CardWeight > CardWeight.One)
                return false;
        }

        return true;
    }

    /// <summary>
    /// 是否飞机
    /// </summary>
    /// <param name="cards"></param>
    /// <returns></returns>
    public static bool IsTripleStraight(List<BaseCard> cards)
    {
        //小于6 除3不为0
        if(cards.Count <6 || cards.Count %3 != 0)
        {
            return false;
        }

        for (int i = 0; i < cards.Count - 3; i += 3)
        {
            if (cards[i].CardWeight != cards[i + 1].CardWeight)
                return false;
            if (cards[i + 2].CardWeight != cards[i + 1].CardWeight)
                return false;
            if (cards[i].CardWeight != cards[i + 2].CardWeight)
                return false;

            if (cards[i + 3].CardWeight - cards[i].CardWeight != 1)
                return false;
            //不能超过A
            if (cards[i].CardWeight > CardWeight.One || cards[i + 3].CardWeight > CardWeight.One)
                return false;
        }
        return true;
    }

    
    /// <summary>
    /// 是否三不带
    /// </summary>
    /// <param name="cards"></param>
    /// <returns></returns>
    public static bool IsThree(List<BaseCard> cards)
    {
        if(cards.Count != 3)
        {
            return false;
        }
        
        if(cards[0].CardWeight != cards[1].CardWeight)
        {
            return false;
        }

        if(cards[1].CardWeight != cards[2].CardWeight)
        {
            return false;
        }

        if (cards[2].CardWeight != cards[0].CardWeight)
        {
            return false;
        }

        return true;
    }

    /// <summary>
    /// 是否是三带一
    /// </summary>
    /// <param name="cards"></param>
    /// <returns></returns>
    public static bool IsThreeAndOne(List<BaseCard> cards)
    {
        if (cards.Count != 4)
            return false;

        if (cards[0].CardWeight == cards[1].CardWeight && cards[1].CardWeight == cards[2].CardWeight)
            return true;
        else if (cards[1].CardWeight == cards[2].CardWeight && cards[2].CardWeight == cards[3].CardWeight)
            return true;

        return false;
    }

    /// <summary>
    /// 是否是三带二
    /// </summary>
    /// <param name="cards"></param>
    /// <returns></returns>
    public static bool IsThreeAndTwo(List<BaseCard> cards)
    {
        if (cards.Count != 5)
            return false;

        if (cards[0].CardWeight == cards[1].CardWeight && cards[1].CardWeight == cards[2].CardWeight)
        {
            if (cards[3].CardWeight == cards[4].CardWeight)
                return true;
        }
        else if (cards[2].CardWeight == cards[3].CardWeight && cards[3].CardWeight == cards[4].CardWeight)
        {
            if (cards[0].CardWeight == cards[1].CardWeight)
                return true;
        }

        return false;
    }

    /// <summary>
    /// 判断是否是炸弹
    /// </summary>
    /// <param name="cards"></param>
    /// <returns></returns>
    public static bool IsBoom(List<BaseCard> cards)
    {
        if (cards.Count != 4)
            return false;
        if (cards[0].CardWeight != cards[1].CardWeight)
            return false;
        if (cards[1].CardWeight != cards[2].CardWeight)
            return false;
        if (cards[2].CardWeight != cards[3].CardWeight)
            return false;

        return true;
    }

    /// <summary>
    /// 是否王炸
    /// </summary>
    /// <param name="cards"></param>
    /// <returns></returns>
    public static bool IsJokerBoom(List<BaseCard> cards)
    {
        if (cards.Count != 2)
            return false;

        if (cards[0].CardWeight == CardWeight.SJoker && cards[1].CardWeight == CardWeight.LJoker)
        {
            return true;
        }
        else if (cards[0].CardWeight == CardWeight.LJoker && cards[1].CardWeight == CardWeight.SJoker)
        {
            return true;
        }       
        return false;
    }

 
    /// <summary>
    /// 判断能否出牌
    /// </summary>
    /// <param name="cards">要出的牌</param>
    /// <param name="type">出牌类型</param>
    /// <returns>能否出牌</returns>s
    public static bool CanPop(List<BaseCard> cards, out CardType type)
    {
        //这里又学到了out参数,方法多个返回值如何处理
        type = CardType.None;
        bool can = false;

        switch (cards.Count)
        {
            case 1:
                if (IsSingle(cards))
                {
                    type = CardType.Single;
                    can = true;
                }
                break;
            case 2:
                if (IsDouble(cards))
                {
                    type = CardType.Double;
                    can = true;
                }
                else if (IsJokerBoom(cards))
                {
                    type = CardType.JokerBoom;
                    can = true;
                }
                break;
            case 3:
                if (IsThree(cards))
                {
                    type = CardType.Three;
                    can = true;
                }
                break;
            case 4:
                if (IsBoom(cards))
                {
                    type = CardType.Boom;
                    can = true;
                }
                else if (IsThreeAndOne(cards))
                {
                    type = CardType.ThreeAndOne;
                    can = true;
                }
                break;
            case 5:
                if (IsStraight(cards))
                {
                    type = CardType.Straight;
                    can = true;
                }
                else if (IsThreeAndTwo(cards))
                {
                    type = CardType.ThreeAndTwo;
                    can = true;
                }
                break;
            case 6:
                if (IsStraight(cards))
                {
                    type = CardType.Straight;
                    can = true;
                }
                else if (IsDoubleStraight(cards))
                {
                    type = CardType.DoubleStraight;
                    can = true;
                }
                else if (IsTripleStraight(cards))
                {
                    type = CardType.TripleStraight;
                    can = true;
                }
                break;
            case 7:
                if (IsStraight(cards))
                {
                    type = CardType.Straight;
                    can = true;
                }
                break;
            case 8:
                if (IsStraight(cards))
                {
                    type = CardType.Straight;
                    can = true;
                }
                else if (IsDoubleStraight(cards))
                {
                    type = CardType.DoubleStraight;
                    can = true;
                }
                break;
            case 9:
                if (IsStraight(cards))
                {
                    type = CardType.Straight;
                    can = true;
                }//777 888 999 
                else if (IsTripleStraight(cards))
                {
                    type = CardType.TripleStraight;
                    can = true;
                }
                break;
            case 10:
                if (IsStraight(cards))
                {
                    type = CardType.Straight;
                    can = true;
                }
                else if (IsDoubleStraight(cards))
                {
                    type = CardType.DoubleStraight;
                    can = true;
                }
                break;
            case 11:
                if (IsStraight(cards))
                {
                    type = CardType.Straight;
                    can = true;
                }
                break;
            case 12:
                if (IsStraight(cards))
                {
                    type = CardType.Straight;
                    can = true;
                }
                else if (IsDoubleStraight(cards))
                {
                    type = CardType.DoubleStraight;
                    can = true;
                }// 444 555 666 777
                else if (IsTripleStraight(cards))
                {
                    type = CardType.TripleStraight;
                    can = true;
                }
                break;
            case 13:
                break;
            case 14:
                if (IsDoubleStraight(cards))
                {
                    type = CardType.DoubleStraight;
                    can = true;
                }
                break;
            case 15:
                if (IsTripleStraight(cards))
                {
                    type = CardType.TripleStraight;
                    can = true;
                }
                break;
            case 16:
                if (IsDoubleStraight(cards))
                {
                    type = CardType.DoubleStraight;
                    can = true;
                }
                break;
            case 17:
                break;
            case 18:
                if (IsDoubleStraight(cards))
                {
                    type = CardType.DoubleStraight;
                    can = true;
                }// 444 555 666 777 888 999 
                else if (IsTripleStraight(cards))
                {
                    type = CardType.TripleStraight;
                    can = true;
                }
                break;
            case 19:
                break;
            case 20:
                if (IsDoubleStraight(cards))
                {
                    type = CardType.DoubleStraight;
                    can = true;
                }
                break;
            default:
                break;
        }
        return can;
    }
}
发布了57 篇原创文章 · 获赞 37 · 访问量 3万+

猜你喜欢

转载自blog.csdn.net/qq_35030499/article/details/83413721
今日推荐