Class: Tortoise::Interpreter

Inherits:
Object
  • Object
show all
Defined in:
lib/tortoise/interpreter.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(instructions) ⇒ Interpreter

Returns a new instance of Interpreter.



5
6
7
8
9
10
11
12
13
14
15
16
# File 'lib/tortoise/interpreter.rb', line 5

def initialize(instructions)
  lines = instructions.to_s.split("\n")

  @size = lines.shift.to_i
  @canvas = new_canvas
  @direction = 0
  @pen_down = false
  center = (@size - @size/2) - 1

  update_position(center, center)
  draw(lines)
end

Instance Attribute Details

#canvasObject (readonly)

Returns the value of attribute canvas.



3
4
5
# File 'lib/tortoise/interpreter.rb', line 3

def canvas
  @canvas
end

#directionObject (readonly)

Returns the value of attribute direction.



3
4
5
# File 'lib/tortoise/interpreter.rb', line 3

def direction
  @direction
end

#positionObject (readonly)

Returns the value of attribute position.



3
4
5
# File 'lib/tortoise/interpreter.rb', line 3

def position
  @position
end

#sizeObject (readonly)

Returns the value of attribute size.



3
4
5
# File 'lib/tortoise/interpreter.rb', line 3

def size
  @size
end

Instance Method Details

#bk(steps) ⇒ Object



48
49
50
# File 'lib/tortoise/interpreter.rb', line 48

def bk(steps)
  walk(-steps)
end

#draw(commands) ⇒ Object



52
53
54
55
# File 'lib/tortoise/interpreter.rb', line 52

def draw(commands)
  commands = commands.split("\n") if commands.respond_to?(:split)
  commands.each { |command| execute(command) }
end

#fd(steps) ⇒ Object



44
45
46
# File 'lib/tortoise/interpreter.rb', line 44

def fd(steps)
  walk(steps)
end

#lt(degrees) ⇒ Object



40
41
42
# File 'lib/tortoise/interpreter.rb', line 40

def lt(degrees)
  rotate(-degrees)
end

#pdObject



27
28
29
30
# File 'lib/tortoise/interpreter.rb', line 27

def pd
  @pen_down = true
  update_canvas
end

#pen_down?Boolean

Returns:

  • (Boolean)


32
33
34
# File 'lib/tortoise/interpreter.rb', line 32

def pen_down?
  @pen_down
end

#puObject



23
24
25
# File 'lib/tortoise/interpreter.rb', line 23

def pu
  @pen_down = false
end

#rt(degrees) ⇒ Object



36
37
38
# File 'lib/tortoise/interpreter.rb', line 36

def rt(degrees)
  rotate(degrees)
end

#setpos(x, y) ⇒ Object



18
19
20
21
# File 'lib/tortoise/interpreter.rb', line 18

def setpos(x, y)
  @position = place_in_canvas_bounds(x, y)
  update_canvas
end