-
Notifications
You must be signed in to change notification settings - Fork 0
/
towers_of_hanoi.rb
56 lines (48 loc) · 1.02 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
49
50
51
52
53
54
55
56
class TowersOfHanoi
attr_accessor :first_tower, :second_tower, :third_tower
def initialize
@first_tower = [3,2,1]
@second_tower = []
@third_tower = []
end
def render
[@first_tower, @second_tower, @third_tower]
end
def move(start, dest)
if !render[start].empty? && render[dest].length < 3
if render[dest].empty?
render[dest] << render[start].pop
elsif render[start].last < render[dest].last
render[dest] << render[start].pop
else
false
end
end
end
def won?
render == [[], [], [3,2,1]] ? true : false
end
def take_input(messege)
puts
puts messege
input = Integer(gets)
input - 1
end
def play
until won?
system ('clear')
print render
start = take_input('Starting Tower?')
dest = take_input('Ending Tower?')
move(start, dest)
end
system ('clear')
print render
puts
puts "Great, you won!"
end
end
if __FILE__ == $PROGRAM_NAME
game = TowersOfHanoi.new
game.play
end