Class: Battlesnake::Board

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

Overview

Represents a single iteration (turn) of a Battlesnake board during gameplay.

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Base

#==

Constructor Details

#initialize(json_or_hash) ⇒ Board

Returns a new instance of Board.

Parameters:

  • json_or_hash (String, Hash)

    can be a hash of attributes, or a JSON string which represents such a structure.



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_jsonHash (readonly)

Returns board as a data structure usable by other objects.

Returns:

  • (Hash)

    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

#foodArray<Location> (readonly)

Returns list of food location objects.

Returns:

  • (Array<Location>)

    list of food location objects



20
21
22
# File 'lib/battlesnake/board.rb', line 20

def food
  @food
end

#hazardsArray<Location> (readonly)

Returns list of hazard location objects.

Returns:

  • (Array<Location>)

    list of hazard location objects



23
24
25
# File 'lib/battlesnake/board.rb', line 23

def hazards
  @hazards
end

#heightInteger (readonly)

Returns height of the board.

Returns:

  • (Integer)

    height of the board



11
12
13
# File 'lib/battlesnake/board.rb', line 11

def height
  @height
end

#snakesArray<Snake> (readonly)

Returns list of snake objects.

Returns:

  • (Array<Snake>)

    list of snake objects



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

def snakes
  @snakes
end

#widthInteger (readonly)

Returns width of the board.

Returns:

  • (Integer)

    width of the board



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

def width
  @width
end

Instance Method Details

#available?(*coordinates) ⇒ Boolean

Whether the supplied location is available (unoccupied).

Parameters:

  • *coordinates (Location, Hash, String, Array)

    can be specified as a Location object, hash containing x/y keys, JSON string of such a hash, or a pair of x,y coordinates expressed as a 2-element array or two separate parameters.

Returns:

  • (Boolean)

    true if location is available (unoccupied by snakes, food, hazards, etc).



73
74
75
# File 'lib/battlesnake/board.rb', line 73

def available?(*coordinates)
  !occupied?(*coordinates)
end

#available_directions(location) ⇒ Array<String>

List of directions (up, down, left, right) available for moving from given Location.

Parameters:

  • location (Location)

    from which moving is desired.

Returns:

  • (Array<String>)

    list of direction strings (“up”, “down”, “left”, “right”)



83
84
85
86
87
# File 'lib/battlesnake/board.rb', line 83

def available_directions(location)
  Location::DIRECTIONS.select do |direction|
    available?(location.move(direction))
  end
end

#occupied?(*coordinates) ⇒ Boolean

Whether the supplied location is occupied.

Parameters:

  • *coordinates (Location, Hash, String, Array)

    can be specified as a Location object, hash containing x/y keys, JSON string of such a hash, or a pair of x,y coordinates expressed as a 2-element array or two separate parameters.

Returns:

  • (Boolean)

    true if location is occupied by snakes, food, hazards, etc.



60
61
62
63
# File 'lib/battlesnake/board.rb', line 60

def occupied?(*coordinates)
  location = coordinates.first.is_a?(Location) ? coordinates.first : Location.new(*coordinates)
  occupied_locations.include?(location)
end

#occupied_locationsArray<Location>

List of all occupied locations on the board; snakes, food, hazards, etc

Returns:

  • (Array<Location>)

    list of occupied locations



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