Skip to main content
added 1375 characters in body
Source Link

Additionally, if you didn't care about the order of the information prompted for either you could do away with storing that information in the sub-tables of choicetab and do something like this instead:

local choicetab = {
    Health             = function(args) return 70 * ((args["Average Damage"] * args.Size) / args.Speed)   end,
    Size               = function(args) return (args.Health * args.Speed) / (70 * args["Average Damage"]) end,
    Speed              = function(args) return (70 * args["Average Damage"] * args.Size) / args.Health    end,
    ["Average Damage"] = function(args) return (args.Health * args.Speed) / (70 * args.Size)              end,
}

print("Choose what you want to find:")
for name in pairs(choicetab) do
    print(name)
end
print()
local chosen = io.read()
local fun = choicetab[chosen]
if not fun then
    error("Not a valid choice")
end

local argtab = {}
for name, v in pairs(choicetab) do
    if name ~= chosen then
        print("Input The "..name)
        argtab[name] = io.read()
    end
end
print(fun(argtab))

But that depends on the set of choices always being one more than the arguments that the calculation functions need which may or may not be a valid assumption in any broader case.

Additionally, if you didn't care about the order of the information prompted for either you could do away with storing that information in the sub-tables of choicetab and do something like this instead:

local choicetab = {
    Health             = function(args) return 70 * ((args["Average Damage"] * args.Size) / args.Speed)   end,
    Size               = function(args) return (args.Health * args.Speed) / (70 * args["Average Damage"]) end,
    Speed              = function(args) return (70 * args["Average Damage"] * args.Size) / args.Health    end,
    ["Average Damage"] = function(args) return (args.Health * args.Speed) / (70 * args.Size)              end,
}

print("Choose what you want to find:")
for name in pairs(choicetab) do
    print(name)
end
print()
local chosen = io.read()
local fun = choicetab[chosen]
if not fun then
    error("Not a valid choice")
end

local argtab = {}
for name, v in pairs(choicetab) do
    if name ~= chosen then
        print("Input The "..name)
        argtab[name] = io.read()
    end
end
print(fun(argtab))

But that depends on the set of choices always being one more than the arguments that the calculation functions need which may or may not be a valid assumption in any broader case.

added 352 characters in body
Source Link

Possibly not exactly how I'd write it if I was writing this myself but given the example code and the lack of any other context it works and is reasonably clean.

local choicetab = {
    Health = {"Damage", "Size", "Speed", cb = function(Da, Sz, Sp) return 70 * ((Da * Sz) / Sp) end},
    Size = {"Health", "Damage", "Speed", cb = function(h, Da, Sp) return (h * Sp) / (70 * Da) end},
    Speed = {"Health", "Damage", "Size", cb = function(h, Da, Sz) return (70 * Da * Sz) / h end},
    ["Average Damage"] = {"Health", "Size", "Speed", cb = function(h, Sz, Sp) return (h * Sp) / (70 * Sz) end},
}

print([[Choose what you want to find:
Health
Size
Speed
Average Damage
]])
local chosen = io.read()
local tab = choicetab[chosen]
if not tab then
    error("Not a valid choice")
end

local argtab = {}
for i, v in ipairs(tab) do
    print("Input The "..v)
    argtab[i] = io.read()
end
print(tab.cb(unpack(argtab)))

This might be better for the choice printing loop if you aren't concerned about the order possibly changing (it should be stable but isn't guaranteed to be) since it shows you all the valid choices (even if you remove one or add more).

print("Choose what you want to find:")
for name in pairs(choicetab) do
    print(name)
end

Possibly not exactly how I'd write it if I was writing this myself but given the example code and the lack of any other context it works and is reasonably clean.

local choicetab = {
    Health = {"Damage", "Size", "Speed", cb = function(Da, Sz, Sp) return 70 * ((Da * Sz) / Sp) end},
    Size = {"Health", "Damage", "Speed", cb = function(h, Da, Sp) return (h * Sp) / (70 * Da) end},
    Speed = {"Health", "Damage", "Size", cb = function(h, Da, Sz) return (70 * Da * Sz) / h end},
    ["Average Damage"] = {"Health", "Size", "Speed", cb = function(h, Sz, Sp) return (h * Sp) / (70 * Sz) end},
}

print([[Choose what you want to find:
Health
Size
Speed
Average Damage
]])
local chosen = io.read()
local tab = choicetab[chosen]
if not tab then
    error("Not a valid choice")
end

local argtab = {}
for i, v in ipairs(tab) do
    print("Input The "..v)
    argtab[i] = io.read()
end
print(tab.cb(unpack(argtab)))

Possibly not exactly how I'd write it if I was writing this myself but given the example code and the lack of any other context it works and is reasonably clean.

local choicetab = {
    Health = {"Damage", "Size", "Speed", cb = function(Da, Sz, Sp) return 70 * ((Da * Sz) / Sp) end},
    Size = {"Health", "Damage", "Speed", cb = function(h, Da, Sp) return (h * Sp) / (70 * Da) end},
    Speed = {"Health", "Damage", "Size", cb = function(h, Da, Sz) return (70 * Da * Sz) / h end},
    ["Average Damage"] = {"Health", "Size", "Speed", cb = function(h, Sz, Sp) return (h * Sp) / (70 * Sz) end},
}

print([[Choose what you want to find:
Health
Size
Speed
Average Damage
]])
local chosen = io.read()
local tab = choicetab[chosen]
if not tab then
    error("Not a valid choice")
end

local argtab = {}
for i, v in ipairs(tab) do
    print("Input The "..v)
    argtab[i] = io.read()
end
print(tab.cb(unpack(argtab)))

This might be better for the choice printing loop if you aren't concerned about the order possibly changing (it should be stable but isn't guaranteed to be) since it shows you all the valid choices (even if you remove one or add more).

print("Choose what you want to find:")
for name in pairs(choicetab) do
    print(name)
end
Source Link

Possibly not exactly how I'd write it if I was writing this myself but given the example code and the lack of any other context it works and is reasonably clean.

local choicetab = {
    Health = {"Damage", "Size", "Speed", cb = function(Da, Sz, Sp) return 70 * ((Da * Sz) / Sp) end},
    Size = {"Health", "Damage", "Speed", cb = function(h, Da, Sp) return (h * Sp) / (70 * Da) end},
    Speed = {"Health", "Damage", "Size", cb = function(h, Da, Sz) return (70 * Da * Sz) / h end},
    ["Average Damage"] = {"Health", "Size", "Speed", cb = function(h, Sz, Sp) return (h * Sp) / (70 * Sz) end},
}

print([[Choose what you want to find:
Health
Size
Speed
Average Damage
]])
local chosen = io.read()
local tab = choicetab[chosen]
if not tab then
    error("Not a valid choice")
end

local argtab = {}
for i, v in ipairs(tab) do
    print("Input The "..v)
    argtab[i] = io.read()
end
print(tab.cb(unpack(argtab)))