Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # encoding: utf-8
- #!/usr/bin/env ruby
- n = ARGV.first.to_i
- $dirs = [:up, :right, :down, :left]
- $cur_i = 0
- $cur_j = 0
- $mat = Array.new(n) { Array.new(n) { ' ' } }
- # puts $mat.inspect
- def print_matrix
- $mat.each do |line|
- puts line.join
- end
- end
- def turn(cur_dir)
- idx = ($dirs.index(cur_dir) + 1) % $dirs.length
- $dirs[idx]
- end
- def move(dir)
- case dir
- when :up
- return $cur_i-1, $cur_j
- when :right
- return $cur_i, $cur_j+1
- when :down
- return $cur_i+1, $cur_j
- when :left
- return $cur_i, $cur_j-1
- else
- return $cur_i, $cur_j
- end
- end
- def make_line(dir, n)
- n.times do |i|
- $mat[$cur_i][$cur_j] = '*'
- # print_matrix
- $cur_i, $cur_j = move(dir) unless i+1 == n
- end
- end
- direction = :right
- make_line(direction, n)
- n.downto(2) do |i|
- direction = turn(direction)
- make_line(direction, i)
- end
- print_matrix
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement