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?

4
5
6
7
8
9
10
JollyGreenGiant JollyGreenGiant

2020/5/3

#
public boolean isShowing()
    {
      return isShowing();
    }
I think the issue is to do with this line of code.
danpost danpost

2020/5/3

#
JollyGreenGiant wrote...
public boolean isShowing()
    {
      return isShowing();
    }
I think the issue is to do with this line of code.
Compare that with what I suggested.
danpost danpost

2020/5/3

#
JollyGreenGiant wrote...
From line 17 - 26, I added these lines of code as I thought this would fix my current problem.
Take them back out.
JollyGreenGiant JollyGreenGiant

2020/5/3

#
Ok
JollyGreenGiant JollyGreenGiant

2020/5/3

#
I removed everything from line 17-26
public boolean isShowing()
    {
      return showing();
    }
I'm still having issues with this code
JollyGreenGiant JollyGreenGiant

2020/5/3

#
It tells me it cannot find symbol.
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;
    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 // second pick
            {
                // future post
            }
        }
    }
    
}

                      
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 showing();
    }
}
JollyGreenGiant JollyGreenGiant

2020/5/3

#
The first image shows when I hide these lines of code.
danpost danpost

2020/5/3

#
JollyGreenGiant wrote...
public boolean isShowing()
    {
      return showing();
    }
I'm still having issues with this code
Okay, You call it isShowing:
return isShowing;
(no round brackets)
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;
    public 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();
            }
            if (secondPicked == null) // first pick
            {
                firstPicked = c;
                c.flipCard();
            }
    }
    
   }
}
                      
JollyGreenGiant JollyGreenGiant

2020/5/3

#
I managed to get the images beneath them to show when i click on them.
danpost danpost

2020/5/3

#
In MemoryGame class, change line 51 to:
else
Reactivate line 44.
danpost danpost

2020/5/3

#
JollyGreenGiant wrote...
I managed to get the images beneath them to show when i click on them.
So, it runs with no errors and you can click on a single card to flip it?
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();
            }
            if (secondPicked == null) // first pick
            {
                firstPicked = c;
                c.flipCard();
            }
       }
    
     }
}
                      
There are more replies on the next page.
4
5
6
7
8
9
10