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?

5
6
7
8
9
10
11
JollyGreenGiant JollyGreenGiant

2020/5/3

#
error on line 44
danpost danpost

2020/5/3

#
danpost wrote...
In MemoryGame class, change line 51 to:
else
(On a separate note) Reactivate line 44.
Go back and try this again.
JollyGreenGiant JollyGreenGiant

2020/5/3

#
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;
    public static int matchTries;
    public static int matchCount;
    
    
    public MemoryGame()
    {
        super(5, 4, 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()
    {
        mouseClicking();
    }

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

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

            if (firstPicked == null) // first pick
            {
                firstPicked = c;
                c.flipCard();
            }
            else (secondPicked == null) // first pick
            {
                firstPicked = c;
                c.flipCard();
            }
    }
    
   }
}
                      
danpost danpost

2020/5/3

#
Line 44 should be "if" ... -- not "else" ...
JollyGreenGiant JollyGreenGiant

2020/5/3

#
Still showing syntax errors
danpost danpost

2020/5/3

#
Line 51 should be "else" by itself (that should be the entire line).
JollyGreenGiant JollyGreenGiant

2020/5/3

#
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;
    public static int matchTries;
    public static int matchCount;

    public MemoryGame()
    {
        super(5, 4, 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()
    {
        mouseClicking();
    }

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

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

            if (firstPicked == null) // first pick
            {
                firstPicked = c;
                c.flipCard();
            }
            else 
                (secondPicked == null) // first pick
            {
                firstPicked = c;
                c.flipCard();
            }
        }

    }
}

danpost danpost

2020/5/3

#
Remove line 51 from your last post. (the whole line)
danpost danpost

2020/5/3

#
Last post was edited (line number correction).
JollyGreenGiant JollyGreenGiant

2020/5/3

#
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;
    public static int matchTries;
    public static int matchCount;

    public MemoryGame()
    {
        super(5, 4, 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()
    {
        mouseClicking();
    }

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

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

            if (firstPicked == null) // first pick
            {
                firstPicked = c;
                c.flipCard();
            }
            else 
            {
                firstPicked = c;
                c.flipCard();
            }
        }

    }
}

danpost danpost

2020/5/3

#
No errors? Able to click and flip a single card?
JollyGreenGiant JollyGreenGiant

2020/5/3

#
Line 43
            if (c.isShowing()) return;
Has an error saying cannot find symbol - method isShowing()
danpost danpost

2020/5/3

#
JollyGreenGiant wrote...
Line 43
if (c.isShowing()) return;
Has an error saying cannot find symbol - method isShowing()
Did you ever remove the commenting from the isShowing method in the Card class?
JollyGreenGiant JollyGreenGiant

2020/5/3

#
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/3

#
It compiles but this is what appears on my terminal. at Card.isShowing(Card.java:37) at Card.isShowing(Card.java:37) at Card.isShowing(Card.java:37) at Card.isShowing(Card.java:37) at Card.isShowing(Card.java:37) at Card.isShowing(Card.java:37) at Card.isShowing(Card.java:37) at Card.isShowing(Card.java:37) at Card.isShowing(Card.java:37) at Card.isShowing(Card.java:37)
There are more replies on the next page.
5
6
7
8
9
10
11