r/lua • u/OddToastTheIII • Apr 26 '25
is there a way i can make my script smaller?
i'm new to lua i just made a rock paper scissors game but i want to make the script smaller is there a way i can do that?math.randomseed(os.time())
table = {"rock", "paper", "scissors"}
number = math.random(1, 3)
print ("Welcome to Rock Paper Scissors!\nChoose Your Move!")
move = string.lower(io.read("*l"))
if move == "rock" then
`if number == 1 then`
`print("Tie!")`
`elseif number == 2 then`
`print ("You Lose!")`
`else`
`print ("You Win!")`
`end`end
if move == "paper" then
`if number == 1 then`
`print("You Win!")`
`elseif number == 2 then`
`print ("Tie!")`
`else`
`print ("You Lose!")`
`end`end
if move == "scissors" then
`if number == 1 then`
`print("You Lose!")`
`elseif number == 2 then`
`print ("You Win!")`
`else`
`print ("You Lose!")`
`end`end
print (table[number])
4
Upvotes
8
u/kcx01 Apr 26 '25
Do not assign variables to reserved names. Use tbl instead of table, or even better be more descriptive
1
u/TomatoCo Apr 26 '25
Why do you want to make it smaller?
2
u/kcx01 Apr 26 '25
One way you could shorten it is by catching the draw before you check anything else, and you can use the number to infect the tbl that you set.
So it might look something like this:
2
u/Significant-Season69 Apr 26 '25
math.randomseed(os.time())
table = {"rock", "paper", "scissors"}
number = math.random(1, 3)
print ("Welcome to Rock Paper Scissors!\nChoose Your Move!")
move = string.lower(io.read("*l"))
local moves = { "rock", "paper", "scissors" }
local counters = { rock = { rock = "Tie", paper = "Lose", scissors = "Win" }, paper = { rock = "Win", paper = "Tie", scissors = "Lose" }, scissors = { rock = "Lose", paper = "Win", scissors = "Tie" } }
local botMove = moves[number]
print("You: "..move) print("Bot: "..botMove)
local result = counters[move] and counters[move][botMove]
if result == nil then print("Invalid move!") return end
print(result == "Win" and "You win!" or result == "Lose" and "You lose!" or "Tie!")
1
u/Isogash Apr 26 '25 edited Apr 26 '25
I mean, if nothing else, you don't need separate branches for each input. You could ignore the user input and just output "win" "tie" or "draw" with equal random chance and it would be mathematically equivalent.
Assuming that you may actually need to compare real rock paper scissors entries e.g. between two human players, then you could make the code more compact but it would also make it harder to understand. This approach is actually nice for reading, even if it's more verbose than strictly necessary.
One way of doing this mathematically is to convert both inputs to a number, subtract one from the other and modulo the result by 3 ((a - b) % 3). 3 would be a tie, 2 would be b's victory and 1 would be a's victory.
1
u/Serious-Accident8443 Apr 26 '25 edited Apr 26 '25
Someone else asked me about programming without conditionals so I took the liberty of using this as an example of preferring data over using conditional code:
1
u/xoner2 Apr 26 '25 edited Apr 26 '25
First you notice the if's are similar and refactor to function:
Then you notice the parameters to the function is uniform and can be encoded as a table:
And then you notice it can be all data:
But this is quite confusing and the function+lookup version is best.
1
u/notpeter Apr 26 '25
I thought this would be a fun little learning exercise. Your stated goal of "make the script smaller" has potentially three dimensions: number of lines of source, number of bytes of source, number of bytes of lua bytecode.
I decided to track the steps to improving your script.
3-5. Additional polish you did not ask for.
Final version:
1
u/yyywwwxxxzzz Apr 27 '25
1
u/Stef0206 Apr 28 '25 edited Apr 28 '25
You could use the fact that in your table each move is beat by the next table entry.
This way you map the different win messages to the difference between yours and the computer’s choice.
0 difference means you chose the same move. 1 difference means you won. 2 difference means you lose.