mzs2000 mzs2000 - 11 days ago 6
Java Question

Java memory game card flipping not working

I am working on a java memory game, but I have a problem, what I can't solve. If I click on any card, it turns up, after that I click on the second card, but it doesn't turn up. If they matched after the checking the second card flips up.

Card.class

public class Card{

private int img_x, img_y;
private Img img;
private boolean flipped = false;
private boolean flippable = true;

private static Img back_img;
private static int WIDTH, HEIGHT;

public Card(int img_x, int img_y, img img) {
this.img_x = img_x;
this.img_y = img_y;
this.img = img;
}

public void draw(Graphics g) {
if (flipped) {
g.drawImage(img.getimg(), img_x, img_y, WIDTH, HEIGHT, null);
} else {
g.drawImage(back_img.getimg(), img_x, img_y, WIDTH, HEIGHT, null);
}
}

public void flip() {
if (flippable) {
this.flipped = !flipped;
}
}


ClickEvent

public void clickEvent(int x, int y) {
for (Card card : cards) {
if ((card.getImg_x() < x) && (card.getImg_x() + card.getWIDTH() > x)
&& (card.getImg_y() < y) && (card.getImg_y() + card.getHEIGHT() > y)) {
if (card.isFlippable()) {
card.flip();
selectedCards.add(card);
}
break;
}
}
gamePanel.reapint();
if (selectedCards.size() == 2) { // if 2 card flipped up
checkFlippedCards(); //check the 2 cards' images name, if matched it lock the cards to not flippable
}
gamePanel.reapint();
}


I spend two days to solve this problem, but I have no clue why is it not working. The same method run when I click to any card but only one card flips up as it should.

CheckFlippedCards

private void checkFlippedCards() {

int name1 = selectedCards.get(0).getImgName();
int name2 = selectedCards.get(1).getImgName();
if (name1 == name2) {
score++;
selectedCards.get(0).setFlippable(false);
selectedCards.get(1).setFlippable(false);
}

try {
Thread.sleep(1000);
} catch (InterruptedException ex) {
Logger.getLogger(Vezerlo.class.getName()).log(Level.SEVERE, null, ex);
}

selectedCards.get(0).flip();
selectedCards.get(1).flip();

kivalasztottKartyak.clear();
}

skw skw
Answer

If I understand your code correctly, you are flipping the second card, then checking if both cards are equal, if they are then they stay flipped.

The problem is when they are not, as they are flipped back again before the panel is repainted.

The fact that you have a Thread.sleep does not mean that it will refresh, because the repainting is also done in the same thread that is sleeping.

What I suggest is to put the checking of cards flipped on another thread so that you can refresh your main thread without it interfering with the repainting.

Another solution would be for you to not flip the cards automatically when they are not equal. So, say, they should be flipped back in the next click. This could be done by moving

selectedCards.get(0).flip();
selectedCards.get(1).flip();

kivalasztottKartyak.clear();

to the clickEvent and just check whether selectedCards.size() == 2. In this case you would only flip both cards and not do anything else in this click and get rid of the sleep.

Comments