Corona SDK 从一组颜色中随机选择 4 种颜色

corona sdk pick 4 random colors from an array of colors

如何 select 从 12 种颜色的数组中随机选择 4 种颜色,然后存储选择的 4 种颜色,以便游戏可以告诉用户从 4 种颜色中找到 1 种作为记忆游戏。

例如如果数组列表中有 R,G,B,Y,P,O 颜色

然后游戏选择了 R,Y,B,O 并显示它们

玩家需要寻找的颜色不能是绿色,例如只能是随机选择的4种颜色。

我真的很难弄清楚这个问题,任何帮助都会很棒

这是许多可能的方法之一:

allColors = {"black", "white", "red", "orange", "yellow", "green",
    "blue", "indigo", "violet", "gold", "silver", "bronze"}
chosenColors = {}

while #chosenColors < 4 do
    n = math.random(1, #allColors)
    table.insert(chosenColors, allColors[n])
    table.remove(allColors, n)
end

然后:

for k, v in pairs(chosenColors) do
    print(k, v)
end

这将打印,例如:

1 white
2 indigo
3 red
4 gold