-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtower_of_hanoi.rb
71 lines (57 loc) · 1.11 KB
/
tower_of_hanoi.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
class TowerOfHanoi
def initialize
@rods = [[], [], []]
end
def get_rod index
validate_rod_index index
@rods[index-1]
end
def set_rod index, value
validate_rod_index index
@rods[index-1] = value
end
def setup amount
amount.times do |i|
self.get_rod(1) << amount - i
end
end
def move(from_rod, to_rod)
topmost_dice_on1 = get_rod(from_rod).pop
get_rod(to_rod) << topmost_dice_on1
end
def can_move?(from_rod, to_rod)
from_rod_last = get_rod(from_rod).last
to_rod_last = get_rod(to_rod).last
return false if from_rod_last.nil?
return true if to_rod_last.nil?
from_rod_last < to_rod_last
end
def step
(1..amount).each { |goal_id|
current_goal = goal goal_id
}
if can_move?(1, 2) and can_move?(1, 3)
move(1, 3)
end
end
def goal step_index
amount = self.amount
if even?(amount)
even?(step_index) ? 3 : 2
else
even?(step_index) ? 2 : 3
end
end
def amount
@rods.inject(0) do |memo, i|
memo += i.size
end
end
private
def even? num
num % 2 == 0
end
def validate_rod_index index
raise "Invalid index: #{index}" if index < 1 or index > 3
end
end