From 26ee09c9bbb208c7acd3ff93650f5770c7ad23a8 Mon Sep 17 00:00:00 2001 From: Charles WANG Date: Mon, 30 Dec 2019 18:25:07 +0800 Subject: [PATCH] examples: hanoi.v --- examples/hanoi.v | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) create mode 100644 examples/hanoi.v diff --git a/examples/hanoi.v b/examples/hanoi.v new file mode 100644 index 0000000000..679d1d1d20 --- /dev/null +++ b/examples/hanoi.v @@ -0,0 +1,24 @@ +// hanoi tower +const ( + Num = 7 +) + +fn main() { + hanoi(Num, 'A','B','C') +} + +fn move(n int, a, b string) int { + println('Disc $n from $a to $b\...') + return 0 +} + +fn hanoi(n int, a, b, c string) int { + if n == 1 { + move(1,a,c) + } else { + hanoi(n-1, a, c, b) + move(n,a,c) + hanoi(n-1, b, a, c) + } + return 0 +}