最近在写棋牌游戏,写了一个牛牛的算法,在这里分享给大家! 部分代码参考了网上的!
main.lua
#!/usr/local/bin/lua
local card = require("card")
local bit = require("bit")
local cardBuffer = card.RandCardList()
local cards1 = {}
local cards2 = {}
local cards3 = {}
local cards4 = {}
for i = 1, 20, 1 do
local cardColor = bit.band(cardBuffer[i], 0xF0) / 16 + 1
local cardValue = bit.band(cardBuffer[i], 0x0F)
local cardCount = card.getCountByValue(cardValue)
local cardInfo = {
card_value = cardValue,
card_color = cardColor,
card_count = cardCount
}
if i <= 5 then
cards1[i] = cardInfo
elseif i > 5 and i <= 10 then
cards2[i - 5] = cardInfo
elseif i > 10 and i <= 15 then
cards3[i - 10] = cardInfo
elseif i > 15 and i <= 20 then
cards4[i - 15] = cardInfo
elseif i > 20 and i <= 25 then
cards5[i - 20] = cardInfo
end
end
print("庄家牌:", card.getCardNameByCards(cards1))
print("庄家牛牛类型:", card.getCardTypeNameByType(card.getTypeByCards(cards1)))
print("------------------------------------")
print("闲1牌:", card.getCardNameByCards(cards2))
print("闲1牛牛类型:", card.getCardTypeNameByType(card.getTypeByCards(cards2)))
print("------------------------------------")
print("闲2牌:", card.getCardNameByCards(cards3))
print("闲2牛牛类型:", card.getCardTypeNameByType(card.getTypeByCards(cards3)))
print("------------------------------------")
print("闲3牌:", card.getCardNameByCards(cards4))
print("闲3牛牛类型:", card.getCardTypeNameByType(card.getTypeByCards(cards4)))
print("------------------------------------")
print("闲4牌:", card.getCardNameByCards(cards5))
print("闲4牛牛类型:", card.getCardTypeNameByType(card.getTypeByCards(cards5)))
card.lua
card = {}
-- 花色
CardColor = {
Spade = 4, --黑桃
Heart = 3, --红桃
Plum = 2, --梅花
Block = 1, --方块
}
-- 牌型
CardType = {
NOT_NIU = 0, --没牛
NIU_1 = 1, --牛一
NIU_2 = 2, --牛二
NIU_3 = 3, --牛三
NIU_4 = 4, --牛四
NIU_5 = 5, --牛五
NIU_6 = 6, --牛六
NIU_7 = 7, --牛七
NIU_8 = 8, --牛八
NIU_9 = 9, --牛九
NIU_NIU = 10, --牛牛
SILVER_NIU = 11, --银牛
GOLD_NIU = 12, --金牛
BOMB = 13, --炸弹
SMALL_NIU = 14, --五小牛
}
-- 所有的扑克牌
CardValue = {
card_A = 1,
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,
}
--扑克数据
CardData = {
0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D,
0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1A, 0x1B, 0x1C, 0x1D,
0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28, 0x29, 0x2A, 0x2B, 0x2C, 0x2D,
0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x3A, 0x3B, 0x3C, 0x3D,
}
function card.getCountByValue(value)
if value > 10 then
return 10
else
return value
end
end
function card.getCardNameByCard(Card)
local string = ""
if Card.card_color == 4 then
string = string .. "黑桃"
elseif Card.card_color == 3 then
string = string .. "红桃"
elseif Card.card_color == 2 then
string = string .. "梅花"
elseif Card.card_color == 1 then
string = string .. "方块"
else
string = "ERROR"
end
if 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 card.getCardNameByCards(Cards)
local string = ""
for i = 1, #Cards do
string = string .. card.getCardNameByCard(Cards[i])
end
return string
end
-- 洗牌
function card.RandCardList()
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 = card.deepCopy(CardData);
return cardBuffer;
end
function card.deepCopy(orig)
local orig_type = type(orig)
local copy
if orig_type == 'table' then
copy = {}
for orig_key, orig_value in next, orig, nil do
copy[card.deepCopy(orig_key)] = card.deepCopy(orig_value)
end
setmetatable(copy, card.deepCopy(getmetatable(orig)))
else
-- number, string, boolean, etc
copy = orig
end
return copy
end
function card.compByCardsValue(a, b)
if a.card_value < b.card_value then
return true
end
if a.card_value > b.card_value then
return false
end
return a.card_color < b.card_color
end
function card.sortByCardsValue(cards)
table.sort(cards, card.compByCardsValue);
end
function card.is_small_niu(cards)
local sum = 0
for i = 1, #cards do
sum = sum + cards[i].card_count
end
if sum <= 10 then
return true
else
return false
end
end
function card.is_bomb(cards)
if cards[1].card_value == cards[4].card_value then
return true
elseif cards[2].card_value == cards[5].card_value then
return true
else
return false
end
end
function card.is_gold_niu(cards)
if cards[1].card_value > 10 then
return true
else
return false
end
end
function card.is_silver_niu(cards)
if cards[2].card_value > 10 and cards[1].card_value == 10 then
return true
else
return false
end
end
function card.getNiuByCards(cards)
local lave = 0 --余数
for i = 1, #cards do
lave = lave + cards[i].card_count
end
lave = lave % 10
for i = 1, #cards - 1 do
for j = i + 1, #cards do
if (cards[i].card_count + cards[j].card_count) % 10 == lave then
if lave == 0 then
return 10
else
return lave
end
end
end
end
return 0
end
function card.getTypeByCards(cards)
card.sortByCardsValue(cards)
local cardType = CardType.NOT_NIU
if card.is_small_niu(cards) then
cardType = CardType.SMALL_NIU
return cardType
end
if card.is_bomb(cards) then
cardType = CardType.BOMB
return cardType
end
if card.is_gold_niu(cards) then
cardType = CardType.GOLD_NIU
return cardType
end
if card.is_silver_niu(cards) then
cardType = CardType.SILVER_NIU
return cardType
end
cardType = card.getNiuByCards(cards)
return cardType
end
function card.getCardTypeNameByType(CardType)
if CardType == 0 then
return "没牛"
end
if CardType == 1 then
return "牛一"
end
if CardType == 2 then
return "牛二"
end
if CardType == 3 then
return "牛三"
end
if CardType == 4 then
return "牛四"
end
if CardType == 5 then
return "牛五"
end
if CardType == 6 then
return "牛六"
end
if CardType == 7 then
return "牛七"
end
if CardType == 8 then
return "牛八"
end
if CardType == 9 then
return "牛九"
end
if CardType == 10 then
return "牛牛"
end
if CardType == 11 then
return "银牛"
end
if CardType == 12 then
return "金牛"
end
if CardType == 13 then
return "炸弹"
end
if CardType == 14 then
return "五小牛"
end
return "异常牌型"
end
function card.bankerIsWin(banker_Cards, other_Cards)
local banker_Cards_Type = card.getTypeByCards(banker_Cards)
local other_Cards_Type = card.getTypeByCards(other_Cards)
if banker_Cards_Type ~= other_Cards_Type then
return banker_Cards_Type > other_Cards_Type
end
if banker_Cards_Type == CardType.SMALL_NIU then
return true
end
if banker_Cards_Type == CardType.BOMB then
return banker_Cards[3].card_value > other_Cards[3].card_value
end
if banker_Cards_Type == CardType.GOLD_NIU then
return card.compByCardsValue(other_Cards[5], banker_Cards[5])
end
if banker_Cards_Type == CardType.SILVER_NIU then
return card.compByCardsValue(other_Cards[5], banker_Cards[5])
end
if banker_Cards_Type == CardType.NIU_NIU then
return card.compByCardsValue(other_Cards[5], banker_Cards[5])
end
if banker_Cards_Type == CardType.NOT_NIU then
return card.compByCardsValue(other_Cards[5], banker_Cards[5])
end
return true
end
return card
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。