I'm trying to code this game in processing where you have to click on four tiles in the correct order. If you click on the tiles in the correct order, it's supposed to display "You won!" in the console, and if you mess up, it's meant to say "Try again" and reset. The tiles are supposed to shuffle randomly at the beginning of each try.
However, the game fails me and resets before I finish clicking all tiles. How do I fix this? This is what I have so far:
PImage[] images = new PImage[4];
int[] order = {1, 2, 3, 4};
int[] shuffledOrder;
int currentIndex = 0;
boolean gameWon = false;
void setup() {
size(400, 400);
images[0] = loadImage("1.jpg");
images[1] = loadImage("2.jpg");
images[2] = loadImage("3.jpg");
images[3] = loadImage("4.jpg");
shuffleImages();
}
void draw() {
background(255);
int imageSize = width / 2;
// Display shuffled images in a 2x2 grid
int index = 0;
for (int i = 0; i < 2; i++) {
for (int j = 0; j < 2; j++) {
image(images[shuffledOrder[index] - 1], j * imageSize, i * imageSize, imageSize, imageSize);
index++;
}
}
}
void mouseClicked() {
int imageSize = width / 2;
int clickedIndex = mouseY / imageSize * 2 + mouseX / imageSize;
if (!gameWon) {
if (clickedIndex == order[currentIndex]) {
currentIndex++;
if (currentIndex == 4) {
gameWon = true;
println("You won!");
currentIndex = 0;
shuffleImages();
}
} else {
println("Try again");
currentIndex = 0;
shuffleImages();
}
}
}
void shuffleImages() {
shuffledOrder = shuffleArray(order);
}
int[] shuffleArray(int[] array) {
int[] newArray = array.clone();
for (int i = newArray.length - 1; i > 0; i--) {
int index = (int) random(i + 1);
int temp = newArray[index];
newArray[index] = newArray[i];
newArray[i] = temp;
}
return newArray;
}
one of your if else curly bracket in mouseClicked function is misplaced which made it to always triggers the 'else-> try again' path.