Sha256: a73a7ae754d7cb7cb0b49c680990a11a7178c4e7eff4a60934ea087272869786

Contents?: true

Size: 1.21 KB

Versions: 1

Compression:

Stored size: 1.21 KB

Contents

require 'json'

module Battlesnake
  ##
  # Represents a single iteration (turn) of a Battlesnake board during gameplay.
  class Board
    # @return [Hash] board as a data structure usable by other objects.
    attr_reader :as_json

    # @return [Integer] height of the board
    attr_reader :height

    # @return [Integer] width of the board
    attr_reader :width

    # @return [Array<Snake>] list of snake objects
    attr_reader :snakes

    # @return [Array<Location>] list of food location objects
    attr_reader :food

    # @return [Array<Location>] list of hazard location objects
    attr_reader :hazards

    ##
    # Returns a new instance of Board.
    #
    # @param json_or_hash [String,Hash] can be a hash of attributes, or a JSON string which
    #   represents such a structure.
    #
    # @return [Board]
    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
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
battlesnake-0.1.1 lib/battlesnake/board.rb