Skip to content

Instantly share code, notes, and snippets.

@kenpusney
Last active December 16, 2015 07:49
Show Gist options
  • Save kenpusney/5401717 to your computer and use it in GitHub Desktop.
Save kenpusney/5401717 to your computer and use it in GitHub Desktop.
#!/usr/bin/env ruby
class Guesser
attr_reader :guess
def initialize(num)
@num = num
@guess = rand num
@guessq = []
@guessq.push(@guess)
printf "I guess %d. \n", @guess
end
def reguess(sml)
loop{
if sml
guess_maybe = @guess + rand(10 - @guess)
else
guess_maybe = rand @guess
end
if @guessq.count(guess_maybe) != 0
next
end
@guessq.push(@guess = guess_maybe)
break
}
printf "Oops! Too #{ sml and "small" or "large" }" +
", I guess %d. \n", @guess
return @guess
end
end
loop {
rnd = rand 10
puts "Now the random num generated..."
guesser = Guesser.new 10
loop {
printf "Please guess[ 0 -- 9 ]: "
guess = guesser.guess
if guess == rnd
puts "Congratulations! You made it!!"
break
end
guesser.reguess(guess < rnd)
}
printf "Continue[Y/N]?"
if gets =~ /no?/i
puts "Bye!~"
break
end
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment