Created
January 10, 2016 21:17
-
-
Save silbo/61ad87fcf41bc5a3adc0 to your computer and use it in GitHub Desktop.
non-recursive hanoi tower solver with 3 rods "n" disks in Java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Hanoi { | |
/* | |
* @param int - number of disks | |
*/ | |
static void solve ( int n ) { | |
// create the rods | |
IntStack rod_A = new IntStack(n); | |
IntStack rod_B = new IntStack(n); | |
IntStack rod_C = new IntStack(n); | |
// push the disks to the start rod | |
for ( int i = n; i > 0; i-- ) { | |
rod_A.push(i); | |
} | |
// when there is only one disk | |
if ( n == 1 ) { | |
// move disk from rod A to C | |
rod_C.push(rod_A.pop()); | |
System.out.println( " from A to C" ); | |
// we are done | |
return; | |
} | |
// smallest disk was moved | |
boolean small_disk_moved = false; | |
// location of smallest disk | |
IntStack small_disk_rod = null; | |
// last location of smallest disk | |
IntStack small_disk_last_rod = null; | |
// in case can be divided by 2 | |
if ( n % 2 == 0 ) { | |
small_disk_rod = rod_B; | |
rod_B.push(rod_A.pop()); | |
System.out.println( " from A to B" ); | |
} else { | |
small_disk_rod = rod_C; | |
rod_C.push(rod_A.pop()); | |
System.out.println( " from A to C" ); | |
} | |
// info about smallest disk | |
small_disk_moved = true; | |
small_disk_last_rod = rod_A; | |
// start the solving loop | |
while ( true ) { | |
// if smallest has already moved | |
if ( small_disk_moved ) { | |
// move the next disk, only one legal move | |
if ( small_disk_rod == rod_A ) { | |
int disk1 = rod_B.pop(); | |
int disk2 = rod_C.pop(); | |
// move to the valid location | |
if ( disk1 == -1 && disk2 == -1 ) { | |
// no more disks, we are done! | |
break; | |
} else if ( disk1 == -1 && disk2 != -1 ) { | |
rod_B.push(disk2); | |
System.out.println( " from C to B" ); | |
} else if ( disk1 != -1 && disk2 == -1 ) { | |
rod_C.push(disk1); | |
System.out.println( " from B to C" ); | |
} else if ( disk1 > disk2 ) { | |
rod_B.push(disk1); | |
rod_B.push(disk2); | |
System.out.println( " from C to B" ); | |
} else { | |
rod_C.push(disk2); | |
rod_C.push(disk1); | |
System.out.println( " from B to C" ); | |
} | |
} else if ( small_disk_rod == rod_B ) { | |
int disk1 = rod_A.pop(); | |
int disk2 = rod_C.pop(); | |
// move to the valid location | |
if ( disk1 == -1 && disk2 == -1 ) { | |
// no more disks, we are done! | |
break; | |
} else if ( disk1 == -1 && disk2 != -1 ) { | |
rod_A.push(disk2); | |
System.out.println( " from C to A" ); | |
} else if ( disk1 != -1 && disk2 == -1 ) { | |
rod_C.push(disk1); | |
System.out.println( " from A to C" ); | |
} else if ( disk1 > disk2 ) { | |
rod_A.push(disk1); | |
rod_A.push(disk2); | |
System.out.println( " from C to A" ); | |
} else { | |
rod_C.push(disk2); | |
rod_C.push(disk1); | |
System.out.println( " from A to C" ); | |
} | |
} else { | |
int disk1 = rod_A.pop(); | |
int disk2 = rod_B.pop(); | |
// move to the valid location | |
if ( disk1 == -1 && disk2 == -1 ) { | |
// no more disks, we are done! | |
break; | |
} else if ( disk1 == -1 && disk2 != -1 ) { | |
rod_A.push(disk2); | |
System.out.println( " from B to A" ); | |
} else if ( disk1 != -1 && disk2 == -1 ) { | |
rod_B.push(disk1); | |
System.out.println( " from A to B" ); | |
} else if ( disk1 > disk2 ) { | |
rod_A.push(disk1); | |
rod_A.push(disk2); | |
System.out.println( " from B to A" ); | |
} else { | |
rod_B.push(disk2); | |
rod_B.push(disk1); | |
System.out.println( " from A to B" ); | |
} | |
} | |
small_disk_moved = false; | |
} else { | |
// move the smallest disk to the location it was not previously | |
if ( small_disk_rod == rod_A && small_disk_last_rod == rod_B ) { | |
small_disk_rod = rod_C; | |
small_disk_last_rod = rod_A; | |
rod_C.push(rod_A.pop()); | |
System.out.println( " from A to C" ); | |
} else if ( small_disk_rod == rod_A && small_disk_last_rod == rod_C ) { | |
small_disk_rod = rod_B; | |
small_disk_last_rod = rod_A; | |
rod_B.push(rod_A.pop()); | |
System.out.println( " from A to B" ); | |
} else if ( small_disk_rod == rod_B && small_disk_last_rod == rod_A ) { | |
small_disk_rod = rod_C; | |
small_disk_last_rod = rod_B; | |
rod_C.push(rod_B.pop()); | |
System.out.println( " from B to C" ); | |
} else if ( small_disk_rod == rod_B && small_disk_last_rod == rod_C ) { | |
small_disk_rod = rod_A; | |
small_disk_last_rod = rod_B; | |
rod_A.push(rod_B.pop()); | |
System.out.println( " from B to A" ); | |
} else if ( small_disk_rod == rod_C && small_disk_last_rod == rod_A ) { | |
small_disk_rod = rod_B; | |
small_disk_last_rod = rod_C; | |
rod_B.push(rod_C.pop()); | |
System.out.println( " from C to B" ); | |
} else if ( small_disk_rod == rod_C && small_disk_last_rod == rod_B ) { | |
small_disk_rod = rod_A; | |
small_disk_last_rod = rod_C; | |
rod_A.push(rod_C.pop()); | |
System.out.println( " from C to A" ); | |
} | |
small_disk_moved = true; | |
} | |
} | |
} | |
public static void main ( String argv[] ) { | |
int n; | |
System.out.print( "Number of disks: " ); | |
n = Keyboard.readInt(); | |
if ( n > 0 ) { | |
System.out.println( "Disk moves: " ); | |
solve( n ); | |
} else { | |
System.out.println( "Not a positive number" ); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment