如何确定列表中是否存在项目?

我做了一个小游戏,想要检查一个玩家是否已经在“禁止列表”中。如果存在多个名称,我该如何处理?

例如,我有一个像这样的玩家列表:

PlayerList = {'Player 1', 'Player 2', 'Player 3'}

我希望能够禁止一些玩家并将他们添加到禁止列表中,以防止他们在游戏中做一些事情。我可以像这样处理一个玩家名称:

if (Player_Name ~= 'Player 2') then
print('Hello!!')
else
print('You are banned!')
end

但这只对“Player 2”有效。如何添加多个名称?

我不想使用“or”像这样:

if (Player_Name ~= 'Player 2' or Player_Name ~= 'Player 3') then
print('Hello!!')
else
print('You are banned!')
end

因为我的列表可能包含超过200个,我不想添加超过200个“or”。如何简单地检查我创建的禁止列表中是否包含玩家?例如:

BanList = {'Player 2', 'Player 3'}

也许像这样(当然这不起作用):

if (Player_Name ~= BanList) then
print('Hello!!')
else
print('You are banned!')
end
点赞
用户501459
用户501459

你想使用循环遍历 BanList 中的所有项目,查看其中是否包含该玩家。

BanList = { 'Player 2', 'Player 3' }

function isBanned(playerName)
    for _,name in pairs(BanList) do
        if name == playerName then
            return true
        end
    end
    return false
end

if isBanned(Player_Name) then
    print('你被禁止了!')
else
    print('你好!')
end
2014-03-27 21:25:24