Last active
August 29, 2015 14:24
-
-
Save ChristopheBelpaire/d4c259c13ff6199fe0b6 to your computer and use it in GitHub Desktop.
Spiral with logo DSL
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
class Turtle | |
DIRECTIONS = [:UP, :RIGHT, :DOWN, :LEFT] | |
def initialize(size) | |
@size = size | |
@array = Array.new(size) | |
@array.map! {|line| line = Array.new(size, " ")} | |
@direction = :RIGHT | |
@x = 0 | |
@y = 0 | |
@array[@y][@x] = "*" | |
end | |
def walk(step_numbers) | |
step_numbers.times do | |
case @direction | |
when :UP | |
@y = @y - 1 if @y > 0 | |
when :DOWN | |
@y = @y + 1 if @y < @size | |
when :LEFT | |
@x = @x - 1 if @x > 0 | |
when :RIGHT | |
@x = @x + 1 if @x < @size | |
end | |
@array[@y][@x] = "*" | |
end | |
end | |
def print() | |
@array.each {|line| puts(line.join(''))} | |
end | |
def turn() | |
@direction = DIRECTIONS[(DIRECTIONS.index(@direction) + 1) % 4] | |
end | |
end | |
size = 16 | |
t = Turtle.new(size) | |
size.downto(0) do |step| | |
t.walk(step) | |
t.turn | |
end | |
t.print() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment