Spoiler: show
- Code: Select all
--
function joinMyTables(t1, t2)
for k,v in ipairs(t2) do
table.insert(t1, v)
end
return t1
end
function round(num, idp)
local mult = 10^(idp or 0)
return math.floor(num * mult + 0.5) / mult
end
local function permutation(a, n, cb)
if n == 0 then
cb(a)
else
for i = 1, n do
a[i], a[n] = a[n], a[i]
print(i)
permutation(a, n - 1, cb)
a[i], a[n] = a[n], a[i]
end
end
end
--Usage
local function callback(a, i)
print('{'..table.concat(a, ', ')..'}')
--
local TotalA = a[1]+a[2]
local TotalB = a[3]+a[4]
local Total = TotalA+TotalB
local PurcentA = ((TotalA)*100)/Total
local PurcentB = ((TotalB)*100)/Total
local PurcentRatio = ''
if TotalA < TotalB then
PurcentRatio = (TotalA/TotalB)*100
else
PurcentRatio = (TotalB/TotalA)*100
end
if maxx < PurcentRatio then
maxx = PurcentRatio
--max_index = i -- PROBLEM HERE, 'i' return Nil
end
--
print('TotalA : '..TotalA..' / TotalB : '..TotalB)
print('Total : '..Total)
print('A : '..round(PurcentA)..'% / B : '..round(PurcentB)..'%')
print('Ratio : '..round(PurcentRatio)..'%')
print('')
end
--
local tableA = {} -- Team A
local tableB = {} -- Team B
local number = 4 -- Number of Player
maxx = 0 -- Max rating
max_index = '' -- Index du meilleur rating
i = 0
tableA[0] = {10, 2} -- DEV
tableA[1] = {'A', 'B'} -- PlayerName
tableB[0] = {5, 5} -- DEV
tableB[1] = {'C', 'D'} -- PlayerName
local tableAB = {}
tableAB[0] = joinMyTables(tableA[0], tableB[0]) -- ALL DEV
tableAB[1] = joinMyTables(tableA[1], tableB[1]) -- ALL PlayerName
--
permutation(tableAB[0], number, callback)
print('Meilleur rating : '..round(maxx, 2)..'%')
print('Index du meilleur rating : '..max_index..'NEED INFO HERE')
--
You can test my script here : http://www.compileonline.com/execute_lua_online.php