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?

10
11
12
13
14
15
JollyGreenGiant JollyGreenGiant

2020/5/6

#
Value of the image id?
JollyGreenGiant JollyGreenGiant

2020/5/6

#
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 = 20;
            }
        }

    }

    private void match()
    {
        
    }

}
JollyGreenGiant JollyGreenGiant

2020/5/6

#
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 = 20;
            }
        }

    }

    private void match()
    {
        
    }

}
JollyGreenGiant JollyGreenGiant

2020/5/6

#
I decided to do it this way as I know each image has an id. So I added a int field for the "image id" to the "Card" class, so I can compare the image ids of the 2 currently used ("flipped") cards.
JollyGreenGiant JollyGreenGiant

2020/5/6

#
for (int i=0; i<cards.length; i++) cards[i] = new Card("g"+(i/2), i/2);
I do have a syntax error with this line though.
JollyGreenGiant JollyGreenGiant

2020/5/6

#
The line for the array.
danpost danpost

2020/5/6

#
You do not need an id. You have a value -- plus a getValue method you can use.
JollyGreenGiant JollyGreenGiant

2020/5/6

#
Just continue with the original codes instead of the new ones.
JollyGreenGiant JollyGreenGiant

2020/5/6

#
I'm just stuck on the whole private void match method.
danpost danpost

2020/5/6

#
JollyGreenGiant wrote...
I'm just stuck on the whole private void match method.
I am trying to help you on line 3 of the match method.
JollyGreenGiant JollyGreenGiant

2020/5/6

#
i know, and again thank you.
JollyGreenGiant JollyGreenGiant

2020/5/6

#
public Card(String fname, int val)     {       value= val;       front = new GreenfootImage(fname+".gif");       setImage(back);     } I think I would find the value here, fname+".gif");
JollyGreenGiant JollyGreenGiant

2020/5/6

#
Although if im looking for the value that would be a int.
danpost danpost

2020/5/6

#
JollyGreenGiant wrote...
Although if im looking for the value that would be a int.
Look a little further up to find the int. Then, look down for the get method. You do not need to change anything in the Card class. You are only looking to see what is already available to use -- what options you can apply to a card in your MemoryGame class. It cannot be anything 'private'.
JollyGreenGiant JollyGreenGiant

2020/5/6

#
public int getValue() return value;
There are more replies on the next page.
10
11
12
13
14
15