home / experts / javascript / column45 |
|
Scrambling the PictureThe rule of the sliding puzzle is that you start with a scrambled picture and you try to put the pieces in their correct positions, in a minimum number of moves. Here is the scrambling function:
This function relies on the We scramble the puzzle board by simulating many moves by a player. We chose 3000 as a number that will assure a total randomization of the board. We always keep track of the blank square position,
The new attempted position
If it is not a valid switch, we immediately go to the next iteration with the
We explained swapping in Column 43. Notice that we swap whole nodes, with all their properties and children. For every successful swapping we need to update the current position of the blank square, |
Produced by Yehuda Shiran and Tomer Shiran
Created: August 2, 1999
Revised: August 2, 1999
URL: http://www.webreference.com/js/column45/scramble.html