从颜色数组中随机挑选4个颜色

我要如何从一个包含12种颜色的数组中选择四种随机颜色,然后储存这四种颜色,以便游戏可以告诉用户在记忆游戏中找到其中的一种。

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

游戏随机选择了R、Y、B、O并展示了它们

玩家需要找到的颜色不能是绿色,只能是随机选择的四种颜色。

我真的卡在这里了,任何帮助都将是很棒的。

点赞
用户4403144
用户4403144

以下是可能的其中一种方式:

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
2018-08-03 21:36:57