2

How can I make the following loop end when the user writes stop, otherwise if they answer correctly how can it call the method again so that the number to be guessed is different? The idea of the game is that the user tries to get the number from the class, if they get it correctly then the game asks if they want to guess a new number generated by the class or if they want to stop;if so they write Stop and the game would end. Thanks in advance for the help

class NumberGuessingGame
    #clase NumberGuessingGame
    def initialize
        #metodo que inicia
        @number= rand(0..9)
        #number es igual a un numero random entre 0 y 9
    end

def guess(numer)
        #metodo guess que dice que hay una condicion dada por el usuario, si no se da entonces se pide que el usuario la escriba manualmente
        if numer<@number
            #si el numero es mas pequeño que el numero entonces "Too low"
            "Too low"
        elsif numer>@number 
            #si el numero es mayor a el numero entonces "too high"
            "Too high"
        elsif numer == @number
            #si el numero es igual al numero random que pone la computadora entonces "you got it!"
            "you got it!"
        end
    end
end


game = NumberGuessingGame.new
# Pruebas
a = ""
p "Welcome to Guess the Number"
p "Human VS Machine"
while a != "Stop"
    x = ""
while x != "you got it!" 
    p"Write a number between 0 and 9"
    y = gets.chomp.to_i
    p x = game.guess(y)
end
p "WOOOOW!! Very Impresive. Want to defeat the machine again? If not write 
  stop or guess the new number"
  NumberGuessingGame
  a = gets.chomp
end
  • Check this https://stackoverflow.com/questions/1402757/how-to-break-out-from-a-ruby-block. Talks about breaking loops. – Wax Jun 14 '17 at 05:30
  • The normal approach is to use `loop do end` and then break out of the loop (`break` or `break x`) if all is well. In other words, assume that you will have to repeat unless it's not necessary to do so. – Cary Swoveland Jun 14 '17 at 05:43

1 Answers1

-2

Here is a code solution I produced for this problem. Try it out and see if it is to your liking:

https://gist.github.com/BKSpurgeon/1a2346e278836d5b4448424cb93fd0e9

 class NumberGuessingGame    

    def initialize
        welcome
    end

    def welcome
        puts "Welcome to Guess the Number \n Human VS Machine"         
    end

 def guess 
    loop do       
        @number= rand(0..9)
        p "Write a number between 0 and 9"
        numer = -1
        while numer != @number
            numer = gets.chomp.to_i            
            if numer<@number                
                p "Too low"
            elsif numer>@number                 
                p "Too high"
            elsif numer == @number                
                p "You got it!"
                puts "WOOOOW!! Very Impresive. Want to defeat the machine again? If not write stop otherwise press any key."
                break
            end
        end
        answer = gets.chomp # The Critical Line to break out of the Loop is here:
        break if answer.downcase == "stop"
    end


 end
end

and you'd call it like this:

g = NumberGuessingGame.new
g.guess

that escapes if you write 'stop'. i made minor modifications of the functionality. The loop is broken if the "stop" answer is detected. that is the critical line.

I can't see a good reason for client code to be doing this type of thing:

game = NumberGuessingGame.new
# Pruebas
a = ""
p "Welcome to Guess the Number"
p "Human VS Machine"
while a != "Stop"
    x = ""
while x != "you got it!" 
    p"Write a number between 0 and 9"
    y = gets.chomp.to_i
    p x = game.guess(y)
end
p "WOOOOW!! Very Impresive. Want to defeat the machine again? If not write 
  stop or guess the new number"
  NumberGuessingGame
  a = gets.chomp
end

ideally you want to let all the methods in a class to do all the work - you should not have to write 'welcome to the game etc' from outside the class - that should be the responsibility of the NumberGuessingGame class.

hope this helps.

BenKoshy
  • 33,477
  • 14
  • 111
  • 80