Last active
August 29, 2015 14:19
-
-
Save erlinis/b3b49cfc5d075cfb8ece to your computer and use it in GitHub Desktop.
Script para jugar Punto y Fama contra la "máquina"
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
# Punto y fama | |
# Created by: Erlinis Quintana | |
# Date: April 19 2015 | |
def print_instructions | |
puts """ | |
Instructions: Type a four digits number, without repeat any digits, | |
until you guess the random number created by the computer. | |
After you entered the number you will get a hint, where: | |
1. Each 'F' (Fame) means than one number exists and is located in the right position. | |
2. Each '.' (Point) means than one number exists but is misplaced. \n | |
""" | |
end | |
def generate_random_number | |
digits = (0..9).to_a | |
array_number = [] | |
while (array_number.length < 4) do | |
random_index = 0 + Random.rand(9) | |
if (digits[random_index] != nil) then | |
array_number[array_number.length] = digits[random_index] | |
digits[random_index] = nil | |
end | |
end | |
array_number.join('') | |
end | |
@attempts = 0 | |
@number = generate_random_number | |
@array_number = @number.to_s.split('') | |
@historial = Hash.new | |
def read_guess_number | |
@attempts += 1 | |
print "Enter your guess #{@attempts}: " | |
guess_number = gets.chomp.strip | |
guess_number = (guess_number.length > 4) ? guess_number[0, 4] : guess_number | |
end | |
def evaluate_fame(guess_number) | |
fames = "" | |
new_array_guess = {} | |
array_guess = guess_number.to_s.split('') | |
for i in 0..3 | |
if (@array_number[i] == array_guess[i]) then | |
fames += "F " | |
new_array_guess[i] = nil | |
else | |
new_array_guess[i] = array_guess[i] | |
end | |
end | |
return [fames, new_array_guess] | |
end | |
def evaluate_point(array_guess) | |
points = "" | |
for i in 0..3 | |
if (@array_number.include?(array_guess[i]) ) then | |
points += ". " | |
end | |
end | |
return points | |
end | |
def evaluate_guess_number(guess_number) | |
fames, new_array_guess = evaluate_fame(guess_number) | |
points = evaluate_point(new_array_guess) | |
@historial[guess_number] = "#{fames}#{points}" | |
end | |
def print_historial | |
unless @historial.empty? then | |
puts "------------------------" | |
@historial.each { |key, value| | |
puts " #{key} > #{value} " | |
} | |
puts "------------------------" | |
puts "\n" | |
end | |
end | |
print_instructions | |
guess_number = 0 | |
while !(guess_number == @number.to_s) do | |
guess_number = read_guess_number | |
evaluate_guess_number(guess_number) | |
print_historial | |
end | |
puts "Congratulations, you rock! The number was #{@number}" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment