package com.example.uitest;

import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.widget.ImageButton;
import android.widget.TextView;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

public class MainActivity extends AppCompatActivity {

private ImageButton[] imageButtons = new ImageButton[8];
private boolean[] isFlipped = new boolean[8];
private int[] cardImages = {R.drawable.photo1, R.drawable.photo2, R.drawable.photo3, R.drawable.photo4,R.drawable.photo1, R.drawable.photo2, R.drawable.photo3, R.drawable.photo4};
private List<Integer> matchedIndices = new ArrayList<>();
private int visibleCardCount = imageButtons.length;
TextView tvMove;
int countMove;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tvMove = findViewById(R.id.tvMove);
imageButtons[0] = findViewById(R.id.card1);
imageButtons[1] = findViewById(R.id.card2);
imageButtons[2] = findViewById(R.id.card3);
imageButtons[3] = findViewById(R.id.card4);
imageButtons[4] = findViewById(R.id.card5);
imageButtons[5] = findViewById(R.id.card6);
imageButtons[6] = findViewById(R.id.card7);
imageButtons[7] = findViewById(R.id.card8);

// Add the remaining ImageButtons

List<Integer> indices = new ArrayList<>();
for (int i = 0; i < imageButtons.length; i++) {
indices.add(i);
}

Collections.shuffle(indices); // Shuffle the indices randomly

for (int i = 0; i < imageButtons.length; i++) {
final int index = i;
final int randomIndex = indices.get(i);

imageButtons[randomIndex].setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (matchedIndices.size() % 2 == 0) {
// Only allow clicking when there are no flipped pairs

if (!isFlipped[randomIndex] && !matchedIndices.contains(randomIndex)) {
imageButtons[randomIndex].setImageResource(cardImages[randomIndex]);
isFlipped[randomIndex] = true;
countFlipped++;

if (countFlipped == 2) {
// Disable clicking on other cards
disableClickOnCards(randomIndex);
}

checkMatchedPairs(); // Check for matching pairs after each click
} else {
imageButtons[randomIndex].setImageResource(R.drawable.photo5);
isFlipped[randomIndex] = false;
countFlipped–;
}
} else {
// Disallow clicking when there are already 2 flipped cards
Toast.makeText(MainActivity.this, ““Please wait for the cards to flip back””, Toast.LENGTH_SHORT).show();
}
}
});
}
}

private void disableClickOnCards(int currentClickedIndex) {
for (int i = 0; i < imageButtons.length; i++) {
if (i != currentClickedIndex) {
imageButtons[i].setEnabled(false);
}
}
}

private void enableClickOnAllCards() {
for (int i = 0; i < imageButtons.length; i++) {
imageButtons[i].setEnabled(true);
}
}

private void checkMatchedPairs() {
int countFlipped = 0; // Count the number of flipped cards
int previousFlippedIndex = -1; // Index of the previously flipped card

boolean allMatched = true; // Flag to track if all matches have been found

// Check if all matches have been found
for (int i = 0; i < cardImages.length; i++) {
if (!matchedIndices.contains(i)) {
allMatched = false;
break;
}
}

if (allMatched) {
// Player has finished all matches
Toast.makeText(this, "Finish! Your Move: " + countMove, Toast.LENGTH_SHORT).show();
// You can perform any necessary actions or show a dialog box here
return;
}

for (int i = 0; i < isFlipped.length; i++) {
if (isFlipped[i]) {
countFlipped++;

if (countFlipped == 1) {
previousFlippedIndex = i;
} else if (countFlipped == 2) {
if (cardImages[previousFlippedIndex] == cardImages[i]) {
// Matched pair found
matchedIndices.add(previousFlippedIndex);
matchedIndices.add(i);
setCardsInvisible();

if (visibleCardCount == 0) {
tvMove.setText("Finish! Your Move: " + countMove);
} else {
countMove += 1;
tvMove.setText("Move: " + countMove);
}

// Reset flipped status
isFlipped[previousFlippedIndex] = false;
isFlipped[i] = false;
countMove += 1;
tvMove.setText("Move: " + countMove);
} else {
// Not a match
isFlipped[previousFlippedIndex] = false;
isFlipped[i] = false;

// Show the first card for a brief moment before flipping back
final int finalPreviousIndex = previousFlippedIndex;
final int finalCurrentIndex = i;

imageButtons[previousFlippedIndex].setImageResource(cardImages[previousFlippedIndex]);
imageButtons[i].setImageResource(cardImages[i]);
imageButtons[previousFlippedIndex].setEnabled(false);
imageButtons[i].setEnabled(false);

Handler handler = new Handler();
handler.postDelayed(new Runnable() {
@Override
public void run() {
// Flip the cards back
imageButtons[finalPreviousIndex].setImageResource(R.drawable.photo5);
imageButtons[finalCurrentIndex].setImageResource(R.drawable.photo5);
imageButtons[finalPreviousIndex].setEnabled(true);
imageButtons[finalCurrentIndex].setEnabled(true);
}
}, 1000); // 1 second delay before flipping back

}

countFlipped = 0; // Reset flipped count
}
}
}
}

private void setCardsInvisible() {
for (int index : matchedIndices) {
imageButtons[index].setVisibility(View.INVISIBLE);

// Additional actions or dialog box can be added here if desired

}

visibleCardCount -= 2;
}
} how to fix error