Class: Robot::Simulator::Robot

Inherits:
Object
  • Object
show all
Defined in:
lib/robot/simulator/robot.rb

Overview

Robot class corresponds to robot which can move around on the table.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(table) ⇒ Robot

Returns a new instance of Robot.



7
8
9
# File 'lib/robot/simulator/robot.rb', line 7

def initialize(table)
  @table = table
end

Instance Attribute Details

#current_trackObject

Returns the value of attribute current_track.



5
6
7
# File 'lib/robot/simulator/robot.rb', line 5

def current_track
  @current_track
end

#tableObject (readonly)

Returns the value of attribute table.



5
6
7
# File 'lib/robot/simulator/robot.rb', line 5

def table
  @table
end

Instance Method Details

#moveObject



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/robot/simulator/robot.rb', line 17

def move
  return false unless is_on_table?

  newX = current_track.coordinate.x
  newX += 1 if current_track.facing == Direction::EAST
  newX -= 1 if current_track.facing == Direction::WEST

  newY = current_track.coordinate.y
  newY += 1 if current_track.facing == Direction::NORTH
  newY -= 1 if current_track.facing == Direction::SOUTH

  new_coordinate = Coordinate.new(newX, newY)
  return false unless is_valid_coordinate? new_coordinate

  self.current_track = Track.new new_coordinate, current_track.facing
end

#place(coordinate, direction) ⇒ Object



11
12
13
14
15
# File 'lib/robot/simulator/robot.rb', line 11

def place(coordinate, direction)
  return false unless is_valid_coordinate?(coordinate)

  self.current_track = Track.new coordinate, direction
end

#turn_leftObject



34
35
36
37
38
# File 'lib/robot/simulator/robot.rb', line 34

def turn_left
  return false unless is_on_table?

  self.current_track = Track.new current_track.coordinate, Direction.left(current_track.facing)
end

#turn_rightObject



40
41
42
43
44
# File 'lib/robot/simulator/robot.rb', line 40

def turn_right
  return false unless is_on_table?

  self.current_track = Track.new current_track.coordinate, Direction.right(current_track.facing)
end