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?

11
12
13
14
15
danpost danpost

2020/5/6

#
JollyGreenGiant wrote...
public int getValue() return value;
The is the method you need to take advantage of in the match method.
JollyGreenGiant JollyGreenGiant

2020/5/6

#
for line 3 i would put: if (getValue matches(), return value;)
danpost danpost

2020/5/6

#
JollyGreenGiant wrote...
for line 3 i would put: if (getValue matches(), return value;)
I said "take advantage of", not copy or emulate. Call the getValue method on the cards so you can compare those values.
JollyGreenGiant JollyGreenGiant

2020/5/7

#
public int getValue()

{

return value;

}

public Card(String fname, int val)

{

value= val;

front = new GreenfootImage(fname+".gif");

setImage(back);

}

JollyGreenGiant JollyGreenGiant

2020/5/7

#
So I would change Val in public card to getValue?
JollyGreenGiant JollyGreenGiant

2020/5/7

#
I so confused.
danpost danpost

2020/5/7

#
JollyGreenGiant wrote...
So I would change Val in public card to getValue?
I already told you that you were done editing the Card class. No more changes were to be made to it.
JollyGreenGiant JollyGreenGiant

2020/5/7

#
Okay. I'll need to focus on the getValue method in the private match class.
JollyGreenGiant JollyGreenGiant

2020/5/7

#
Im getting nowhere with the final stage.
danpost danpost

2020/5/7

#
JollyGreenGiant wrote...
Im getting nowhere with the final stage.
Hint: << subject >> . << verb >> << card >> . << get value >>
JollyGreenGiant JollyGreenGiant

2020/5/7

#
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(Greenfoot.getRandomNumber(10));
        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/7

#
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(int val)
    {
        value= val;
        front = new GreenfootImage("g"+ val +".gif");
        setImage(back);
    }

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

2020/5/7

#
This is what I have so far.
JollyGreenGiant JollyGreenGiant

2020/5/7

#
if (firstPicked == null) 
        {
            firstPicked = this;
            flipCard();
        }
        else if (secondPicked == null) 
        {
            secondPicked = this;
            flipCard();
        {
           if (firstPicked !=match)
JollyGreenGiant JollyGreenGiant

2020/5/7

#
Something along the lines of this when it comes to matching the images in the private void match?
There are more replies on the next page.
11
12
13
14
15