Class: Battlesnake::Location

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

Overview

Represents a pair of (x,y) coordinates, and provides helper methods for managing distance and direction.

Constant Summary collapse

DIRECTIONS =

Valid directions.

['up', 'down', 'right', 'left']

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*coordinates) ⇒ Location

Instantiates with the given (x,y) coordinates. Coordinates can be:

  • a hash containing “x” and “y” keys

  • a JSON string that parses to such a hash

  • an array containing (x,y) coordinates

  • two separate (x,y) parameters

Parameters:

  • coordinates (Hash, Array, String)

    the coordinates



32
33
34
35
36
# File 'lib/battlesnake/location.rb', line 32

def initialize(*coordinates)
  set_xy(*coordinates)

  @as_json = {'x' => x, 'y' => y}
end

Instance Attribute Details

#as_jsonHash (readonly)

Returns coordinates as a hash, for integration with other objects.

Returns:

  • (Hash)

    coordinates as a hash, for integration with other objects.



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

def as_json
  @as_json
end

#xInteger (readonly)

Returns a positive integer representing the distance from the left side of the board.

Returns:

  • (Integer)

    a positive integer representing the distance from the left side of the board.



10
11
12
# File 'lib/battlesnake/location.rb', line 10

def x
  @x
end

#yInteger (readonly)

Returns a positive integer representing the distance from the bottom of the board.

Returns:

  • (Integer)

    a positive integer representing the distance from the bottom of the board.



14
15
16
# File 'lib/battlesnake/location.rb', line 14

def y
  @y
end

Instance Method Details

#coordsArray

Convenience method to return (x,y) coordinates as an array.

Returns:

  • (Array)


42
43
44
45
# File 'lib/battlesnake/location.rb', line 42

def coords
  return @coords if defined?(@coords)
  @coords = [x, y]
end

#direction(location) ⇒ one of DIRECTIONS

Note:

Returns nil if the locations are the same. Favors “up” or “down” if horizontal and vertical distances are the same.

Determine the most prominent orthoganal direction [see DIRECTIONS] toward another location.

Parameters:

  • location (Location)

    another location instance.

Returns:



67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/battlesnake/location.rb', line 67

def direction(location)
  return nil if distance(location) == 0

  dx = delta_x(location)
  dy = delta_y(location)

  if dx.abs <= dy.abs
    dy > 0 ? 'up' : 'down'
  elsif dx.abs > dy.abs
    dx > 0 ? 'right' : 'left'
  end
end

#distance(location) ⇒ Integer

Calculate the distance from this instance to another.

Parameters:

  • location (Location)

    another Location instance.

Returns:

  • (Integer)

    the number of blocks away, in “manhattan distance”.



53
54
55
# File 'lib/battlesnake/location.rb', line 53

def distance(location)
  [delta_x(location).abs, delta_y(location).abs].reduce(:+)
end

#move(requested_direction) ⇒ Location

Note:

Returns nil if the requested direction is not recognized.

Return a new location instance with coordinates moved one step in the requested direction.

Parameters:

Returns:

  • (Location)

    a new location instance.



88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/battlesnake/location.rb', line 88

def move(requested_direction)
  return nil unless DIRECTIONS.include?(requested_direction)

  new_x = x
  new_y = y

  case requested_direction
  when 'right'
    new_x += 1
  when 'left'
    new_x -= 1
  when 'up'
    new_y += 1
  when 'down'
    new_y -= 1
  end

  self.class.new(new_x, new_y)
end