I am a beginner in java programming language
I tried to implement Fisher Yates's algorithm in my code, but when I run the application it doesn't randomize my puzzle. when I was looking for references I was confused about the correct location to place the command so I could randomize the puzzle.
previously I used collection.shuffle , but I was told to use fisher yates manual.
static void shuffleArray(int[] pieces)
{
Random rnd = new Random();
for (int i = pieces.length - 1; i > 0; i--)
{
int index = rnd.nextInt(i + 1);
// Swap
int a = pieces[index];
pieces[index] = pieces[i];
pieces[i] = a;
}
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_puzzle);
setName();
//Button to go to the StartActivity
goHome = findViewById(R.id.goHome);
goHome.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
goHome();
}
});
//timer-------------------------------------------------------
//counter initializing
countTimer = findViewById(R.id.count_timer);
countTimer.setText("OSecs");
timer.start();//starting timer
//-------------------------------------------------timer
RelativeLayout layout = findViewById(R.id.layout);
ImageView imageView = findViewById(R.id.imageView);
Intent intent = getIntent();
String assetName = intent.getStringExtra("assetName");
//picture from camera and gallery ------------------------------------------------
mCurrentPhotoPath = intent.getStringExtra("mCurrentPhotoPath");
mCurrentPhotoUri = intent.getStringExtra("mCurrentPhotoUri");
//-------------------------------------------------picture from camera
// run image related code after the view was laid out
// to have all dimensions calculated
imageView.post(new Runnable() {
@Override
public void run() {
if (assetName != null) {
setPicFromAsset(assetName, imageView);
} else if (mCurrentPhotoPath != null) { //added else if picture from camera
setPicFromPath(mCurrentPhotoPath, imageView);
} else if (mCurrentPhotoUri != null) {
imageView.setImageURI(Uri.parse(mCurrentPhotoUri));
}
pieces = splitImage();
TouchListener touchListener = new TouchListener(PuzzleActivity.this);
// shuffle pieces order
//Collections.shuffle(pieces);
// --- SWITCH FOR COMPLEXITY LEVELS ---
// positioning and size of pieces-------------------------------------------
int border = 28; //boarder size from xml
double ratio = (double) ((layout.getBottom() - imageView.getBottom()) - 2 * border) / imageView.getHeight();
if (ratio > 1) {
ratio = 1;
}
positioningOfPuzzlePieces(touchListener, border, getRows(), getCols(), ratio, layout, imageView);
}
});
}