class Algorithmable::Cups::StacksAndQueues::TowersOfHanoi::Tower

Attributes

index[R]

Public Class Methods

new(index) click to toggle source
# File lib/algorithmable/cups/stacks_and_queues/towers_of_hanoi.rb, line 10
def initialize(index)
  @disks = new_lifo_queue
  @index = index
end

Public Instance Methods

add(disk) click to toggle source
# File lib/algorithmable/cups/stacks_and_queues/towers_of_hanoi.rb, line 15
def add(disk)
  fail "Error placing disk #{disk} on #{@disks.peek}." if !@disks.empty? && @disks.peek <= disk
  @disks.push disk
end
debug(message) click to toggle source
# File lib/algorithmable/cups/stacks_and_queues/towers_of_hanoi.rb, line 41
def debug(message)
  puts message
end
inspect() click to toggle source
# File lib/algorithmable/cups/stacks_and_queues/towers_of_hanoi.rb, line 33
def inspect
  "#<Tower ##{index} #{@disks}>"
end
move_disks(amount, destination, buffer) click to toggle source
# File lib/algorithmable/cups/stacks_and_queues/towers_of_hanoi.rb, line 26
def move_disks(amount, destination, buffer)
  return unless amount > 0
  move_disks amount - 1, buffer, destination
  move_top_to_tower destination
  buffer.move_disks amount - 1, destination, self
end
move_top_to_tower(other_tower) click to toggle source
# File lib/algorithmable/cups/stacks_and_queues/towers_of_hanoi.rb, line 20
def move_top_to_tower(other_tower)
  top = @disks.pop
  debug "Moving disk ##{top} from tower #{index} to #{other_tower.index}."
  other_tower.add top
end
to_a() click to toggle source
# File lib/algorithmable/cups/stacks_and_queues/towers_of_hanoi.rb, line 37
def to_a
  @disks.to_a
end