This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
Tusillody's profile picture

[Lua] Need help with a game script

Started by Tusillody, 03 April 2012 - 07:49 PM
Tusillody #1
Posted 03 April 2012 - 09:49 PM
Hello, im new to computercraft, I've only been using it for a couple days. Im also new to lua, but I think ive picked up pretty well for just starting. Ive made plenty of programs in computercraft, and found/fixed all of my own errors up until now. I have a program, with a built in chance game, that if won, unlocks a door beside it. The game works all the way up until the result is made. It will display the result correctly, but even if the result matches the guess made by the user, the door will not open, and the game acts as if the user guessed incorrectly. Below is the program. Ive left comments on the lines that I think are messed up, if anyone could help me, it'd be much appreciated.


print ("Hello")
print ("What is your name?")
Name = io.read()
print ("Welcome "..Name..".")
sleep(1)
print ("What would you like to do?")
sleep(1)
term.clear()
print ("============================")
print ("=======Please enter:========")
print ("======'Unlock door'=========")
print ("======'Send message'========")
print ("==='Try lucky door game'====")
print ("============================")
choice = io.read()
if choice == "Unlock door" then
print ("Please enter the password:")
pass = io.read()
if pass == "0568" then
redstone.setOutput("right", true )
else
print ("Incorrect password!")
end
elseif choice == "Send message" then
rednet.open("right")
print ("Please enter the message:")
message = io.read()
print ("Sending message…")
sleep(1)
rednet.broadcast(message)
print("Message sent!")
elseif choice == "Try lucky door game" then
print ("Please enter a number between two and twelve.")
guess = io.read()
print ("I will roll two dice, and if they add up to be your number, you win.")
sleep(1)
print ("Your guess is "..guess..".")
sleep(1)
print ("Rolling…")
sleep(1)
num1 = math.random(1,6)
print ("First roll is "..num1.."!")
sleep(1)
print ("Rolling again…")
sleep(1)
num2 = math.random(1,6)
print ("Second roll is "..num2.."!")
sleep(1)
result = num1 + num2
print ("Final result = "..result.."!")
if result == guess then – This is where the program messes up.
redstone.setOutput("left", true ) – Even when the two are equal, the door does not open,
print ("Congragulations!!") – And the congragulations message is not displayed.
sleep(3)
else
print ("Aww, bad luck..") – Only this is, as if the user guessed wrong.
end
end



As I said before, any help would be much appreciated.

~Tus
Cloudy #2
Posted 03 April 2012 - 09:52 PM
You should use code tags, but your problem is simple to solve. Basically, io.read() returns a string and you're trying to compare it with a number.

Change this line of code
guess = io.read()
to
guess = tonumber(io.read())

It will now work correctly.
Tusillody #3
Posted 03 April 2012 - 09:58 PM
You should use code tags, but your problem is simple to solve. Basically, io.read() returns a string and you're trying to compare it with a number.

Change this line of code
guess = io.read()
to
guess = tonumber(io.read())

It will now work correctly.

I see now why it didnt work, thank you so much :)/>/>
And Ill have to look up code tags, thank you again.

Im also trying to figure out how I could set the game part of the program to a function by itself, and be able to replay it. such as:

print ("Would you like to play again?")
print("y/n")
playchoice = io.read()
if playchoice == y then

Id like to be able to put the game after the then. But thank you for your help. :D/>/>
Cloudy #4
Posted 03 April 2012 - 10:08 PM
The easiest way would be to put your entire program in a while loop. So at the start, type "while true do" and at the end type "end". At the end of your code (before the end you just added), put similar like you already have:
print ("Would you like to play again?")
print("y/n")
playchoice = io.read()
if playchoice == "y" then
print("Restarting.")
term.clear()
term.setCursorPos(1,1)
else
break
end

Code tags are done by doing [code] round your text :)/>/>
Tusillody #5
Posted 03 April 2012 - 10:24 PM
The easiest way would be to put your entire program in a while loop. So at the start, type "while true do" and at the end type "end". At the end of your code (before the end you just added), put similar like you already have:
print ("Would you like to play again?")
print("y/n")
playchoice = io.read()
if playchoice == "y" then
print("Restarting.")
term.clear()
term.setCursorPos(1,1)
else
break
end

Code tags are done by doing
round your text :D/>/>

Before you replied, I pulled the game code from the program, and put it on a different computer as startup, and then at the end, asked to play again. If user enters y, then the computer restarts, thus restarting the entire game. If user enters anything but y, computer shuts down. :)/>/> Thanks for the help though :3
Cloudy #6
Posted 03 April 2012 - 11:34 PM
While that is a solution, I don't think restarting the entire computer is very elegant :)/>/> Glad you got that sorted though.