Class: Battlesnake::Location
- Inherits:
-
Object
- Object
- Battlesnake::Location
- 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
-
#as_json ⇒ Hash
readonly
Coordinates as a hash, for integration with other objects.
-
#x ⇒ Integer
readonly
A positive integer representing the distance from the left side of the board.
-
#y ⇒ Integer
readonly
A positive integer representing the distance from the bottom of the board.
Instance Method Summary collapse
-
#coords ⇒ Array
Convenience method to return (x,y) coordinates as an array.
-
#direction(location) ⇒ one of DIRECTIONS
Determine the most prominent orthoganal direction [see DIRECTIONS] toward another location.
-
#distance(location) ⇒ Integer
Calculate the distance from this instance to another.
-
#initialize(*coordinates) ⇒ Location
constructor
Instantiates with the given (x,y) coordinates.
-
#move(requested_direction) ⇒ Location
Return a new location instance with coordinates moved one step in the requested direction.
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
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_json ⇒ Hash (readonly)
Returns 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 |
#x ⇒ Integer (readonly)
Returns 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 |
#y ⇒ Integer (readonly)
Returns 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
#coords ⇒ Array
Convenience method to return (x,y) coordinates as an 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
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.
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.
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
Returns nil if the requested direction is not recognized.
Return a new location instance with coordinates moved one step in the requested direction.
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 |