Cant keep open both cards in memorygame using ArrayLists

Hello,

I have made a memorygame using processing. We arent allowed to use classes. So i have used ArrayLists instead.

Now i just cant seem to fix this bug: “when i click open the second card it sometime immediatly closes, sometimes it stays open untill i click somewhere else in the GUI”.

These 2 parts of the code are for the cards and turning them:"

void gameScherm(ArrayList<PImage> kaarten, ArrayList<Boolean> kaartenOpen, ArrayList<PImage> geselecteerdeKaarten) {
  boolean kaartenGelijk = false;
  background(62, 86, 120);
  int hoogte = 50;
  int breedte = 50;
  PImage achterkant;
  achterkant = loadImage("Achterkant.png");

  ////Dat achterlijke tekstveld van cp5 weg werpen uit het scherm
  naamVeld.setPosition(width/2 - 1000, 80);

  int xStart = 50;
  int yStart = 10;
  for (int i = 0; i < kaarten.size(); i++) {
    if (i % 5 == 0) {
      xStart = 50;
      yStart += 60;

      image(achterkant, xStart, yStart, breedte, hoogte);
    } else {
      xStart = xStart + 60;
      image(achterkant, xStart, yStart, breedte, hoogte);
    }

    if (mouseX >= xStart && mouseX <= xStart + breedte && mouseY >= yStart && mouseY <= yStart + hoogte) {
      if (mousePressed) {
        if (openStaandeKaarten < 2) {
          if (kaartenOpen.get(i) == false) {
            image(kaarten.get(i), xStart, yStart, breedte, hoogte);
            geselecteerdeKaarten.add(kaarten.get(i));
            checkenDoodsKaart();
            kaartenOpen.set(i, true);
            openStaandeKaarten++;
          }
        }
      }
      //println("Muis is op locatie " + i);
    } else {
      if (kaartenOpen.get(i) == true) {
        image(kaarten.get(i), xStart, yStart, breedte, hoogte);
      }
    }
  }

ArrayList<Integer> matchedCardIndexes = new ArrayList<Integer>();

void checkenOpPunt() {
  boolean kaartenGelijk;
  //int aantalKaarten = 0; DEZE KAN WEG DUS
  if (openStaandeKaarten == 2) {
    // Get the two selected cards
    PImage kaart1 = geselecteerdeKaarten.get(0);
    PImage kaart2 = geselecteerdeKaarten.get(1);
    beurten++;
    maxAantalPogingen--;

    // Check if the cards match
    if (Arrays.equals(kaart1.pixels, kaart2.pixels)) {
      // The cards match, increment the score and clear the selected cards
      kaartenGelijk = true;
      scoreSpeler1++;
      geselecteerdeKaarten.clear();
      openStaandeKaarten = 0;

      // Remove the matched cards from the kaarten and kaartenOpen arrays
      int index1 = kaarten.indexOf(kaart1);
      kaarten.remove(index1);
      kaartenOpen.remove(index1);
      int index2 = kaarten.indexOf(kaart2);
      kaarten.remove(index2);
      kaartenOpen.remove(index2);

      // Update the size of the arrays
      //aantalKaarten -= 2; DEZE KAN WEG DUS
 } else {
      // De kaarten komen niet overeen, kaarten na timer weer dichtdraaien
      kaartenGelijk = false;
      int sluitTijd = millis() + 1000;
      for (int i = 0; i < kaarten.size(); i++) {
        if (kaarten.get(i) == kaart1 || kaarten.get(i) == kaart2) {
          kaartenOpen.set(i, true);
        }
      }
      geselecteerdeKaarten.clear();
      openStaandeKaarten = 0;
      while (millis() < sluitTijd) {
        // Wait for the timer to expire
      }
      for (int i = 0; i < kaarten.size(); i++) {
        if (kaartenOpen.get(i)) {
          kaartenOpen.set(i, false);
        }
      }
    }
  }
}

Hi @Yokiyami,

Welcome to the forum! :wink:

(disclaimer: ArrayList is also a class eheh :yum:)

I think the issue is caused by your use of the mousePressed variable.

Since the draw() function is being executed roughly 60 times per second, if you check something related to the mousePressed variable then from one frame to another it can be True then flipping your card twice if you keep pressing the mouse.

See this example:

void draw() {
  if (mousePressed) {
    println("mousePressed true at frame " + frameCount);
  }
}

void mousePressed() {
  println("mousePressed()");
  // Flip your card here...
}

If I click only once on the canvas, I get this:

mousePressed()
mousePressed true at frame 35
mousePressed true at frame 36
mousePressed true at frame 37
mousePressed true at frame 38

So you see that it’s triggering the condition multiple times which is not what you want.

Instead you should be using the mousePressed() function (as shown above) which is triggered only once per click.

1 Like

I have tried that in multiple ways, i just cant seem to get it work… FML

I can’t help more without any code :wink:

What you should do is make a simple proof of example of your game without any external files (images we can’t test)

I dont even have a clue how to make that simple proof of example… Yeah i could start from scratch, or start replacing all the image related code and use something else instead of that, but i have a deadline this sunday and i have a report to write also, so i really hope someone can help me in the right direction…

Dont know if this is possible, but i have uploaded it via wetransfer, i dont want my code to be publicly available so everyone can use it. Can i DM it or something?

Try to put this code into the mousePressed() function.

1 Like