how to fix the error package com.example.test;

import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageButton;
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};

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        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 (!isFlipped[randomIndex]) {
                        imageButtons[randomIndex].setImageResource(cardImages[index]);
                        isFlipped[randomIndex] = true;

                        checkMatchedPairs(); // Check for matching pairs after each click
                    } else {
                        imageButtons[randomIndex].setImageResource(R.drawable.photo5);
                        isFlipped[randomIndex] = false;
                    }
                }
            });

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

                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
                                // Set visibility to invisible for both matched cards
                                imageButtons[previousFlippedIndex].setVisibility(View.INVISIBLE);
                                imageButtons[i].setVisibility(View.INVISIBLE);

                                // Reset flipped status
                                isFlipped[previousFlippedIndex] = false;
                                isFlipped[i] = false;
                            } else {
                                // Reset flipped status and set card background
                                imageButtons[previousFlippedIndex].setImageResource(R.drawable.photo5);
                                imageButtons[i].setImageResource(R.drawable.photo5);
                                isFlipped[previousFlippedIndex] = false;
                                isFlipped[i] = false;
                            }

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