Sha256: 577efe59ec3fdc6bb7174f55042a04b85cb29f80660f4b408f066f3473e9fda6

Contents?: true

Size: 642 Bytes

Versions: 1

Compression:

Stored size: 642 Bytes

Contents

module Gameboard
  class Coordinate
    attr_reader :position, :x, :y
    
    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

    def neighbors
      relative_neighbors.map { |point| delta(point) }
    end
    private
      def delta(point)
        point.zip(position).map {|combined| combined.reduce(:+) }
      end

      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

1 entries across 1 versions & 1 rubygems

Version Path
gameboard-2.0.0 lib/gameboard/coordinate.rb