0
\$\begingroup\$

I would like to create a game which consists of taking a random image, slicing it up into some number of pieces (according to a selected difficulty) and letting the player solve the puzzle.

I've googled this, but didn't find anything relevant that would at least help me understand how this kind of games are to be approached. I did come across this article Custom Sprite Batcher in LibGDX, but it is not complete and is rather confusing to me for now.

Any suggestions are much appreciated. Thanks.

\$\endgroup\$
1

1 Answer 1

1
\$\begingroup\$

You should probably use texture regions.

With texture regions you can select a sprite, doesn't necessarily have to be a sprite in your case, and slice it up in parts to be reused.

As example:

int numOfTiles_Horizontal = 3;//Can be any size as long as it lines up with the texture size.
int numOfTiles_Vertical = 3;//Can be any size as long as it lines up with the texture size.

float tile_width = texture.getWdith() / numOfTiles_Horizontal;
float tile_height = texture.getHeight() / numOfTiles_Vertical;

TextureRegion[][] sprites = new TextureRegion[numOfTiles_Horizontal][numOfTiles_Vertical];
for(int col = 0; col < numOfTiles_Horizontal; col++) {
    for(int row = 0; row < numOfTiles_Vertical; row++){
        sprites[c][r] = new TextureRegion(texture, c * tile_width, r * tile_height, tile_width, tile_height);
    }
}

This leaves you with and 2D array of textureregions of the original texture. The you can copy this one into a new one. Scramble the new one. And then add your slide puzzle mechanic. Then each time the player has moved a a slice just do this.

public Boolean checkPuzzleSolvedOrSomething(){
    Boolean[] isEqual = new Boolean[numOfTiles_Horizontal * numOfTiles_Vertical];
    int sprite_counter = 0;

    for(int col = 0; col < numOfTiles_Horizontal; col++) {
        for(int row = 0; row < numOfTiles_Vertical; row++){
            if(sprites_scrambled[c][r] == sprites[c][r]){isEqual[sprite_counter] = true; }
            else{isEqual[sprite_counter] = false; }
            sprite_counter++;
        }
    }

    for(int i = 0; i < isEqual.length; i++;){
        if(isEqual[i] == true){continue;}//If designated region is in the right place continue
        else{return false;}//Else return that the puzzle is not yet completed.
    }
    return false;//Should never reach this but is needed to complete method.
}

Hopes this helps.

\$\endgroup\$
1
  • \$\begingroup\$ Let me know if this helped, I am really curious as to how you will fix it c: \$\endgroup\$
    – Maarten
    Commented Apr 4, 2014 at 11:40

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .