Class: Battlesnake::Board
Overview
Represents a single iteration (turn) of a Battlesnake board during gameplay.
Instance Attribute Summary collapse
-
#as_json ⇒ Hash
readonly
Board as a data structure usable by other objects.
-
#food ⇒ Array<Location>
readonly
List of food location objects.
-
#hazards ⇒ Array<Location>
readonly
List of hazard location objects.
-
#height ⇒ Integer
readonly
Height of the board.
-
#snakes ⇒ Array<Snake>
readonly
List of snake objects.
-
#width ⇒ Integer
readonly
Width of the board.
Instance Method Summary collapse
-
#available?(location) ⇒ Boolean
Whether the supplied location is available (unoccupied).
-
#available_directions(location) ⇒ Array<String>
List of directions (up, down, left, right) available for moving from given Location.
-
#initialize(json_or_hash) ⇒ Board
constructor
Returns a new instance of Board.
-
#occupied?(location) ⇒ Boolean
Whether the supplied location is occupied.
-
#occupied_locations ⇒ Array<Location>
List of all occupied locations on the board; snakes, food, hazards, etc.
-
#on_board?(location) ⇒ Boolean
Where the supplied location falls within the boundaries of the board.
-
#paths(from, to) ⇒ Array<Path>
List of valid, consecutive paths from one location to the next.
Methods inherited from Base
Constructor Details
#initialize(json_or_hash) ⇒ Board
Returns a new instance of Board.
32 33 34 35 36 37 38 39 40 41 |
# File 'lib/battlesnake/board.rb', line 32 def initialize(json_or_hash) data = json_or_hash.is_a?(String) ? JSON.parse(json_or_hash) : json_or_hash @as_json = data @height = data['height'] @width = data['width'] @snakes = data['snakes'].map{ |attrs| Snake.new(attrs) } @food = data['food'].map{ |attrs| Location.new(attrs) } @hazards = data['hazards'].map{ |attrs| Location.new(attrs) } end |
Instance Attribute Details
#as_json ⇒ Hash (readonly)
Returns board as a data structure usable by other objects.
8 9 10 |
# File 'lib/battlesnake/board.rb', line 8 def as_json @as_json end |
#food ⇒ Array<Location> (readonly)
Returns list of food location objects.
20 21 22 |
# File 'lib/battlesnake/board.rb', line 20 def food @food end |
#hazards ⇒ Array<Location> (readonly)
Returns list of hazard location objects.
23 24 25 |
# File 'lib/battlesnake/board.rb', line 23 def hazards @hazards end |
#height ⇒ Integer (readonly)
Returns height of the board.
11 12 13 |
# File 'lib/battlesnake/board.rb', line 11 def height @height end |
#snakes ⇒ Array<Snake> (readonly)
Returns list of snake objects.
17 18 19 |
# File 'lib/battlesnake/board.rb', line 17 def snakes @snakes end |
#width ⇒ Integer (readonly)
Returns width of the board.
14 15 16 |
# File 'lib/battlesnake/board.rb', line 14 def width @width end |
Instance Method Details
#available?(location) ⇒ Boolean
Whether the supplied location is available (unoccupied).
78 79 80 |
# File 'lib/battlesnake/board.rb', line 78 def available?(location) on_board?(location) && !occupied?(location) end |
#available_directions(location) ⇒ Array<String>
List of directions (up, down, left, right) available for moving from given Location.
88 89 90 91 92 |
# File 'lib/battlesnake/board.rb', line 88 def available_directions(location) Location::DIRECTIONS.select do |direction| available?(location.move(direction)) end end |
#occupied?(location) ⇒ Boolean
Whether the supplied location is occupied.
58 59 60 |
# File 'lib/battlesnake/board.rb', line 58 def occupied?(location) occupied_locations.include?(location) end |
#occupied_locations ⇒ Array<Location>
List of all occupied locations on the board; snakes, food, hazards, etc
47 48 49 50 |
# File 'lib/battlesnake/board.rb', line 47 def occupied_locations return @occupied_locations if defined?(@occupied_locations) @occupied_locations = snakes.map(&:body).flatten + food + hazards end |
#on_board?(location) ⇒ Boolean
Where the supplied location falls within the boundaries of the board.
68 69 70 |
# File 'lib/battlesnake/board.rb', line 68 def on_board?(location) location.x >= 0 && location.y >= 0 && location.x < width && location.y < height end |
#paths(from, to) ⇒ Array<Path>
List of valid, consecutive paths from one location to the next. Paths may not:
- wander outside board boundaries.
- use the same location more than once.
- contain occupied locations, EXCEPT the start/end locations.
The exception for start/end locations allows us to generate paths, for example, from a snake to a food location, without having to calulate the starting/ending permutations ourselves.
108 109 110 |
# File 'lib/battlesnake/board.rb', line 108 def paths(from, to) end |