HiveBrain v1.2.0
Get Started
← Back to all entries
patternMinor

Lua Formula Code

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
codeformulalua

Problem

A friend of mine dabbles in lua, and he recently sent me a code which applies an equation we were discussing. I personally am very mediocre with lua, but my Python senses are tingling - I feel as though this code could almost certainly be shortened. Any thoughts?

print("Choose what you want to find:")
print("Health")
print("Size")
print("Speed")
print("Average Damage")
print("")
chosen = io.read()
if chosen == 'Health' then
    print("Input The Damage")
    Da = io.read()
    print("Input The Size")
    Sz = io.read()
    print("Input The Speed")
    Sp = io.read()
    local m = 70 * ((Da * Sz) / Sp)
    print(m)
elseif chosen == 'Size' then
    print("Input the Health")
    h = io.read()
    print("Input the Damage")
    Da = io.read()
    print("Input the Speed")
    Sp = io.read()
    local n = (h*Sp) / (70*Da)
    print(n)
elseif chosen == 'Speed' then
    print("Input the Health")
    h = io.read()
    print("Input the Damage")
    Da = io.read()
    print("Input The Size")
    Sz = io.read()
    local o = (70*Da*Sz) / h
    print(o)
elseif chosen == 'Average Damage' then
    print("Input the Health")
    h = io.read()
    print("Input The Size")
    Sz = io.read()
    print("Input The Speed")
    Sp = io.read()
    local p = (h * Sp) / (70 * Sz)
    print(p)
end

Solution

I have never written any Lua code before, but a short view in a tutorial at http://www.lua.org/ let me come up with this:

Extract a method which returns 3 values:

print("Choose what you want to find:")
print("Health")
print("Size")
print("Speed")
print("Average Damage")
print("")
chosen = io.read()
if chosen == 'Health' then
    Da,Sz,Sp = read3values("Input The Damage","Input The Size","Input The Speed")
    print((70 * ((Da * Sz) / Sp)))
elseif chosen == 'Size' then
    h,Da,Sp= read3values("Input the Health","Input the Damage","Input the Speed")
    print(((h*Sp) / (70*Da)))
elseif chosen == 'Speed' then
    h,Da,Sz = read3values("Input the Health","Input the Damage","Input The Size")
    print(((70*Da*Sz) / h))
elseif chosen == 'Average Damage' then
    h,Sz,Sp = read3values"Input the Health","Input The Size","Input The Speed")
    print(((h * Sp) / (70 * Sz)))
end

function read3values(text1,text2,text3)
    local value1 = readValue(text1)
    local value2 = readValue(text1)
    local value3 = readValue(text1)
    return value1,value2,value3
end

function readValue(text)
    print(text)
    return io.read()
end


You may also want to extract the repeating string constants.

Code Snippets

print("Choose what you want to find:")
print("Health")
print("Size")
print("Speed")
print("Average Damage")
print("")
chosen = io.read()
if chosen == 'Health' then
    Da,Sz,Sp = read3values("Input The Damage","Input The Size","Input The Speed")
    print((70 * ((Da * Sz) / Sp)))
elseif chosen == 'Size' then
    h,Da,Sp= read3values("Input the Health","Input the Damage","Input the Speed")
    print(((h*Sp) / (70*Da)))
elseif chosen == 'Speed' then
    h,Da,Sz = read3values("Input the Health","Input the Damage","Input The Size")
    print(((70*Da*Sz) / h))
elseif chosen == 'Average Damage' then
    h,Sz,Sp = read3values"Input the Health","Input The Size","Input The Speed")
    print(((h * Sp) / (70 * Sz)))
end

function read3values(text1,text2,text3)
    local value1 = readValue(text1)
    local value2 = readValue(text1)
    local value3 = readValue(text1)
    return value1,value2,value3
end


function readValue(text)
    print(text)
    return io.read()
end

Context

StackExchange Code Review Q#44527, answer score: 8

Revisions (0)

No revisions yet.