Matching ImageViews in android studios by dragging, using java

24 views Asked by At

Creating a guessing game by placing images on top of a row of boxes to guess the image under them. when all correct it's suppose to pick up as match. But Game is sometimes picking up wrong matches. and when I drop tolerance it doesn't pick up any. I think it my match method but don't know how to go about it anymore.

public class StageOne extends AppCompatActivity {

List<ImageView> imageViews = new ArrayList<>();
List<ImageView> targetViews = new ArrayList<>();
float[] xDown;
float[] yDown;
float screenWidth;
float screenHeight;
/*float tolerance = 18; // Define your tolerance for successful match*/
ImageView winMessageImageView;
private Button button;
private Button button2;
private Button button3;
private int clickCount = 0;
private int clickCount2 = 0;
ImageView boxImageView1;
ImageView boxImageView2;
ImageView boxImageView3;
ImageView lifeImageView1;
ImageView lifeImageView2;
ImageView lifeImageView3;
ImageView gameOverImageView;
ImageView fireworkImageView;
ImageView fireworkImageView2;
ImageView fireworkImageView3;
ImageView fireworkImageView4;

public void launchStageTwo(View v) {
    Intent i = new Intent(this, StageTwo.class);
    startActivity(i);
}


@SuppressLint({"ClickableViewAccessibility", "MissingInflatedId"})
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_stage_one);

    winMessageImageView = findViewById(R.id.youWon);
    button = findViewById(R.id.help);
    button2 = findViewById(R.id.check);
    button3 = findViewById(R.id.nextstage);
    boxImageView1 = findViewById(R.id.box1);
    boxImageView2 = findViewById(R.id.box2);
    boxImageView3 = findViewById(R.id.box3);
    lifeImageView1 = findViewById(R.id.lifebarthree);
    lifeImageView2 = findViewById(R.id.lifebartwo);
    lifeImageView3 = findViewById(R.id.lifebarone);
    gameOverImageView = findViewById(R.id.gameover1);
    fireworkImageView = findViewById(R.id.firework);
    fireworkImageView2 = findViewById(R.id.firework2);
    fireworkImageView3 = findViewById(R.id.firework3);
    fireworkImageView4 = findViewById(R.id.firework4);

    Glide.with(this)
            .asGif()
            .load(R.drawable.fireworks2)
            .diskCacheStrategy(DiskCacheStrategy.NONE) // Disable disk caching for GIFs
            .into(fireworkImageView);

    Glide.with(this)
            .asGif()
            .load(R.drawable.fireworks2)
            .diskCacheStrategy(DiskCacheStrategy.NONE) // Disable disk caching for GIFs
            .into(fireworkImageView2);

    Glide.with(this)
            .asGif()
            .load(R.drawable.fireworks)
            .diskCacheStrategy(DiskCacheStrategy.NONE) // Disable disk caching for GIFs
            .into(fireworkImageView3);

    Glide.with(this)
            .asGif()
            .load(R.drawable.fireworks3)
            .diskCacheStrategy(DiskCacheStrategy.NONE) // Disable disk caching for GIFs
            .into(fireworkImageView4);


    imageViews.add(findViewById(R.id.star1));
    imageViews.add(findViewById(R.id.coin1));
    imageViews.add(findViewById(R.id.peach1));

    targetViews.add(findViewById(R.id.star));
    targetViews.add(findViewById(R.id.coin));
    targetViews.add(findViewById(R.id.peach));

    xDown = new float[imageViews.size()];
    yDown = new float[imageViews.size()];


    Display display = getWindowManager().getDefaultDisplay();
    Point size = new Point();
    display.getSize(size);
    screenWidth = size.x;
    screenHeight = size.y;


    button.setOnClickListener(v -> {
        // Increment the click count
        clickCount++;

        if (clickCount == 1) {
            boxImageView1.setVisibility(View.GONE);
        }

        // Check if the click count is 2, and disable the button if it is
        if (clickCount == 2) {
            boxImageView2.setVisibility(View.GONE);
            button.setEnabled(false);
        }
    });

    button2.setOnClickListener(v -> {
        // Increment the click count
        clickCount2++;

        if (clickCount2 == 1 && allTargetsMatched()) {
            winMessageImageView.setVisibility(View.VISIBLE);
            boxImageView1.setVisibility(View.INVISIBLE);
            boxImageView2.setVisibility(View.INVISIBLE);
            boxImageView3.setVisibility(View.INVISIBLE);

            fireworkImageView.setVisibility(View.VISIBLE);
            fireworkImageView2.setVisibility(View.VISIBLE);
            fireworkImageView3.setVisibility(View.VISIBLE);
            fireworkImageView4.setVisibility(View.VISIBLE);

            button3.setVisibility(View.VISIBLE);
        } else if (clickCount2 == 1) {
            lifeImageView1.setVisibility(View.GONE);
        }
        if (clickCount2 == 2 && allTargetsMatched()) {
            winMessageImageView.setVisibility(View.VISIBLE);
            boxImageView1.setVisibility(View.INVISIBLE);
            boxImageView2.setVisibility(View.INVISIBLE);
            boxImageView3.setVisibility(View.INVISIBLE);

            fireworkImageView.setVisibility(View.VISIBLE);
            fireworkImageView2.setVisibility(View.VISIBLE);
            fireworkImageView3.setVisibility(View.VISIBLE);
            fireworkImageView4.setVisibility(View.VISIBLE);

            button3.setVisibility(View.VISIBLE);
        } else if (clickCount2 == 2) {
            lifeImageView2.setVisibility(View.GONE);
        }
        if (clickCount2 == 3 && allTargetsMatched()) {
            winMessageImageView.setVisibility(View.VISIBLE);
            boxImageView1.setVisibility(View.INVISIBLE);
            boxImageView2.setVisibility(View.INVISIBLE);
            boxImageView3.setVisibility(View.INVISIBLE);

            fireworkImageView.setVisibility(View.VISIBLE);
            fireworkImageView2.setVisibility(View.VISIBLE);
            fireworkImageView3.setVisibility(View.VISIBLE);
            fireworkImageView4.setVisibility(View.VISIBLE);

            button3.setVisibility(View.VISIBLE);
        } else if (clickCount2 == 3) {
            lifeImageView3.setVisibility(View.GONE);
            gameOverImageView.setVisibility(View.VISIBLE);
        }
    });

    for (int i = 0; i < imageViews.size(); i++) {
        final ImageView imageView = imageViews.get(i);
        final int finalI = i;

        imageView.setOnTouchListener((v, event) -> {
            switch (event.getAction()) {

                //touch
                case MotionEvent.ACTION_DOWN:
                    xDown[finalI] = event.getX();
                    yDown[finalI] = event.getY();
                    break;

                //move finger
                case MotionEvent.ACTION_MOVE:
                    float movedX = event.getX();
                    float movedY = event.getY();

                    // Calculate the movement
                    float distanceX = movedX - xDown[finalI];
                    float distanceY = movedY - yDown[finalI];

                    // Calculate the new X position
                    float newX = imageView.getX() + distanceX;
                    float newY = imageView.getY() + distanceY;

                    // Limit the movement to stay within the screen bounds
                    if (newX >= 0 && (newX + imageView.getWidth()) <= screenWidth) {
                        imageView.setX(newX);
                    }
                    if (newY >= 0 && (newY + imageView.getHeight()) <= screenHeight) {
                        imageView.setY(newY);
                    }

                    // Check if the imageView is near a target
                    for (int j = 0; j < targetViews.size(); j++) {
                        ImageView target = targetViews.get(j);
                        if (isMatch(imageView, target)) {
                            break; // Exit the loop if a match is found
                        }
                    }
                    break;

                // Release
                case MotionEvent.ACTION_UP:
                    break;
            }
            return true;
        });
    }
}

private boolean isMatch(ImageView imageView, ImageView target) {

    Rect imageViewRect = new Rect();
    imageView.getGlobalVisibleRect(imageViewRect);

    Rect targetRect = new Rect();
    target.getGlobalVisibleRect(targetRect);

    // Check if the bounding rectangles intersect within tolerance
    if (Rect.intersects(imageViewRect, targetRect)) {

        Rect intersection = new Rect();
        if (intersection.setIntersect(imageViewRect, targetRect)) {
            int intersectionArea = intersection.height();
            int imageViewArea = imageViewRect.height();

            return intersectionArea >= 0.8 * imageViewArea;
        }
    }

    return false;
}

private boolean allTargetsMatched() {
    for (ImageView target : targetViews) {
        boolean targetMatched = false;
        for (ImageView imageView : imageViews) {
            if (isMatch(imageView, target)) {
                targetMatched = true;
                break; // 
            }
        }
        if (!targetMatched) {
            return false; 
        }
    }
    return true;
}

}

I tried changing tolerance but doesn't pick up at all. I changed the game logic from images being placed in a frame directly above the box to having images placed on top of boxes to be directly on top of images being guessed but still when slightly out of place it doesn't pick up. I wish to bring back this previous layout too.

0

There are 0 answers