# Cant keep open both cards in memorygame using ArrayLists

**URL:** https://discourse.processing.org/t/cant-keep-open-both-cards-in-memorygame-using-arraylists/41263
**Category:** Beginners
**Tags:** homework
**Created:** [March 13, 2023, 1:30pm UTC](https://discourse.processing.org/t/cant-keep-open-both-cards-in-memorygame-using-arraylists/41263 "2023-03-13T13:30:36Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![Yokiyami](https://avatars.discourse-cdn.com/v4/letter/y/ecae2f/32.png) [@Yokiyami](https://discourse.processing.org/u/Yokiyami)
#### Post date: [March 13, 2023, 1:30pm UTC](https://discourse.processing.org/t/cant-keep-open-both-cards-in-memorygame-using-arraylists/41263/1 "2023-03-13T13:30:37Z")

</div>

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:"

```auto
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);
        }
      }
    }
  }
}

```

---

<div class="post-metadata">

### Author: ![josephh](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/josephh/32/210_2.png) [@josephh](https://discourse.processing.org/u/josephh)
#### Post date: [March 14, 2023, 12:34pm UTC](https://discourse.processing.org/t/cant-keep-open-both-cards-in-memorygame-using-arraylists/41263/2 "2023-03-14T12:34:18Z")

</div>

Hi @Yokiyami,

Welcome to the forum! 😉

> [@Yokiyami](#):
>
> We arent allowed to use classes. So i have used ArrayLists instead.

(disclaimer: `ArrayList` is also a class eheh 😋)

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:

```processing
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:

```plaintext
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.

---

<div class="post-metadata">

### Author: ![Yokiyami](https://avatars.discourse-cdn.com/v4/letter/y/ecae2f/32.png) [@Yokiyami](https://discourse.processing.org/u/Yokiyami)
#### Post date: [March 15, 2023, 1:25pm UTC](https://discourse.processing.org/t/cant-keep-open-both-cards-in-memorygame-using-arraylists/41263/3 "2023-03-15T13:25:56Z")

</div>

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

---

<div class="post-metadata">

### Author: ![josephh](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/josephh/32/210_2.png) [@josephh](https://discourse.processing.org/u/josephh)
#### Post date: [March 15, 2023, 2:10pm UTC](https://discourse.processing.org/t/cant-keep-open-both-cards-in-memorygame-using-arraylists/41263/4 "2023-03-15T14:10:05Z")

</div>

I can’t help more without any code 😉

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

---

<div class="post-metadata">

### Author: ![Yokiyami](https://avatars.discourse-cdn.com/v4/letter/y/ecae2f/32.png) [@Yokiyami](https://discourse.processing.org/u/Yokiyami)
#### Post date: [March 15, 2023, 2:36pm UTC](https://discourse.processing.org/t/cant-keep-open-both-cards-in-memorygame-using-arraylists/41263/5 "2023-03-15T14:36:30Z")

</div>

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?

---

<div class="post-metadata">

### Author: ![josephh](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/josephh/32/210_2.png) [@josephh](https://discourse.processing.org/u/josephh)
#### Post date: [March 15, 2023, 4:04pm UTC](https://discourse.processing.org/t/cant-keep-open-both-cards-in-memorygame-using-arraylists/41263/6 "2023-03-15T16:04:35Z")

</div>

> [@Yokiyami](#):
>
> ```auto
> 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);
> 
> ```

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