8 has 6 inversions (it precedes 5, 1, 4, 6, 2, 3 ).7 has 6 inversions (it precedes 5, 1, 4, 6, 2, 3 ).We represent it as one dimensional array: Let’s imagine a 3×3 slide puzzle lite the one in this image: Randomly shuffling the pieces in the slide puzzle So the next thing to try would be this: we can represent a Slide Puzzle as a one dimensional array, what if we used an algorithm to shuffle the array? Let’s study this approach next. If we repeat the process enough times we would end up with a seemingly reasonable shuffled puzzle, still it seems a cumbersome and inefficient approach. Using this approach it could happen that we select the same piece twice to move it forward and back, for example, so that we would be shuffling the puzzle in one move and returning it back to its original state the next: not what we want! We could solve that problemīy keeping track of the pieces we have already moved and selecting when possible one we have not moved yet. We start with a solved puzzle, we select randomly one of the possible pieces to slide and we move it, and we repeat the process N times. The simplest way to shuffle our puzzle would be what I call the manual process, since it kind of replicates the process we would use if instead of a software puzzle we were playing with a typical plastic toy one. In this post we are going to talk about the algorithm to shuffle the slide puzzle, and how this apparently simple process has a few surprises in store for an unaware developer. Have you ever considered how you would implement your own Slide Puzzle Game? If so you probably have considered stuff like the user interaction sliding the pieces around, how to check when the puzzle is solved, and how to shuffle the slide puzzle every time the user selects new game. Slide puzzles: simple, addictive and fun.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |