lua——赢三张牌型处理相关算法(上)——牌值数据

赢三张,我们东北管这个叫拖拉机。其他的几个名称是违规的。。


是一个简单粗暴的扑克牌(dubo)游戏。玩家每人三张牌,通过特定的牌型规则进行比大小。(押注什么的就不说了,具体详细规则请参考百度)

最近一直深陷在各种扑克玩法的牌型逻辑处理中,对比了一下,觉得赢三张应该算是比较简单的了,于是在这里写出一些牌型逻辑处理的算法。有些也适用于其他的扑克牌玩法。

注:语言为lua


首先是扑克牌的权值数据:对于这类花色有特殊意义的扑克玩法,一般常用的数据结构是采用16进制双位数,一位表示花色,一位表示牌值

例如:

CardData=
{
    0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09,0x0A,0x0B,0x0C,0x0D,0x0E,   --黑桃 2 - A(14)
    0x12,0x13,0x14,0x15,0x16,0x17,0x18,0x19,0x1A,0x1B,0x1C,0x1D,0x1E,   --红桃 2 - A
    0x22,0x23,0x24,0x25,0x26,0x27,0x28,0x29,0x2A,0x2B,0x2C,0x2D,0x2E,   --樱花 2 - A
    0x32,0x33,0x34,0x35,0x36,0x37,0x38,0x39,0x3A,0x3B,0x3C,0x3D,0x3E,   --方块 2 - A
}

然后使用位运算&可以方便的获取花色及牌值

lua语言里是band,需要导入bit模块。

即:

luabit = require"bit"
local cardType = luabit.band(CardData[i] , 0xF0)
local cardVal = luabit.band(CardData[i] , 0x0F)


得到的值对应的牌:


CardColor =
{
    Spade = 0,         --黑桃
    Heart = 16,         --红桃
    Plum  = 32,         --梅花
    Block = 48,         --方块
}

CardValue =
{
    card_2 = 2,
    card_3 = 3,
    card_4 = 4,
    card_5 = 5,
    card_6 = 6,
    card_7 = 7,
    card_8 = 8,
    card_9 = 9,
    card_10 = 10,
    card_J = 11,
    card_Q = 12,
    card_K = 13,
    card_A = 14,
}

扎金花游戏没有大小王,故总牌数为52

g_CardsCount            = 52     --扑克数目
g_PerPlaCardCount       = 3      --每个玩家牌数

下面我们写出一个简单的洗牌发牌测试函数


洗牌:

function cardTool.RandCardList()

    local bufferCount = #CardData;
    local randCount = 1;
    local position = 1;


    math.randomseed(os.time()) 


        
    for i = 1,#CardData do
        local ranOne = math.random(1,#CardData+1-i)
        CardData[ranOne], CardData[#CardData+1-i] = CardData[#CardData+1-i],CardData[ranOne]
    end

    local cardBuffer = tabletool.deepcopy(CardData);
    return cardBuffer;
end

发牌:

local cardBuffer =cardTool.RandCardList()


--cardBuffer[1]=6
--cardBuffer[2]=7+16
--cardBuffer[3]=7

--cardBuffer[4]=5+16
--cardBuffer[5]=7+32
--cardBuffer[6]=7+48


local cards1={}
local cards2={}

for i=1,6,1 do
    local cardColor = luabit.band(cardBuffer[i] , 0xF0)
    local cardValue = luabit.band(cardBuffer[i] , 0x0F)


    local cardinfo =
    {
        card_value = cardValue;
        card_Color = cardColor;
    }
    if i >3 then
        cards2[i-3] = cardinfo
    else
        cards1[i] = cardinfo
    end


end

输出两组牌:

table: 00444950 {
  [1] => table: 00444950 {
           [card_Color] => 48
           [card_value] => 2
         }
  [2] => table: 00444950 {
           [card_Color] => 16
           [card_value] => 9
         }
  [3] => table: 00444950 {
           [card_Color] => 0
           [card_value] => 14
         }
}

table: 00444978 {
  [1] => table: 00444978 {
           [card_Color] => 32
           [card_value] => 2
         }
  [2] => table: 00444978 {
           [card_Color] => 32
           [card_value] => 4
         }
  [3] => table: 00444978 {
           [card_Color] => 32
           [card_value] => 12
         }
}

为了方便查看结果,我们写个翻译函数

function cardTool.getCardNamebyCard(Card)

    local string=""
    if Card.card_Color ==0 then
        string=string.."黑桃"
    elseif Card.card_Color ==16 then
        string=string.."红桃"
    elseif Card.card_Color ==32 then
        string=string.."梅花"
    elseif Card.card_Color ==48 then
        string=string.."方块"
    else
        string="ERROR"
    end

    if Card.card_value==14 then
        string=string.."A"
    elseif Card.card_value==13 then
        string=string.."K"
    elseif Card.card_value==12 then
        string=string.."Q"
    elseif Card.card_value==11 then
        string=string.."J"
    else
         string=string..Card.card_value
    end
    return string
end

function cardTool.getCardNamebyCards(Cards)
    local string=""
    for i = 1,#Cards do
       string=string..cardTool.getCardNamebyCard(Cards[i])
    end
    return string
end


上文中两组牌即为:

  方块2红桃9黑桃A

  梅花2梅花4梅花Q

以上就是牌值数据相关的处理函数,下一章我们实现判定牌型的相关函数。







你可能感兴趣的:(游戏算法,lua,nine_sun算法专栏,棋牌算法,算法,赢三张,lua,后台)