I’d like to make a shuffle puzzle, but running into some issues:
I have a texture that i split up in 3x3 pieces using textureRegion.
puzzlePieces and puzzlePieces_scrambled are 2D TextureRegion arrays
and what I’m trying to do is shuffle up my original 2d array (puzzlePieces) and call it PuzzlePieces scrambled.
I had no clue how to do this. So I thought let’s just put all the pieces of the original TextureRegion array (puzzlePieces) into an array and use
the Array.shuffle() method to shuffle up the texarray (textureRegion array) and then loop over the texarray to provide the puzzlePieces_Scrambled[][] with the shuffled pieces.
Unfortunatley my puzzlePieces_Scrambled[][] contains pieces that are double and some pieces are not there.
It seems that array.shuffle() is not restricted when putting the content of a location to another place to not use that content again.
If you get what I mean…
How do I solve this?
Or maybe I am using a wrong approach? And/or overcomplicating things?
(and Libgdx (!) code examples for shuffle puzzle would be great too)
Here is my code:
int arrayIndex = 0;
puzzleTextures = new Texture("img/portret_JacobCornelisz800x480.jpg");
tile_width = puzzleTextures.getWidth() / numberOfTiles_hor; // =3
tile_height = puzzleTextures.getHeight() / numberOfTiles_ver; // =3
// create a 3x3 textureRegion[][]
puzzlePieces = TextureRegion.split(puzzleTextures, (int) tile_width,
(int) tile_height);
puzzlePieces_scrambled = puzzlePieces;
// put the textureRegion[][] in an Array (TextureRegion has no shuffle method -> array does....)
texarray = new Array<TextureRegion>();
for (int row = 0; row < numberOfTiles_hor; row++) {
for (int col = 0; col < numberOfTiles_ver; col++) {
texarray.add(puzzlePieces[row][col]);
}
}
// shuffle Array
texarray.shuffle();
//put shuffled array into 2nd TextureRegion for shuffled puzzle pieces
for (int row = 0; row < numberOfTiles_hor; row++) {
for (int col = 0; col < numberOfTiles_ver; col++) {
puzzlePieces_scrambled[row][col].setRegion(texarray
.get(arrayIndex));
System.out.println("arrayindex" + arrayIndex);
arrayIndex++;
}
}
// but unfortunately Array.shuffle() doesnt take every object in array just once :(