在Roblox上创建游戏,但我遇到的问题是确保每个人的目标都不是他们自己或已经将他们作为目标的人。
假设您有4个玩家随机分配了目标。我想防止这种结果。
玩家1定位到玩家2
Player2以Player3为目标
Player3以Player1为目标
Player4定位到Player4
或Player3定位到Player4
Player4定位到Player3
function aFunctionfdfsd()
local targetList = {} -- List of people(3+) who haven't been targeted
while myTarget == Me or myTarget == targetsMe do -- cant be yourself or exchanged
ranPlayer = math.random(1, #targetList)
myTarget = targetList[ranPlayer]
--HELP HERE ?
end
table.remove(targetList, targetList[ranPlayer])
end
答案 0 :(得分:0)
选项1)
创建目标列表的本地副本,然后从目标列表中删除目标播放器。 然后从简化的目标列表中选择一个目标。
local myTargetList = {}
for i,v in ipairs(targetList) do
if v != Me then
table.insert(myTargetList, v)
end
end
-- pick target from myTargetList instead of targetList
选项2)
随机选择一个目标,直到该目标不是目标玩家为止。
答案 1 :(得分:0)
一种可靠的方法可能只是使每个玩家偏移固定的数量,以为他们提供目标。
圆1)偏移= 1
玩家1以玩家2为目标
播放器2以播放器3为目标
播放器3以播放器4为目标
播放器4以播放器1为目标
第2轮偏移量= 2
播放器1定位到播放器3
播放器2以播放器4为目标
播放器3以播放器1为目标
播放器4以播放器2为目标
等等。
一旦偏移等于玩家人数,您只需将其重置为1。而且,如果您有玩家加入和离开,那么玩家将很难发现模式。或者,您也可以只在每轮比赛中随机播放一次球员名单。
-- keep track of the current offset
local offset = 1
-- get a mapping of players to targets
local function getTargetsForRound()
local targets = {}
local playerList = game.Players:GetPlayers()
for i, player in ipairs(playerList) do
-- calculate the target index and correct for 1-based arrays
local targetIndex
if i + offset > #playerlist then
targetIndex = (i + offset) % #playerList
else
targetIndex = i + offset
end
targets[ player.Name ] = playList[ targetIndex ].Name
end
-- increment the offset for next time
offset = offset + 1
if offset == #playersList then
offset = 1
end
end
-- at the start of every round, fetch the next targets
local t = getTargetsForRound()
for playerName, targetName in pairs(t) do
print( string.format("%s's target is %s", playerName, targetName )
end
答案 2 :(得分:0)
我决定在解决方案中使用一些递归和随机性。
在下面的代码中,您输入玩家名称列表(即game.PLayers:getChildren()
),然后该代码将随机将其与其他玩家匹配。它会进行检查以确保他们无法匹配自己或已经被攻击的目标,并且如果您剩下最后一个没有选择目标的家伙,它将自动获得新列表。
由于我使用的种子确实是随机的,因此您不必担心会陷入递归循环中,因为下一个循环总是不同的。
function stayOnTarget(players)
math.randomseed(tonumber(tostring({}):sub(8))) --Very reliable random seed
local availableTargets = {}
local choosenTargets = {}
--Duplicating players array without linking them
for _, player in pairs(players) do
table.insert(availableTargets, player)
end
for i, player in pairs(players) do
--If the last player is left with themselves, try again
if i == #players and availableTargets[1] == player then
return stayOnTarget(players)
end
--Pick a target that is not themselves
local targetIndex = math.random(1, #availableTargets)
local target = availableTargets[targetIndex]
while target == player do
targetIndex = math.random(1, #availableTargets)
target = availableTargets[targetIndex]
end
--Store target and remove them from available targets
choosenTargets[player] = target
table.remove(availableTargets, targetIndex)
end
return choosenTargets
end
local results = stayOnTarget({"User1", "User2", "User3", "User4"})
--Print target matches
for i, v in pairs(results) do
print(i .. " = " .. v)
end
最后,将返回一个字典,如果您要查看结果,我已经设置了外观以打印结果。如果您有任何问题,请发表评论。