Class: ToyRobotSim::Location

Inherits:
Object
  • Object
show all
Defined in:
lib/toy_robot_sim/location.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(x, y) ⇒ Location

Returns a new instance of Location.



5
6
7
8
9
10
11
12
13
# File 'lib/toy_robot_sim/location.rb', line 5

def initialize(x, y)
  if x.respond_to?(:to_i) && y.respond_to?(:to_i)
    @x = x.to_i
    @y = y.to_i
  else
    @x = 0
    @y = 0
  end
end

Instance Attribute Details

#xObject (readonly)

Returns the value of attribute x.



3
4
5
# File 'lib/toy_robot_sim/location.rb', line 3

def x
  @x
end

#yObject (readonly)

Returns the value of attribute y.



3
4
5
# File 'lib/toy_robot_sim/location.rb', line 3

def y
  @y
end

Instance Method Details

#eastObject



21
22
23
# File 'lib/toy_robot_sim/location.rb', line 21

def east
  ToyRobotSim::Location.new(x + 1, y)
end

#northObject



17
18
19
# File 'lib/toy_robot_sim/location.rb', line 17

def north
  ToyRobotSim::Location.new(x, y + 1)
end

#southObject



25
26
27
# File 'lib/toy_robot_sim/location.rb', line 25

def south
  ToyRobotSim::Location.new(x, y - 1)
end

#westObject



29
30
31
# File 'lib/toy_robot_sim/location.rb', line 29

def west
  ToyRobotSim::Location.new(x - 1, y)
end