Class: Tortoise::Interpreter
- Inherits:
-
Object
- Object
- Tortoise::Interpreter
- Defined in:
- lib/tortoise/interpreter.rb
Instance Attribute Summary collapse
-
#canvas ⇒ Object
readonly
Returns the value of attribute canvas.
-
#direction ⇒ Object
readonly
Returns the value of attribute direction.
-
#position ⇒ Object
readonly
Returns the value of attribute position.
-
#size ⇒ Object
readonly
Returns the value of attribute size.
Instance Method Summary collapse
- #bk(steps) ⇒ Object
- #draw(commands) ⇒ Object
- #fd(steps) ⇒ Object
-
#initialize(instructions) ⇒ Interpreter
constructor
A new instance of Interpreter.
- #lt(degrees) ⇒ Object
- #pd ⇒ Object
- #pen_down? ⇒ Boolean
- #pu ⇒ Object
- #rt(degrees) ⇒ Object
- #setpos(x, y) ⇒ Object
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
#canvas ⇒ Object (readonly)
Returns the value of attribute canvas.
3 4 5 |
# File 'lib/tortoise/interpreter.rb', line 3 def canvas @canvas end |
#direction ⇒ Object (readonly)
Returns the value of attribute direction.
3 4 5 |
# File 'lib/tortoise/interpreter.rb', line 3 def direction @direction end |
#position ⇒ Object (readonly)
Returns the value of attribute position.
3 4 5 |
# File 'lib/tortoise/interpreter.rb', line 3 def position @position end |
#size ⇒ Object (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 |
#pd ⇒ Object
27 28 29 30 |
# File 'lib/tortoise/interpreter.rb', line 27 def pd @pen_down = true update_canvas end |
#pen_down? ⇒ Boolean
32 33 34 |
# File 'lib/tortoise/interpreter.rb', line 32 def pen_down? @pen_down end |
#pu ⇒ Object
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 |