Lua语言实现斗地主规则
Lua语言实现斗地主规则,可以参考如下代码:
-- 定义扑克牌 local poker = { {value = 1, name = "A"}, {value = 2, name = "2"}, {value = 3, name = "3"}, {value = 4, name = "4"}, {value = 5, name = "5"}, {value = 6, name = "6"}, {value = 7, name = "7"}, {value = 8, name = "8"}, {value = 9, name = "9"}, {value = 10, name = "10"}, {value = 11, name = "J"}, {value = 12, name = "Q"}, {value = 13, name = "K"} }
-- 定义花色 local suits = {"♠", "♥", "♣", "♦"}
-- 获取一副扑克牌 function get_poker() local all_poker = {} for _, suit in ipairs(suits) do for _, card in ipairs(poker) do table.insert(all_poker, {suit = suit, value = card.value, name = card.name .. suit}) end end return all_poker end
-- 洗牌 function shuffle(deck) for i = 1, #deck do local j = math.random(1, #deck) deck[i], deck[j] = deck[j], deck[i] end end
-- 发牌 function deal(deck, players) for i = 1, 17 do for _, player in ipairs(players) do table.insert(player.hand, table.remove(deck)) end end end
-- 比较两个牌型大小 function compare(hand1, hand2) local type1, data1 = get_type(hand1) local type2, data2 = get_type(hand2) if type1 > type2 then return true elseif type1 < type2 then return false else if type1 == 1 then -- 单牌 return data1[1].value > data2[1].value elseif type1 == 2 then -- 对子 return data1[1].value > data2[1].value elseif type1 == 3 then -- 三张 return data1[1].value > data2[1].value elseif type1 == 4 then -- 三带一 return data1[1].value > data2[1].value elseif type1 == 5 then -- 三带二 return data1[1].value > data2[1].value elseif type1 == 6 then -- 顺子 return data1[1].value > data2[1].value elseif type1 == 7 then -- 连对 return data1[1].value > data2[1].value elseif type1 == 8 then -- 飞机不带 return data1[1].value > data2[1].value elseif type1 == 9 then -- 飞机带单 return data1[1].value > data2[1].value elseif type1 == 10 then -- 飞机带双 return data1[1].value > data2[1].value elseif type1 == 11 then -- 炸弹 return data1[1].value > data2[1].value elseif type1 == 12 then -- 王炸 return true end end end
-- 获取牌型和牌型数据 function get_type(hand) local type, data = nil, {} table.sort(hand, function(a, b) return a.value < b.value end) local len = #hand if len == 1 then -- 单牌 type, data = 1, hand elseif len == 2 and hand[1].name == "小王" and hand[2].name == "大王" then -- 王炸 type, data = 12, hand elseif len == 2 and hand[1].value == hand[2].value then -- 对子 type, data = 2, hand elseif len == 3 and hand[1].value == hand[2].value and hand[2].value == hand[3].value then -- 三张 type, data = 3, hand elseif len == 4 and hand[1].value == hand[2].value and hand[2].value == hand[3].value and hand[3].value == hand[4].value then -- 炸弹 type, data = 11, hand elseif len == 4 and hand[1].value == hand[2].value and hand[2].value == hand[3].value then -- 三带一 type, data = 4, hand table.insert(data, hand[4]) elseif len == 4 and hand[2].value == hand[3].value and hand[3].value == hand[4].value then -- 三带一 type, data = 4, hand table.insert(data, 1, hand[1]) elseif len == 5 and hand[1].value == hand[2].value and hand[2].value == hand[3].value and hand[4].value == hand[5].value then -- 三带二 type, data = 5, hand elseif len == 5 and hand[1].value == hand[2].value and hand[3].value == hand[4].value and hand[4].value == hand[5].value then -- 三带二 type, data = 5, hand table.insert(data, 1, hand[5]) elseif len == 5 and hand[1].value + 4 == hand[5].value and hand[1].value ~= 2 then -- 顺子 type, data = 6, hand elseif len == 6 and hand[1].value == hand[2].value and hand[3].value == hand[4].value and hand[5].value == hand[6].value and hand[1].value + 4 == hand[5].value and hand[1].value ~= 2 then -- 连对 type, data = 7, hand elseif len == 6 and hand[1].value == hand[2].value and hand[2].value == hand[3].value and hand[4].value == hand[5].value and hand[5].value == hand[6].value then -- 飞机不带 type, data = 8, hand elseif len == 8 and hand[1].value == hand[2].value and hand[2].value == hand[3].value and hand[4].value == hand[5].value and hand[5].value == hand[6].value and hand[7].value == hand[8].value and hand[1].value + 4 == hand[5].value and hand[1].value ~= 2 then -- 飞机带双 type, data = 10, hand else -- 无法识别的牌型 type, data = nil, {} end return type, data end
-- 打印牌 function print_hand(hand) local str = "" for _, card in ipairs(hand) do str = str .. card.name .. " " end print(str) end
-- 游戏主函数 function play() local deck = get_poker() shuffle(deck) local players = { {name = "Player 1", hand = {}}, {name = "Player 2", hand = {}}, {name = "Player 3", hand = {}} } deal(deck, players) for _, player in ipairs(players) do table.sort(player.hand, function(a, b) return a.value < b.value end) print(player.name .. "的牌:") print_hand(player.hand) end local last_hand = {} while true do local current_player = players[1] if #current_player.hand == 0 then print(current_player.name .. "赢了!") break end print(current_player.name .. "出牌:") local current_hand = {} if #last_hand == 0 then -- 首轮出牌 current_hand = current_player.hand else -- 跟牌 local valid = false while not valid do current_hand = {} print("跟牌:") print_hand(last_hand) print("请选择要出的牌:") local input = io.read() local selected = {} for name in string.gmatch(input, "[^%s]+") do for i, card in ipairs(current_player.hand) do if card.name == name then table.insert(selected, table.remove(current_player.hand, i)) break end end end if #selected == #last_hand then -- 选中的牌数量必须等于上家出的牌数量 table.sort(selected, function(a, b) return a.value < b.value end) local type, data = get_type(selected) if type ~= nil and compare(selected, last_hand) then -- 牌型合法且大于上家出的牌 current_hand = selected valid = true else print("出牌不合法!") for _, card in ipairs(selected) do -- 出牌不合法,将选中的牌放回手牌 table.insert(current_player.hand, card) end end else print("出牌不合法!") for _, card in ipairs(selected) do -- 出牌不合法,将选中的牌放回手牌 table.insert(current_player.hand, card) end end end end table.sort(current_hand, function(a, b) return a.value < b.value end) print(current_player.name .. "出的牌:") print_hand(current_hand) last_hand = current_hand table.remove(players, 1) table.insert(players, current_player) end end
-- 开始游戏 play()
原文地址: https://www.cveoy.top/t/topic/kmM 著作权归作者所有。请勿转载和采集!