-
Notifications
You must be signed in to change notification settings - Fork 25
Expand file tree
/
Copy pathruby-prolog-hanoi
More file actions
executable file
·38 lines (28 loc) · 925 Bytes
/
ruby-prolog-hanoi
File metadata and controls
executable file
·38 lines (28 loc) · 925 Bytes
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
#!/usr/bin/env ruby
$LOAD_PATH.unshift File.join(File.dirname(__FILE__), '..', 'lib')
require 'ruby-prolog'
# Inspired by..
# http://www.csupomona.edu/~jrfisher/www/prolog_tutorial/2_3.html
# http://eigenclass.org/hiki.rb?tiny+prolog+in+ruby
# Adapted by Preston Lee.
c = RubyProlog::Core.new
c.instance_eval do
move[0,:X,:Y,:Z] << :CUT # There are no more moves left
move[:N,:A,:B,:C] << [
is(:M,:N){|n| n - 1}, # reads as "M IS N - 1"
move[:M,:A,:C,:B],
write_info[:A,:B],
move[:M,:C,:B,:A]
]
write_info[:X,:Y] << [
write["move a disc from the "],
write[:X], write[" pole to the "],
write[:Y], writenl[" pole "]
]
hanoi[:N] << move[:N,"left","right","center"]
puts "\nWhat's the solution for a single disc?"
query{hanoi[1]}
puts "\n\nWhat's the solution for 5 discs?"
query{hanoi[5]}
# do_stuff[:STUFF].calls{|env| print env[:STUFF]; true}
end