# Hanoi Tower in Basic

Hello guys,

I'm trying to remember some MSX Basic coding, and I tried to port this recursive C code that solves the Hanoi Tower game. Problem is, MSX Basic lacks resources to do recursion, like function parameters. I searched the net, and I only found MSX Basic code that allows you to play the game, but I just need to solve the game, not play it.

Anyone can help me to port this code to MSX Basic?

void movetower (int n, char orig, char dest, char aux){
if (n==1) {
printf("\nMove disc 1 from tower %c to tower %c", orig, dest);
return;
}
movetower(n-1,orig,aux,dest);
printf("\nMove disc %d from tower %c to tower %c", n, orig, dest);
movetower(n-1,aux,dest,orig);
};

int main() {
int discs;
printf("\t\t\t\tHANOI TOWER\n\n");
printf("Enter number of discs: ");
scanf("%d",&discs);
movetower(discs,'A','C','B');
return 0;
}

example

```100 PRINT "HANOI TOWER"
110 PRINT "Enter number of discs: ";
120 INPUT N
130 O\$="A":D\$="C":A\$="B"
140 GOSUB 200
150 END
200 '
210 IF N=1 THEN PRINT "Move disc 1 from tower " + O\$ + " to tower " + D\$:RETURN
220 SWAP A\$,D\$:N=N-1
230 GOSUB 200
240 SWAP A\$,D\$:N=N+1
250 PRINT "Move disc" + STR\$(N) + " from tower " + O\$ + " to tower " + D\$
260 SWAP A\$,O\$:N=N-1
270 GOSUB 200
280 SWAP A\$,O\$:N=N+1
290 RETURN
```

Thanks a lot umaiboux!

I was worried that it would need a bigger algorithm in order to deal with the lack of function parameters and variable scope. But your solution showed me that you can do without function parameters without major problems.

I wonder if that is the case with all recursive functions. We can code them in MSX Basic without much trouble? I mean, are the any bottlenecks / major problems when dealing with recursive functions in MSX Basic?

- This is OK

```#include <stdio.h>

void nest(int count, char s[])
{
int i;
if (count > 2) {
printf("%s\n", s);
return;
}
for(i=0; i<=count; i++) {
s[count] = 65 + i;
nest(count + 1, s);
}
}

int main(void)
{
char s[] = "123";
nest(0, s);
return 0;
}
```

- This is NG

```100 S\$="123"
120 C=0
130 GOSUB 200
140 END
200 '
210 IF C>2 THEN PRINT S\$:RETURN
220 FOR I=0 TO C
230 MID\$(S\$,C+1,1) = CHR\$(65+I)
240 C=C+1
250 GOSUB 200
260 C=C-1
270 NEXT
280 RETURN
```

- This is OK

```100 S\$="123"
120 C=0
130 GOSUB 200
140 END
200 '
210 IF C>2 THEN PRINT S\$:RETURN
220 I(C)=0
230 MID\$(S\$,C+1,1) = CHR\$(65+I(C))
240 C=C+1
250 GOSUB 200
260 C=C-1
270 I(C)=I(C)+1:IF I(C)<=C GOTO 230
280 RETURN
```

- This will be OK

```100 PRINT "HANOI TOWER 2"
110 N=4
120 O\$="A":D\$="C":A\$="B":H\$=""
130 ON INTERVAL=10+RND(-TIME)*10 GOSUB 300:INTERVAL ON
140 GOSUB 200
150 END
200 '
210 IF N=1 THEN PRINT H\$ + "Move disc 1 from tower " + O\$ + " to tower " + D\$:RETURN
220 SWAP A\$,D\$:N=N-1
230 GOSUB 200
240 SWAP A\$,D\$:N=N+1
250 PRINT H\$ + "Move disc" + STR\$(N) + " from tower " + O\$ + " to tower " + D\$
260 SWAP A\$,O\$:N=N-1
270 GOSUB 200
280 SWAP A\$,O\$:N=N+1
290 RETURN
300 '
310 INTERVAL OFF
315 N1=N:O1\$=O\$:D1\$=D\$:A1\$=A\$:H1\$=H\$
320 N=2
330 O\$="d":D\$="f":A\$="e":H\$=" "
340 GOSUB 200
345 N=N1:O\$=O1\$:D\$=D1\$:A\$=A1\$:H\$=H1\$ :' If you delete this line, it will be NG.
350 RETURN
```

If it is an exercise of recursion, you've got the solution. If it's an exercise in solving the task, note that Towers of Hanoi can be solved by a very simple algorithm without recursion, by alternating between these two moves:

1. Move the small disc in a cycle: from 1 to 2, or from 2 to 3, or from 3 to 1, unless the number of discs is odd, in which case, move it in the opposite cycle: from 1 to 3, or from 3 to 2, or from 2 to 1.
2. Move the only other disc that can be moved that is not the small disc.