Sha256: 4f943caac9218978238e2175513bdc447cff2d63b2ab0f5c4e504fa8bd2cda72

Contents?: true

Size: 1.43 KB

Versions: 3

Compression:

Stored size: 1.43 KB

Contents

module Gameboard
  class Coordinate
    # Public: Returns the X,Y coordinate of the instance as [X, Y].
    attr_reader :position
    # Public: Returns the X coordinate of the instance.
    attr_reader :x
    # Public: Returns the Y coordinate of the instance.
    attr_reader :y

    # Public: Initialize the Cell.
    #
    # x - The X coordinat
    # y - The Y coordinate in an X,Y grid.
    #
    def initialize(x,y)
      invalid_type = "Coordinates must be integers!"
      raise invalid_type unless (x.is_a?(Integer) && y.is_a?(Integer))
      @position = [x,y]
      @x = x
      @y = y
    end

    # Public: return neigboring coordinates relative to :position.
    #
    # Examples
    #
    #   Coordinate.new(1,1).neighbors
    #     #=> [
    #           [0, 2], [1, 2],
    #           [2, 2], [0, 1],
    #           [2, 1], [0, 0],
    #           [0, 1], [0, 2]
    #         ]
    def neighbors
      relative_neighbors.map { |point| delta(point) }
    end
    private

      # Internal: Add an array with :position.
      #
      # Examples
      #
      #   Coordinate.new(1,1).delta([2,2])
      #     #=> [3, 3]
      #
      def delta(point)
        point.zip(position).map {|combined| combined.reduce(:+) }
      end

      # Internal: Array of possible neigbors.
      #
      def relative_neighbors
        [
          [-1, 1], [0, 1], [1, 1],
          [-1, 0], [1, 0],
          [-1, -1], [-1, 0], [-1, 1]
        ]
      end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
gameboard-3.5.0 lib/gameboard/coordinate.rb
gameboard-3.3.0 lib/gameboard/coordinate.rb
gameboard-3.2.0 lib/gameboard/coordinate.rb