Last active
December 16, 2015 07:49
-
-
Save kenpusney/5401717 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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