Here is my code
$wins = 0
$plays = 0
$num = 0
$range = 0
$best = 5
$playAgain = true
class NumGame
def initialize()
@totalGuesses = 0
@i = 5
@guess = 0
end
def generate()
puts "Choose Number Range: 0 - "
$range=gets
$num=rand($range)
puts "Guess a Number from 0 - #$range"
end
def guess() {
until @i == 0
puts "Guess a Number: "
@guess = gets
@totalGuesses=@totalGuesses+1
@i=@i+1
if @guess>$range or @guess<0
puts "Guess out of range"
next
end
if @guess == $num
puts "Congrats! You guessed the number!"
$wins=$wins+1
if @totalGuesses < $best
$best=@totalGuesses
end
break
elsif @guess < $num
puts "Higher - #@i guesses left!"
else
puts "Lower - #@i guesses left!"
end
if @i == 0
puts "Sorry! You lost the game!"
end
end
$plays=$plays+1
end
def replay()
puts "You've played #$plays games and won #$wins games!"
puts "Your best game was #$best guesses!"
puts "Play Again? [y/n]"
@choice = gets
if @choice.start_with?("y")
playAgain=true
else
playAgain=false
end
end
end
game = NumGame.new
while playAgain == true
game.generate
game.guess
game.replay
end
Error is on the end
statement in the guess function. There are no hashes so I don’t understand why there is an error. Other articles don’t have good solutions for this type of program. Any help is greatly appreciated. Thanks.
I’ve tried adding the ==>
but that doesn’t work as there are no hashes in the code. I’ve tried using other Stack Overflow articles but none of them work.
>Solution :
The error is caused by the curly bracket in the def guess() {
line. Just remove the {
and this issue should be fixed.