This site requires JavaScript, please enable it in your browser!
Greenfoot back
JollyGreenGiant
JollyGreenGiant wrote ...

2020/4/26

How do i make a simple memory card game?

8
9
10
11
12
13
14
JollyGreenGiant JollyGreenGiant

2020/5/5

#
So where would I go in order to get the images to match? Would this be in the private void mouseClicking?
danpost danpost

2020/5/5

#
JollyGreenGiant wrote...
So where would I go in order to get the images to match? Would this be in the private void mouseClicking?
Replace the two lines, 39 and 40, with the one line:
match();
All the rest of your changes (adding code to compare the picked cards, etc.) will go ONLY in the match method.
JollyGreenGiant JollyGreenGiant

2020/5/5

#
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot, and MouseInfo)

/**
 * Write a description of class MemoryGame here.
 * 
 * Andy McCallum
 * 01/05/2020
 */
public class MemoryGame extends World
{
    Card[] cards = new Card[20];

    private static Card firstPicked;
    private static Card secondPicked;
    private int timer;
    public static int matchTries;
    public static int matchCount;

    private int match;

    public MemoryGame()
    {
        super(4, 5, 100);
        // load array
        // shuffle
        // deal
        for (int i=0; i<cards.length; i++) cards[i] = new Card("g"+(i/2), i/2);
        java.util.Collections.shuffle(java.util.Arrays.asList(cards));
        for (int i=0; i<cards.length; i++) addObject(cards[i], i/5, i%5);

    }

    public void act()
    {
        if (timer > 0)
        {
            timer--;
            if (timer > 0) return;
            match();
        }
        mouseClicking();

    } 

    private void mouseClicking()
    {
        if(Greenfoot.mouseClicked(null))
        {
            Actor clickedOn = Greenfoot.getMouseInfo().getActor();

            if (clickedOn == null || ! (clickedOn instanceof Card)) return;
            Card c = (Card)clickedOn;
            if (c.isShowing()) return;

            if (firstPicked == null) 
            {
                firstPicked = c;
                c.flipCard();
            }
            else 
            {
                secondPicked = c;
                c.flipCard();
                timer = 40;
            }
        }

    }

    private void match()
    {

        if (firstPicked == null)
        {
            firstPicked = this;
            flipCard();
        }
        else if (secondPicked == null) 
        {
            secondPicked = this;
            flipCard();
            if (firstPicked != match) 
            {
                firstPicked.flipCard();
                flipCard();
            }
            else paired++;
            firstCard = null;
            secondCard = null;
        }
    }

}
JollyGreenGiant JollyGreenGiant

2020/5/5

#
I still stuck on having the cards compare to each other.
danpost danpost

2020/5/5

#
JollyGreenGiant wrote...
I wasn't sure if this was how i would compare the cards.
You cannot compare a boolean (true) with a Card object (firstPicked or secondPicked). Also, using an equal sign does not perform a comparison -- it (trying to) sets the value to the variable. Since firstPicked and secondPicked are of type Card, true cannot be set to it. To compare two things for equality (like value or same object), you need to use two equal signs. An example is:
if (valueA == valueB)
JollyGreenGiant JollyGreenGiant

2020/5/5

#
private void match()
    {
 
        if (firstPicked == null)
        {
            firstPicked = this;
            flipCard();
        }
        else if (secondPicked == null) 
        {
            secondPicked = this;
            flipCard();
            if (firstPicked != match) 
            {
                firstPicked.flipCard();
                flipCard();
            }
            else paired++;
            firstCard = null;
            secondCard = null;
        }
    }
Lines 73 -89
danpost danpost

2020/5/5

#
Why are you comparing them to null? The match method is only called when both firstPicked and secondPicked are known to NOT be null. You need to compare the values of the two cards to each other.
JollyGreenGiant JollyGreenGiant

2020/5/5

#
You cannot compare a boolean (true) with a Card object (firstPicked or secondPicked). Also, using an equal sign does not perform a comparison -- it (trying to) sets the value to the variable. Since firstPicked and secondPicked are of type Card, true cannot be set to it. To compare two things for equality (like value or same object), you need to use two equal signs. Looking at the code again, it didn't make much sense to me so i scraped it as boolean can not = true. I'm really desperate to get this game done and again thank you so much for helping me, I'm still a novice when it comes to coding.
JollyGreenGiant JollyGreenGiant

2020/5/5

#
if (firstPicked == match)
        {
            firstPicked = this;
            flipCard();
        }
        else if (secondPicked == match) 
        {
            secondPicked = this;
            flipCard();
            if (firstPicked ! = match) 
            {
                firstPicked.flipCard();
                flipCard();
            }
            else paired++;

            firstCard = firstCard;
            secondCard = secondCard;

        } 
What lines would i need to change?
danpost danpost

2020/5/5

#
JollyGreenGiant wrote...
What lines would i need to change?
Everything. You do not want to compare cards to anything. You want to compare the value of the two cards to each other.
JollyGreenGiant JollyGreenGiant

2020/5/5

#
How would I compare the value of the two cards to each other in the private void match? Would it be if (firstPicked == secondPicked) = match?
JollyGreenGiant JollyGreenGiant

2020/5/5

#
private void match()
    {

        if (firstPicked == secondPicked) 
        {
            match();
        }

    }
danpost danpost

2020/5/5

#
Last time:
danpost wrote...
Never (just about) do you ever call a method from within that method. Or, in other words, never have a method call itself.
JollyGreenGiant JollyGreenGiant

2020/5/6

#
I'm still not quite sure how I would compare the value of the two cards to each other?
JollyGreenGiant JollyGreenGiant

2020/5/6

#
I'm still stuck on the final part of the game.
There are more replies on the next page.
8
9
10
11
12
13
14