--随机开奖颜色 function gamelogic:OpenResult() --红白颜色的次数 local tRed = 0 local tWhite = 0 local Result = {} for i = 1, 4, 1 do --随机开1或2 1代表红 2代表白 local RandNum = math.random(1,2) table.insert(Result, RandNum) if RandNum %2 == 0 then tRed = tRed + 1 else tWhite = tWhite + 1 end end return tRed, tWhite end --开奖区域 function gamelogic:OpenCardArry(nRed, nWhite) local AreaWinLose = 0 local nMultiple = 0 if nRed %2 == 0 or nWhite %2 == 0 then AreaWinLose = 1 end if nRed %2 ~= 0 or nWhite %2 ~= 0 then AreaWinLose = 2 end if nRed == 4 then AreaWinLose = 3 end if nRed == 4 then AreaWinLose = 4 end if nRed == 1 and nWhite == 3 then AreaWinLose = 5 end if nRed == 3 and nWhite == 1 then AreaWinLose = 6 end if AreaWinLose == 1 or AreaWinLose == 3 or AreaWinLose == 4 then nMultiple = 15 else nMultiple = 3 end return AreaWinLose, nMultiple end 有没有更简单的方法实现
时间: 2023-11-16 16:04:45 浏览: 84
可以使用一个二进制数字代表红白两种颜色的状态,比如红色为1,白色为0,那么四个数字组成的二进制数就可以代表开奖结果。然后根据这个二进制数的值,可以直接得出开奖区域和倍率。具体代码如下:
```
function gamelogic:OpenResult()
-- 生成一个4位二进制数代表开奖结果
local result = math.random(0, 15)
-- 计算红白颜色的次数
local tRed = 0
local tWhite = 0
for i = 1, 4 do
if bit32.band(result, 2^(i-1)) > 0 then
tRed = tRed + 1
else
tWhite = tWhite + 1
end
end
return tRed, tWhite, result
end
function gamelogic:OpenCardArry(nRed, nWhite, result)
-- 根据二进制数的值计算开奖区域和倍率
local AreaWinLose = 0
local nMultiple = 0
if result == 15 or result == 0 then
AreaWinLose = 1
elseif result == 1 or result == 14 or result == 2 or result == 13 or result == 4 or result == 11 or result == 8 or result == 7 then
AreaWinLose = 2
elseif result == 10 or result == 5 then
AreaWinLose = 3
elseif result == 3 or result == 12 then
AreaWinLose = 4
elseif result == 9 then
AreaWinLose = 5
elseif result == 6 then
AreaWinLose = 6
end
if AreaWinLose == 1 or AreaWinLose == 3 or AreaWinLose == 4 then
nMultiple = 15
else
nMultiple = 3
end
return AreaWinLose, nMultiple
end
```
注意,这里使用了 Lua 中的位运算函数 bit32.band,如果你的 Lua 版本不支持,可以使用一些其他方法实现。
阅读全文