class Model::TokenContainer

Public Class Methods

new() click to toggle source
# File lib/karel/model/token_container.rb, line 3
def initialize
  @tokens = {}
end

Public Instance Methods

any?(location) click to toggle source
# File lib/karel/model/token_container.rb, line 7
def any?(location)
  @tokens.key?(location)
end
as_json() click to toggle source
# File lib/karel/model/token_container.rb, line 28
def as_json
  @tokens.keys.sort.map do |location|
    {
      'location' => location.to_s,
      'count' => @tokens[location]
    }
  end
end
pick(location) click to toggle source
# File lib/karel/model/token_container.rb, line 11
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
put(location, value = 1) click to toggle source
# File lib/karel/model/token_container.rb, line 22
def put(location, value = 1)
  @tokens[location] ||= 0
  @tokens[location] += value
  return self
end