






Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!







public void movePile(int n, int from, int to, int via){ /* TODO */ }
// hanoi.java class HanoiException extends RuntimeException { public HanoiException(String err) { super(err); } } public class Hanoi { /*
* moveOneRing(0,1) * moveOneRing(0,2) * moveOneRing(1,2) * moveOneRing(0,1) * moveOneRing(2,0) * moveOneRing(2,1) * moveOneRing(0,1) ** * where 0 is tower A, 1 is tower B, 2 is tower C. * * @param n a counter that, when n==1 means we've removed all rings above the * bottom ring * @param from the index of the tower to move all rings from * @param to the index of the tower to move all rings to * @param via the index of the tower which intermediate rings will be moved to */ //from is the source tower, to is the destination and via is the helper tower public void movePile(int n, int from, int to, int via) { if (n == 1 ) { moveOneRing(from, to); } else { movePile(n- 1 , from, via, to); moveOneRing(from, to); movePile(n- 1 , via, to, from); } } /** * @param i the index of the tower, starting indexing at 0. * @return the Hanoi stack. */ public Stack getTower(int i) { return towers[i]; } public void playHanoi() { /// move all rings from tower A (0), to tower B (1), via tower C (2)
movePile(numRings, 0 , 1 , 2 ); } /**
@Test public void playSixRings() { playNRings(6); } @Test public void playSevenRings() { playNRings(7); } @Test public void playEightRings() { playNRings(8); } @Test public void playNineRings() { playNRings(9); } @Test public void playTenRings() { playNRings(10); } @Test public void playTwentyRings() { playNRings(20); } /**
capacity = size; S = new Object[size]; top = -1; } public Stack(){ this(DEFAULT_SIZE); } public boolean isEmpty(){ return top < 0; } public int size(){