Very new student, Hanoi Towers



First of all, thanks to everyone who helped me when I first asked
about this:

public class Towers {
static int nDisks = 3 ;
public static void main(String[] args) {
hanoiTower(nDisks, 'A', 'B','C');}
public static void hanoiTower( int topN, char src, char inter,
char dest)
{
if(topN == 1)
System.out.println( " Disc 1 from "+ src + " to " +
dest);
else {
hanoiTower(topN-1, src, dest, inter);

System.out.println( "Disk" + topN +"from" + src +
"to" + dest);
hanoiTower(topN-1, inter, src, dest);

}
Disc 1 from A to C
Disk2fromAtoB
Disc 1 from C to B
Disk3fromAtoC
Disc 1 from B to A
Disk2fromBtoC
Disc 1 from A to C

Please tell me where I'm not getting it. This is how I try to work it
until I fail, or it works by coincidence and I'm wrong from the
beginning::
I can see the first line of the solution being printed as a command,
not because (topN == 1) being true. And, A is src, and C is dest.,
like the arguments set.
So, on to the else, where topN becomes 2 after the hanoiTower
(topN-1,src,dest,inter) is applied. So the line "Disk2from A(still in
src slot) to B, (now in dest slot) is printed. OK.
So now on to the next hanoiTower, the last in the else, where topN
becomes 1, 'A' is now inter, 'B' is now src, 'C' is now dest..

Back up to the if(topN == 1) where it is now true. "Disc 1 from C to
B" prints, but how did src come to represent 'C', and dest come to
represent 'B'. In my logic, the last hanoiTower would read (1, inter,
dest, src) for that line to print.
Then, why does the next line, "Disk3fromAtoC" print at all when the
else should not have been entered?
I've went back to reviewing,instead of fuming, and I found another
text, albeit a much simpler one, that does not mention recursion at
all, but I would like to continue with the more intense text, by
Deitel. I've been looking for an instructor, or student instructor in
a
local college, or high school, who might be willing to give me a mini
lecture. Like other instances before, I've suddenly thought,"
Eureka!", then, "How could I have been so thick-headed for so
long?".Please help.
.



Relevant Pages

  • tower of hanoi, recursion
    ... public static void hanoiTower(int topN, char src, char inter, ... char dest) ... Disc 1 from A to C ...
    (comp.lang.java.help)
  • Re: is strncpy useful at all?
    ... > You return a pointer to the terminating null byte of 'dest'. ... char* tstrncpy; ... void runTest(FP_STRNCPY fp, char* dest, char* src, size_t maxlen, ... runTest(tstrncpy, dest, src, sizeof src - 1, DEST_SIZE); ...
    (alt.comp.lang.learn.c-cpp)
  • Re: HLA History
    ... Well, I see that teaching people is in of itself, very dangerous. ... In C std libraries, everything is dest, src notation. ... My first programming language was actually TI-83 Calculator basic. ...
    (alt.lang.asm)
  • Re: Windows Assembly
    ... assembler as it's native AT&T style syntax is src, ... That's a preprocessor, not an assembler. ... What are the odds that it'll be with something that's "src, dest"? ...
    (alt.lang.asm)
  • Re: rsync
    ... ~$rsync -t src dest ... rsync -av src/ dest ... skipping directory src/. ...
    (alt.os.linux)

Loading