r/lua • u/smellycheese08 • Apr 26 '25
I made a little rock paper scissors program inspired by a recent post
Idk why but seeing this post https://www.reddit.com/r/lua/s/eWJooyqrJZ Just gave me an itch to remake it, anyway:
local moves = {
rock = {rock = "Draw", paper = "Loss", scissors = "Win!"},
paper = {rock = "Win!", paper = "Draw", scissors = "Loss"},
scissors = {rock = "Loss", paper = "Win!", scissors = "Draw"}
}
local function GetRandMove(randNum)
local n = 0
local randNum = math.random(1, 3)
for k, _ in pairs(moves) do
n = n + 1
if randNum == n then return k end
end
end
local function sleep(n)
local t = os.time() + n
while os.time() < t do end
end
print([[
Time to Play:
--------
rock
paper
scissors
--------
]])
while true do
print("\nYour Move: ")
local PlayerInput = io.read()
local BotMove = GetRandMove()
print(BotMove, "\n")
sleep(.2)
print(moves[PlayerInput][BotMove])
sleep(.4)
::playagain::
print("\ntype y to play again or x to quit: \n")
local playAgain = io.read()
if playAgain == "x" then
print("Goodbye!")
break
elseif playAgain == "y" then
print("\nAlright!\n")
else
goto playagain
end
end
4
Upvotes
3
u/Bright-Historian-216 Apr 26 '25
here's my version. a bit less clunky i think. and definitely doesn't use goto.
2
u/Serious-Accident8443 Apr 26 '25
Nice. I can make it shorter and remove the conditional code if you like. ;)
2
u/[deleted] Apr 27 '25
I was wondering why I was smelling smoke.
2
u/[deleted] Apr 27 '25 edited Apr 27 '25
I went crazy and uhh...
Code
1
u/CirnoIzumi Apr 26 '25
Any reason you don't just do moves[randNum]? Iirc even dictionaries still have numerical indexes
1
u/AutoModerator Apr 26 '25
Hi! Your code block was formatted using triple backticks in Reddit's Markdown mode, which unfortunately does not display properly for users viewing via old.reddit.com and some third-party readers. This means your code will look mangled for those users, but it's easy to fix. If you edit your comment, choose "Switch to fancy pants editor", and click "Save edits" it should automatically convert the code block into Reddit's original four-spaces code block format for you.
I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.
1
u/Significant-Season69 Apr 27 '25
Why do you need to make it complicated if you can make an easy one but the same?
1
u/Isogash Apr 27 '25