-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtowers_of_hanoi.rb
48 lines (38 loc) · 1.19 KB
/
towers_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
def towers_of_hanoi(num_discs)
towers = {1 => [], 2 => [], 3 => []}
num_discs.downto(1).each do |i|
towers[1] << i
end
initial_state = towers[1]
until towers[2] == initial_state || towers[3] == initial_state
puts "Tower 1: #{towers[1]}, Tower 2: #{towers[2]}, Tower 3: #{towers[3]}"
tower_from = 0
tower_to = 0
loop do
puts "Move from which tower?"
tower_from = gets.chomp.to_i
if (1..3).include?(tower_from) && !towers[tower_from].empty?
break
elsif !(1..3).include?(tower_from)
puts "Invalid Tower"
else
puts "Tower is empty"
end
end
loop do
puts "To which tower?"
tower_to = gets.chomp.to_i
if (1..3).include?(tower_to) && (towers[tower_to].empty? || towers[tower_to].last > towers[tower_from].last)
break
elsif tower_to == tower_from
puts "Cannot return disc to the same tower"
elsif !(1..3).include?(tower_to)
puts "Invalid Tower"
else
puts "Cannot move disc onto smaller disc"
end
end
towers[tower_to].push(towers[tower_from].pop)
end
puts "The Towers of Hanoi are yours!!!"
end