-
-
Save TrevorS/a7892e044337bd6c8de8 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 | |
# encoding: utf-8 | |
random = Random.new | |
code = '' | |
4.times { code << random.rand(1..6).to_s } | |
puts %q( | |
Can you guess the code? | |
You have 10 guesses. | |
The code is 4 digits between 1 and 6. | |
-: No match x: Wrong spot o: Match | |
) | |
10.times do | |
print 'Enter your guess: ' | |
guess = gets.chomp | |
while !guess.match(/^[1-6]{4}$/) | |
puts "Your guess should be 4 digits between 1 and 6. Try again." | |
guess = gets.chomp | |
end | |
response = '' | |
guess.split('').each_with_index do |c, i| | |
if c == code[i] | |
response << 'o' | |
elsif code.match(c) | |
response << 'x' | |
else | |
response << '-' | |
end | |
end | |
puts response | |
if response == 'oooo' | |
puts "You win!" | |
exit | |
end | |
end | |
puts "You lose!" | |
puts "The code was #{code}." |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment