class Robot::Matrix
Constants
- DIRECTION_ARROWS
Attributes
direction[R]
matrix[RW]
position[R]
Public Class Methods
new(position, direction)
click to toggle source
# File lib/robot/matrix.rb, line 13 def initialize(position, direction) @position = position @direction = direction @matrix = generate_matrix end
Public Instance Methods
to_s()
click to toggle source
puts an arrow pointing in the direction of the current x,y of the robot on a matrix and returns a string of the matrix
# File lib/robot/matrix.rb, line 22 def to_s set_current_position! matrix.reverse.map { |row| row.join(' ') }.join("\n") end
Private Instance Methods
generate_matrix()
click to toggle source
constructs a matrix of positions Output:
[ ["X", "X", "X", "X", "X"], ["X", "X", "X", "X", "X"], ["X", "X", "X", "X", "X"], ["X", "X", "X", "X", "X"], ["X", "X", "X", "X", "X"]
]
# File lib/robot/matrix.rb, line 49 def generate_matrix [].tap { |a| 25.times { a << 'X' } }.each_slice(5).to_a end
set_current_position!()
click to toggle source
sets the current position x,y coords to a directional arrow
# File lib/robot/matrix.rb, line 31 def set_current_position! matrix[ position[:y] ][ position[:x] ] = DIRECTION_ARROWS[direction] end