protected int swapTileToLocationCountingIllegal ( char t , int x , int y ) { // Used to hold the count of illegal swaps int illegal = 0 ; // Find out where the tile to move is int tileX = getXForTile ( t ) ; int tileY = getYForTile ( t ) ; // Shift the tile into the correct column by repeatedly moving it left or right. while ( tileX != x ) { if ( ( tileX - x ) > 0 ) { if ( swapTiles ( tileX , tileY , tileX - 1 , tileY ) ) { illegal ++ ; } tileX -- ; } else { if ( swapTiles ( tileX , tileY , tileX + 1 , tileY ) ) { illegal ++ ; } tileX ++ ; } } // Shift the tile into the correct row by repeatedly moving it up or down. while ( tileY != y ) { // Commented out because tiles never swap down the board during the solvability test because tiles are // swapped into place left to right, top to bottom. The top row is always filled first so tiles cannot be // swapped down into it. Then the next row is filled but ones from the row above are never swapped down // into it because they are alrady in place and never move again and so on. /* if (tileY - y > 0) *{*/ if ( swapTiles ( tileX , tileY , tileX , tileY - 1 ) ) { illegal ++ ; } tileY -- ; /*} * else { if (swapTiles(tileX, tileY, tileX, tileY + 1)) illegal++; tileY++;}*/ } return illegal ; }
Repeatedly swaps a tile with its neighbours until it reaches the specified location . If the tile is swapped with the empty tile then this is a legal move . If the tile is swapped with another non - empty tile then this is an illegal move and the total number of illegal moves is counted .