v/examples/hanoi.v

23 lines
312 B
V
Raw Normal View History

2019-12-30 11:25:07 +01:00
// hanoi tower
const (
2020-05-22 17:36:09 +02:00
num = 7
2019-12-30 11:25:07 +01:00
)
fn main() {
hanoi(num, 'A', 'B', 'C')
2019-12-30 11:25:07 +01:00
}
fn move(n int, a string, b string) {
2020-12-04 15:05:39 +01:00
println('Disc $n from $a to ${b}...')
2019-12-30 11:25:07 +01:00
}
fn hanoi(n int, a string, b string, c string) {
2019-12-30 11:25:07 +01:00
if n == 1 {
move(1, a, c)
2019-12-30 11:25:07 +01:00
} else {
hanoi(n - 1, a, c, b)
move(n, a, c)
hanoi(n - 1, b, a, c)
2019-12-30 11:25:07 +01:00
}
}