protected boolean swapTiles ( int x1 , int y1 , int x2 , int y2 ) { // Used to indicate that one of the swapped tiles was the empty tile boolean swappedEmpty = false ; // Get the tile at the first position char tile1 = board [ y1 ] [ x1 ] ; // Store the tile from the second position at the first position char tile2 = board [ y2 ] [ x2 ] ; board [ y1 ] [ x1 ] = tile2 ; // Store the first tile in the second position board [ y2 ] [ x2 ] = tile1 ; // Check if the first tile was the empty tile and update the empty tile coordinates if so if ( tile1 == ' ' ) { emptyX = x2 ; emptyY = y2 ; swappedEmpty = true ; } // Else check if the second tile was the empty tile and update the empty tile coordinates if so else if ( tile2 == ' ' ) { emptyX = x1 ; emptyY = y1 ; swappedEmpty = true ; } return ! swappedEmpty ; }
Swaps the two tiles at the specified coordinates . One of the tiles may be the empty tile and the empty tile position will be correctly updated . If neither of the tiles is empty then this is an illegal swap in which case the method returns true .