Sha256: c452e0d6c5e82f2d06df21f6d86472510937e52fb062c3c81bd7ad4b96e6ccc1

Contents?: true

Size: 772 Bytes

Versions: 1

Compression:

Stored size: 772 Bytes

Contents

# frozen_string_literal: true

module Qi
  # The Action abstraction.
  class Action
    CAPTURE_CHAR = "&"
    DROP_CHAR    = "*"

    # Action initializer.
    def initialize(*captures, **squares)
      @captures = captures
      @squares  = squares
    end

    # Commit an action to the position.
    #
    # @return [Array] An action to change the position.
    def call(**diffs)
      captures = @captures + Array(diffs.delete(CAPTURE_CHAR))
      drop     = diffs.delete(DROP_CHAR)

      unless drop.nil?
        index = captures.rindex(drop)
        raise ::IndexError, "Piece #{drop.inspect} not captured!" if index.nil?

        captures.delete_at(index)
      end

      squares = @squares.merge(diffs).compact

      [captures.sort, squares]
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
qi-10.0.0.beta4 lib/qi/action.rb