Bheki Fuzwayo
Bheki Fuzwayo

Reputation: 1

How to get my number of attempts correct?

Hello all I previously asked how to fix a guessing number game on ruby which I got all done but now the number of attempts or tries is not reflecting nicely. Do I resolve this?

This is the code:

def check(int, r_int)
    tries = 0
    if int < r_int
      tries +=1
      puts "Guess Higher"
    elsif int > r_int
      tries +=1
      puts "Guess Lower"
    elsif int == r_int
      win = true
      puts "You are correct"
      puts "You had attempted this "+ tries.to_s + " times to win"
      abort
    end
end

This is the main function:

def main

win = false

puts "Lets play a game!"
puts "I am thinking of a number between 1 and 100"
rnd_int = rand(100)

while not win
    guess = gets.chomp.to_i
    value = check(guess, rnd_int)
end

end

Overall everything works but the tries at the end of the game remains at 0 not sure where the mistake is.

Upvotes: 0

Views: 95

Answers (1)

Polar Bear
Polar Bear

Reputation: 6808

Perhaps the code could look like following (just a suggestion)

def check(attempts,guess,number)
    puts ">>> Attempts [#{attempts}]: guess higher" if guess < number
    puts ">>> Attempts [#{attempts}]: guess lower"  if guess > number
    if guess == number
        puts "Winner!!!"
        return true
    end
    return false
end


if __FILE__ == $0
    win = false
    attempts = 5
    
    puts "
        Lets play a game!
        I am thinking of a number between 1 and 100
    "
    
    number = rand(100)
    
    while not win
        print "Your guess: "
        guess = gets.chomp.to_i
        attempts -= 1
        unless attempts
            puts "You could not guess right"
            exit
        end
        win = check(attempts, guess, number)
    end
end

Output sample


                Lets play a game!
                I am thinking of a number between 1 and 100

Your guess: 50
>>> Attempts [4]: guess higher
Your guess: 80
>>> Attempts [3]: guess higher
Your guess: 90
>>> Attempts [2]: guess lower
Your guess: 86
Winner!!!

The code could be of following shape

win = false
attempts = 5

puts "
    Lets play a game!
    I am thinking of a number between 1 and 100
"
number = rand(100)

while not win
    print "Your guess: "
    guess = gets.chomp.to_i
    attempts -= 1
    score = guess <=> number
    win = true if score == 0
    puts "Attempts [#{attempts}]: guess higher" if score < 0
    puts "Attempts [#{attempts}]: guess lower"  if score > 0
    unless attempts > 0
        puts "You could not guess it right"
        exit
    end
end

puts "You are winner!!!"

Utilizing <=> operator the code would be shaped as following

attempts = 5

puts "
    Lets play a game!
    I am thinking of a number between 1 and 100

"
number = rand(100)

while true
    print "Your guess: "
    guess = gets.chomp.to_i
    attempts -= 1
    case guess <=> number
    when 0
        puts "\n>>> Nice guess, you are winner!!!"
        exit
    when -1
        puts "Attempts [#{attempts}]: guess higher" if attempts > 0
    when 1  
        puts "Attempts [#{attempts}]: guess lower"  if attempts > 0
    end
    unless attempts > 0
        puts "\n>>> Sorry, you could not guess it right"
        exit
    end
end

Upvotes: 1

Related Questions