Sha256: cfa1526869b3f5b2b85f0ef83a741581261665c070dece6b5b84ebea44dc1ec9

Contents?: true

Size: 679 Bytes

Versions: 1

Compression:

Stored size: 679 Bytes

Contents

module Model
  class TokenContainer
    def initialize
      @tokens = {}
    end

    def any?(location)
      @tokens.key?(location)
    end

    def pick(location)
      if !@tokens.key?(location)
        fail "No tokens at #{location}"
      elsif @tokens[location] == 1
        @tokens.delete(location)
      else
        @tokens[location] -= 1
      end
      return self
    end

    def put(location)
      @tokens[location] ||= 0
      @tokens[location] += 1
      return self
    end

    def as_json
      @tokens.keys.sort.map do |location|
        {
          'location' => location.to_s,
          'count' => @tokens[location]
        }
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
karel-interpreter-0.1.0 lib/karel/model/token_container.rb