Skip to content

Latest commit

 

History

History
31 lines (26 loc) · 758 Bytes

finish_guess_the_number_game.md

File metadata and controls

31 lines (26 loc) · 758 Bytes

Description

Imagine you are creating a game where the user has to guess the correct number. But there is a limit of how many guesses the user can do.

  • If the user tries to guess more than the limit, the function should throw an error.
  • If the user guess is right it should return true.
  • If the user guess is wrong it should return false and lose a life.

Can you finish the game so all the rules are met?

My Solution

class Guesser
  def initialize(number, lives)
    @number = number
    @lives = lives
  end

  def guess(n)
    raise error if @lives.zero?
    if n != @number
      @lives -= 1
      false
    else
      true
    end
  end
end