No help is needed with this code. It was just the only way to share it without uploading the scenario itself.
The following is the world class the generates the solutions to the puzzle. It does not use recursion or iterating of any kind to solve the puzzle. It uses its current state and move count to determine the move that is to be made next, provided the puzzle has not yet been completed.
import greenfoot.*; public class Hanoi extends World { private int[] cts = new int[3]; // track the number of rings on each of the 3 pegs private static int ringCount = 8; // holds the total number of rings in the puzzle private Ring[] order = new Ring[ringCount]; // lists order of rings by size private Ring[][] rings = new Ring[3][ringCount]; // tracks the arrangement of rings on each of the 3 pegs Counter counter; // to track and display move count public Hanoi() { super(800, 400, 1); prepareBackground(); addActors(); } private void addActors() { // the rings (added to arrays as needed) for (int i=0; i<ringCount; i++) { rings[0][i] = new Ring(ringCount-1-i); addObject(rings[0][i], 200, 300-30*i); addRing(0, rings[0][i]); order[ringCount-1-i] = rings[0][i]; } // the move counter counter = new Counter("Moves: "); addObject(counter, 80, 25); } private void prepareBackground() { GreenfootImage bg = getBackground(); // the board with pegs bg.setColor(new java.awt.Color(128, 50, 50)); bg.fillRect(50, 316, 700, 30); bg.fillRect(195, 51, 10, 265); bg.fillRect(395, 51, 10, 265); bg.fillRect(595, 51, 10, 265); // the instructions and ring count text GreenfootImage text = new GreenfootImage("Use 'space' to execute a move", 24, java.awt.Color.black, null); bg.drawImage(text, 100, 360); text = new GreenfootImage("Use '3' through '8' to adjust ring count", 24, java.awt.Color.black, null); bg.drawImage(text, 400, 360); text = new GreenfootImage("Rings: "+ringCount, 24, java.awt.Color.black, null); bg.drawImage(text, 700, 15); } /** * removes the top ring from the array that tracks the arrangement of the rings on the pegs * and returns the ring removed from the array */ private Ring removeRing(int pegNum) { Ring ring = rings[pegNum][cts[pegNum]-1]; cts[pegNum]--; return ring; } /** * adds a ring to a peg in the array that tracks the arrangement of the rings on the pegs */ private void addRing(int pegNum, Ring ring) { rings[pegNum][cts[pegNum]] = ring; cts[pegNum]++; } /** * checks for keystrokes for move execution and setting ring count */ public void act() { String key = Greenfoot.getKey(); if ("space".equals(key)) executeMove(); if (key != null && "345678".indexOf(key) >= 0) { ringCount = "345678".indexOf(key)+3; Greenfoot.setWorld(new Hanoi()); } } /** * executes the next move in solving the puzzle if not solved yet */ private void executeMove() { // check if all moves have been executed if (counter.getValue() == (int)Math.pow(2, ringCount)-1) return; // puzzle completed int moves = counter.increment(); // get incremented move count // determine 'from' peg int bit = ringCount-1; while (moves%(int)Math.pow(2, bit) != 0) bit--; int from = (order[bit].getX()-200)/200; // remove ring from 'from' peg Ring ring = removeRing(from); // determine 'to' peg int to = from; // first possible 'to' peg with rings if (cts[(from+1)%3] != 0 && // rings on 'to' peg ring.getIndex() < rings[(from+1)%3][cts[(from+1)%3]-1].getIndex() && // top ring on 'to' peg is larger (rings[(from+1)%3][cts[(from+1)%3]-1].getIndex()+ring.getIndex())%2 == 1) // top ring on 'to' peg is legal { to = (from+1)%3; } // other possible 'to' peg with rings else if (cts[(from+2)%3] != 0 && // rings on 'to' peg ring.getIndex() < rings[(from+2)%3][cts[(from+2)%3]-1].getIndex() && // top ring on 'to' peg is larger (rings[(from+2)%3][cts[(from+2)%3]-1].getIndex()+ring.getIndex())%2 == 1) // top ring on 'to' peg is legal { to = (from+2)%3; } // empty 'to' pegs else if (cts[(from+1)%3] == 0) to = (from+1)%3; else to = (from+2)%3; // 'to' peg is empty moveRing(ring, from, to); addRing(to, ring); } /** * animates the movement of a ring from one peg to another */ private void moveRing(Ring ring, int from, int to) { while (ring.getY() != 30) { ring.setLocation(ring.getX(), ring.getY()-10); Greenfoot.delay(1); } int dir = (int)Math.signum(to-from); while (ring.getX() != 200+200*to) { ring.setLocation(ring.getX()+dir*10, ring.getY()); Greenfoot.delay(1); } int targetY = 300-30*(cts[to]); while (ring.getY() != targetY) { ring.setLocation(ring.getX(), ring.getY()+10); Greenfoot.delay(1); } } }