Not a troll post, let’s simplify.
Now from what I read, you are simply wanting to shuffle objects around in an array, right? Take texture regions and everything else out of the equation, it does not matter the data type. We simply want to swap values.
Lets start simple:
I am sure you are familiar with the main method but anyway:
public class Main {
/**
* @param args
*/
public static void main(String[] args) {
new ArrayShuffler();
}
}
So we now have a new instance of the ArrayShuffler, all the work is done in the constructor (don’t do this :p) so let’s take a look. I have commented it well enough that hopefully you understand:
public class ArrayShuffler {
// Just create a very basic array with 2 elements, we will swap them about
PuzzlePiece[] puzzlePieces = new PuzzlePiece[2];
public ArrayShuffler() {
// Fill the array with new puzzle pieces and pass their index into them,
// so we can print out data
for (int x = 0; x < puzzlePieces.length; x++) {
puzzlePieces[x] = new PuzzlePiece(x);
}
/*
* So lets make 2 local variables, we grab the first element we want to swap and the second element we want to swap
*/
PuzzlePiece p1 = puzzlePieces[0];
PuzzlePiece p2 = puzzlePieces[1];
/*
* Now we basically put p1 into where p2 used to be and vice versa
*/
puzzlePieces[0] = p2;
puzzlePieces[1] = p1;
/*
* Iterate over the array, we grab the first piece in the array and pass in the current element we are checking.
* We then go ahead and print the data inside
*/
for(int x = 0; x < puzzlePieces.length; x++){
puzzlePieces[x].setNewIndex(x).print();
}
}
}
The output of the process:
I was first at index 1, now I am at index 0
I was first at index 0, now I am at index 1
Here is what the PuzzlePiece class looks like, all it does is append a string and print it:
public class PuzzlePiece {
StringBuilder stringBuilder = new StringBuilder();
public PuzzlePiece(int index) {
stringBuilder.append("I was first at index " + String.valueOf(index));
}
public PuzzlePiece setNewIndex(int newIndex){
stringBuilder.append(", now I am at index " + String.valueOf(newIndex));
return this;
}
public void print(){
System.out.println(stringBuilder);
}
}
Now, lets apply this to something a bit more well, random.
StringBuilder stringBuilder = new StringBuilder();
public PuzzlePiece(int indexX, int indexY) {
stringBuilder.append("I was first at index " + String.valueOf(indexX) + ":" + String.valueOf(indexY));
}
public PuzzlePiece setNewIndex(int newIndexX, int newIndexY){
stringBuilder.append(", now I am at index " + String.valueOf(newIndexX) + ":" + String.valueOf(newIndexY));
return this;
}
public void print(){
System.out.println(stringBuilder);
}
}
And for the actual shuffling bit, here it is and hopefully I have commented it enough for you to make use of it.
public class ArrayShuffler {
// Just create an array with 3*3 elements, we will swap them about
PuzzlePiece[][] puzzlePieces = new PuzzlePiece[3][3];
public ArrayShuffler() {
/*
* We are going to create a 2d array of puzzle peices and pass their X
* and Y indexes (I usually presume these are coordinates since face it,
* 2d arrays are create with tiles and coordinates!
*/
for (int x = 0; x < puzzlePieces.length; x++) {
for (int y = 0; y < puzzlePieces.length; y++) {
puzzlePieces[x][y] = new PuzzlePiece(x, y);
}
}
/*
* So lets make 2 local variables, we grab the first element we want to
* swap and the second element we want to swap
*/
PuzzlePiece p1;
PuzzlePiece p2;
/*
* These are the indexes of each puzzle piece, x1 and y1 being the index
* of the first element we want to swap, x2 and y2 being the index of
* the second element we want to swap
*/
int x1;
int x2;
int y1;
int y2;
int maxShuffles = 50;
for (int currentShuffles = 0; currentShuffles < maxShuffles; currentShuffles++) {
/*
* Now pick a the indexes at random
*/
x1 = MathUtils.random(0, puzzlePieces.length - 1);
y1 = MathUtils.random(0, puzzlePieces[0].length - 1);
x2 = MathUtils.random(0, puzzlePieces.length - 1);
y2 = MathUtils.random(0, puzzlePieces[0].length - 1);
/*
* Now the fun part! Lets simply get a copy of the elements that are
* at [x1][y1] and [x2][y2]
*/
p1 = puzzlePieces[x1][y1];
p2 = puzzlePieces[x2][y2];
/*
* Last but not least, the part you have been waiting for. Lets swap
* them :D
*/
puzzlePieces[x1][y1] = p2;
puzzlePieces[x2][y2] = p1;
}
/*
* Now lets check to ensure that all our pieces are at different indexes
* from what they started at. We could have done this in the loop above
* but I am sure your CPU will cope with the 1 extra loop :p
*/
for (int x = 0; x < puzzlePieces.length; x++) {
for (int y = 0; y < puzzlePieces.length; y++) {
puzzlePieces[x][y].setNewIndex(x, y).print();
}
}
}
}
And the output:
I was first at index 2:1, now I am at index 0:0
I was first at index 1:1, now I am at index 0:1
I was first at index 0:2, now I am at index 0:2
I was first at index 0:0, now I am at index 1:0
I was first at index 0:1, now I am at index 1:1
I was first at index 1:0, now I am at index 1:2
I was first at index 2:2, now I am at index 2:0
I was first at index 1:2, now I am at index 2:1
I was first at index 2:0, now I am at index 2:2