while( s[0] + s[1] ) //while first and second pegs aren't empty
{
//next ring to move is smaller of rings on the two pegs not moved onto last
if( last == 0 ) next = z[width +s[1]] < z[2 * width + s[2]] ? 1 : 2;
if( last == 1 ) next = z[s[0]] < z[2 * width + s[2]] ? 0 : 2;
if( last == 2 ) next = z[s[0]] < z[width + s[1]] ? 0 : 1;
//top ring of 'to' peg must be larger and an even 'distance' away
if( ( z[next * width + s[next]] > z[last * width + s[last]] ) ||
( ( z[last * width + s[last]] - z[next * width + s[next]] ) % 2 == 0 ) )
last = 3 - next - last;