I’m trying to make a slot machine game in Ruby and this is as far as I’ve gotten. It still won’t run and says there is an error on the last line, which I do not know what is or how to fix it.
I need it to have an output similar to this:
How much total money would you like to play with today? 25
Total cash: $ 25
How much would you like to bet? 10
Cherry – Orange – Orange
You have won $ 20
Would you like to continue? (yes to continue) yes
Total cash: $ 35
How much would you like to bet?
etc…
I’ve already got the winnings set on there, like if you get two, you win twice your bet and if you get three, you win three times your bet.
But I get the error: 33: syntax error, unexpected $end, expecting kEND cash += cash + winnings
What is wrong with my code, and how do I fix it?
def multiplier(s1, s2, s3)
if s1 == s2 and s2 == s3:
multiplier = 3
elsif s1 == s2 or s2 == s3 or s1 == s3:
multiplier = 2
else
multiplier = 0;
return multiplier
def main()
slotImageList = ['Cherry', 'Orange', 'Plum', 'Bell', 'Melon', 'Bar']
cash = gets
puts "How much total money would you like to play with today? " +cash
while True:
puts("Total cash: $", cash)
bet = gets
puts "How much would you like to bet? " +bet
cash = (cash - bet)
slotImage1 = slotImageList.sample
slotImage2 = slotImageList.sample
slotImage3 = slotImageList.sample
puts "slotImage1", " - ", "slotImage2", " - ", "slotImage3"
winnings = bet * multiplier(slotImage1, slotImage2, slotImage3)
puts "You have won $" +winnings
cash = cash + winnings
cont = gets
puts "Would you like to continue? (yes to continue) " +cont
if cont != "yes":
puts "You have ended with $" +cash
else
puts " "
end
When you see the message:
you can translate it to, “I reached the end-of-file (“$end”), but I did not expect it, because I was still waiting to see an
endstatement.” It means that you forgot to type at least one pairedend, and you need to go through your code and ensure that it’s indented properly so that you can visually match up the statements.Below is the result of fixing your code to be correct.; in some spots you seemed to have used indentation to close a block (like Python) instead of the correct syntax.
If I were to take a few more liberties with it, here’s how I might write it: