Sha256: 870552b5cf2faa0e60299a4db2dba6aa443f9c9857b3fbf3366321277b572aee

Contents?: true

Size: 694 Bytes

Versions: 1

Compression:

Stored size: 694 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, value = 1)
      @tokens[location] ||= 0
      @tokens[location] += value
      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.2.0 lib/karel/model/token_container.rb