mirror of
https://github.com/coding-horror/basic-computer-games.git
synced 2025-12-23 07:29:02 -08:00
add 32 diamond in ruby
This commit is contained in:
45
32 Diamond/ruby/diamond.rb
Normal file
45
32 Diamond/ruby/diamond.rb
Normal file
@@ -0,0 +1,45 @@
|
||||
def intro
|
||||
print " DIAMOND
|
||||
CREATIVE COMPUTING MORRISTOWN, NEW JERSEY
|
||||
|
||||
|
||||
|
||||
FOR A PRETTY DIAMOND PATTERN,
|
||||
TYPE IN AN ODD NUMBER BETWEEN 5 AND 21? "
|
||||
end
|
||||
|
||||
def get_facets
|
||||
while true
|
||||
number = gets.chomp
|
||||
if /^\d+$/.match(number)
|
||||
return number.to_i
|
||||
end
|
||||
puts "!NUMBER EXPECTED - RETRY INPUT LINE"
|
||||
print "? "
|
||||
end
|
||||
end
|
||||
|
||||
def get_diamond_lines(facets)
|
||||
spacers = (facets - 1) / 2
|
||||
lines = [' ' * spacers + 'C' + ' ' * spacers]
|
||||
lines += (1...facets).step(2).to_a.map { |v|
|
||||
spacers -= 1
|
||||
' ' * spacers + 'CC' + '!' * v + ' ' * spacers
|
||||
}
|
||||
lines + lines[0..-2].reverse
|
||||
end
|
||||
|
||||
def draw_diamonds(lines)
|
||||
repeat = 60 / lines[0].length
|
||||
(0...repeat).each { lines.map { |l| l * repeat }.each { |l| puts l } }
|
||||
end
|
||||
|
||||
def main
|
||||
intro
|
||||
facets = get_facets
|
||||
puts
|
||||
lines = get_diamond_lines(facets)
|
||||
draw_diamonds(lines)
|
||||
end
|
||||
|
||||
main
|
||||
Reference in New Issue
Block a user