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?

7
8
9
10
11
12
13
JollyGreenGiant JollyGreenGiant

2020/5/4

#
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()
    {
        match();
    }
}
JollyGreenGiant JollyGreenGiant

2020/5/4

#
Like this?
danpost danpost

2020/5/4

#
JollyGreenGiant wrote...
private void match()
{
    match(); 
}
Stack overflow will no doubt occur with this. Never (just about) do you ever call a method from within that method. Or, in other words, never have a method call itself. That is where you compare the values of the two picked cards.
JollyGreenGiant JollyGreenGiant

2020/5/5

#
I can locate the values in the mouse clicked method for the two cards and add them to the private void match method?
danpost danpost

2020/5/5

#
JollyGreenGiant wrote...
I can locate the values in the mouse clicked method for the two cards and add them to the private void match method?
Why, when you have the fields holding the two picked cards?
JollyGreenGiant JollyGreenGiant

2020/5/5

#
How do I make two matching images stay on screen but flip back if they don't match? Would I have to create a flip and unflip methods when comparing with firstCard and secondCard?
danpost danpost

2020/5/5

#
JollyGreenGiant wrote...
How do I make two matching images stay on screen but flip back if they don't match? Would I have to create a flip and unflip methods when comparing with firstCard and secondCard?
You already have a flipCard method that will both flip and unflip. If the values of the picked cards do not match, use the method on both picked cards.
JollyGreenGiant JollyGreenGiant

2020/5/5

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

/**
 * Write a description of class Card here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Card extends Actor
{
    private static GreenfootImage back = new GreenfootImage("cover.gif");

    private GreenfootImage front;
    private boolean isShowing;
    private int value;

    public void flipCard()
    {
        isShowing = ! isShowing;
        setImage(isShowing ? front : back);

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

    }

    public int getValue()
    {
        return value;
    }

    public Card(String fname, int val)
    {
        value= val;
        front = new GreenfootImage(fname+".gif");
        setImage(back);
    }

    public boolean isShowing()
    {
        return isShowing;
    }
}
JollyGreenGiant JollyGreenGiant

2020/5/5

#
My flip card method is still not working, I'm not sure where i went wrong here?
JollyGreenGiant JollyGreenGiant

2020/5/5

#
I most likely added far too much code to the flip card method.
JollyGreenGiant JollyGreenGiant

2020/5/5

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

/**
 * Write a description of class Card here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Card extends Actor 
{
    private static GreenfootImage back = new GreenfootImage("cover.gif");

    private GreenfootImage front;
    private boolean isShowing;
    private int value;
    
    
    public void flipCard()
    {
        isShowing = ! isShowing;
        setImage(isShowing ? front : back);

        if (firstPicked == front) 
            {
                firstPicked = 0;
                flipCard();
                return;
            }
            else 
            {
                secondPicked = 0;
                flipCard(); 
                return;
            }

    }

    public int getValue()
    {
        return value;
    }

    public Card(String fname, int val)
    {
        value= val;
        front = new GreenfootImage(fname+".gif");
        setImage(back);
    }

    public boolean isShowing()
    {
        return isShowing;
    }
}
JollyGreenGiant JollyGreenGiant

2020/5/5

#
I amended the flipCard method but I'm still having issues.
danpost danpost

2020/5/5

#
JollyGreenGiant wrote...
I most likely added far too much code to the flip card method.
That is overstating the obvious. Especially since the Card class was already complete. That is, nothing more should have been added to it (or taken away). There was nothing more to do in that class once the isShowing method was added.
JollyGreenGiant JollyGreenGiant

2020/5/5

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

/**
 * Write a description of class Card here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Card extends Actor 
{
    private static GreenfootImage back = new GreenfootImage("cover.gif");

    private GreenfootImage front;
    private boolean isShowing;
    private int value;

    public void flipCard()
    {
        isShowing = ! isShowing;
        setImage(isShowing ? front : back);
    }

    public int getValue()
    {
        return value;
    }

    public Card(String fname, int val)
    {
        value= val;
        front = new GreenfootImage(fname+".gif");
        setImage(back);
    }

    public boolean isShowing()
    {
        return isShowing;
    }
}
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;
            firstPicked.turn(-90);
            secondPicked.turn(-90);
        }
        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()
    {

    }
}
There are more replies on the next page.
7
8
9
10
11
12
13