Advertisement
vfonic

Untitled

Jun 22nd, 2015
232
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 0.89 KB | None | 0 0
  1. # encoding: utf-8
  2. #!/usr/bin/env ruby
  3.  
  4. n = ARGV.first.to_i
  5.  
  6. $dirs = [:up, :right, :down, :left]
  7. $cur_i = 0
  8. $cur_j = 0
  9. $mat = Array.new(n) { Array.new(n) { ' ' } }
  10.  
  11. # puts $mat.inspect
  12.  
  13. def print_matrix
  14.   $mat.each do |line|
  15.     puts line.join
  16.   end
  17. end
  18.  
  19. def turn(cur_dir)
  20.   idx = ($dirs.index(cur_dir) + 1) % $dirs.length
  21.   $dirs[idx]
  22. end
  23.  
  24. def move(dir)
  25.   case dir
  26.   when :up
  27.     return $cur_i-1, $cur_j
  28.   when :right
  29.     return $cur_i, $cur_j+1
  30.   when :down
  31.     return $cur_i+1, $cur_j
  32.   when :left
  33.     return $cur_i, $cur_j-1
  34.   else
  35.     return $cur_i, $cur_j
  36.   end
  37. end
  38.  
  39. def make_line(dir, n)
  40.   n.times do |i|
  41.     $mat[$cur_i][$cur_j] = '*'
  42.     # print_matrix
  43.     $cur_i, $cur_j = move(dir) unless i+1 == n
  44.   end
  45. end
  46.  
  47. direction = :right
  48. make_line(direction, n)
  49.  
  50. n.downto(2) do |i|
  51.   direction = turn(direction)
  52.   make_line(direction, i)
  53. end
  54.  
  55. print_matrix
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement