Class: Algorithmable::Cups::StacksAndQueues::TowersOfHanoi::Tower

Inherits:
Object
  • Object
show all
Includes:
DataStructs
Defined in:
lib/algorithmable/cups/stacks_and_queues/towers_of_hanoi.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from DataStructs

#new_bag, #new_deque_queue, #new_fifo_queue, #new_lifo_queue, #new_max_priority_queue, #new_min_priority_queue, #new_ordered_symbol_table, #new_priority_queue

Methods included from DataStructs::Tree

#new_ordered_binary_tree

Methods included from DataStructs::LinkedList

#new_doubly_linked_list, #new_singly_linked_list

Constructor Details

#initialize(index) ⇒ Tower


10
11
12
13
# File 'lib/algorithmable/cups/stacks_and_queues/towers_of_hanoi.rb', line 10

def initialize(index)
  @disks = new_lifo_queue
  @index = index
end

Instance Attribute Details

#indexObject (readonly)

Returns the value of attribute index


8
9
10
# File 'lib/algorithmable/cups/stacks_and_queues/towers_of_hanoi.rb', line 8

def index
  @index
end

Instance Method Details

#add(disk) ⇒ Object


15
16
17
18
# 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) ⇒ Object


41
42
43
# File 'lib/algorithmable/cups/stacks_and_queues/towers_of_hanoi.rb', line 41

def debug(message)
  puts message
end

#inspectObject


33
34
35
# File 'lib/algorithmable/cups/stacks_and_queues/towers_of_hanoi.rb', line 33

def inspect
  "#<Tower ##{index} #{@disks}>"
end

#move_disks(amount, destination, buffer) ⇒ Object


26
27
28
29
30
31
# 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) ⇒ Object


20
21
22
23
24
# 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_aObject


37
38
39
# File 'lib/algorithmable/cups/stacks_and_queues/towers_of_hanoi.rb', line 37

def to_a
  @disks.to_a
end