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?

3
4
5
6
7
8
9
danpost danpost

2020/5/2

#
JollyGreenGiant wrote...
<< Code Omitted >> Im having errors with these two lines of code.
Highlight everything from the end of line 13 to the start of line 17 and hit the 'Del' key. Then re-key the two lines back in.
JollyGreenGiant JollyGreenGiant

2020/5/2

#
That works now.
JollyGreenGiant JollyGreenGiant

2020/5/2

#
for (int i=0; i<cards.length; i++) cards[i] = new Card("g"+(i/2), i/2);
I have another error with this line here. It says constructor Card in class Card cannot be applied to given types; required : no arguments, found: java.lang.String,int, reason: actual and formal arguments lists differ in length.
danpost danpost

2020/5/2

#
JollyGreenGiant wrote...
<< Code Omitted >> I have another error with this line here. It says constructor Card in class Card cannot be applied to given types; required : no arguments, found: java.lang.String,int, reason: actual and formal arguments lists differ in length
Okay. add the following to Card class:
public Card(String fname, int val)
{
    value= val;
    front = new GreenfootImage(fname+".gif");
    setImage(back);
}
JollyGreenGiant JollyGreenGiant

2020/5/2

#
That works perfectly!
JollyGreenGiant JollyGreenGiant

2020/5/2

#
Al I have to do now is create a public act method in the world class to control the game.
JollyGreenGiant JollyGreenGiant

2020/5/2

#
For the public act method, i would have to place if statements to control the game?
danpost danpost

2020/5/2

#
JollyGreenGiant wrote...
For the public act method, i would have to place if statements to control the game?
Well, first thing is you want to test for a click on any card. Basically, a check for any click will do to start. If a click is detected, you can then go ahead and determine which card was clicked:
public void act()
{
    mouseClicking();
    
}

private void mouseClicking()
{
    if (Greenfoot.mouseClicked(null)) // any click
    {
        Actor clickedOn = Greenfoot.getMouseInfo().getActor();
Now, this actor may or may not be a card -- maybe the world background or some other type object was clicked on. If it is not a card, then go back to the act method immediately:
        if (clickedOn == null || ! (clickedOn instanceof Card)) return;
        Card c = (Card)clickedOn;
More checking to come. We now know a card was clicked. However, to be a valid card click, the card must be face down. So:
if (c.isShowing()) return;
You will need to add the following method to the Card class:
public boolean isShowing()
{
    return showing();
}
Back in the mouseClicking method, the next step is to determine if this is the first or second click:
        if (firstPicked == null) // first pick
        {
            firstPicked = c;
            c.flipCard();
        }
        else // second pick
        {
            // future post
        }
    }
You should now be able to flip the first card.
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 isShowing();
    }
}
JollyGreenGiant JollyGreenGiant

2020/5/3

#
The game has complied and when i clicked on new memory game it displays the board and the images. I clicked on run and clicked on them but they wont flip over plus when i click on new card i get this: Card(string fname, int val).
JollyGreenGiant JollyGreenGiant

2020/5/3

#
at Card.isShowing(Card.java:38) at Card.isShowing(Card.java:38) at Card.isShowing(Card.java:38) at Card.isShowing(Card.java:38) at Card.isShowing(Card.java:38) at Card.isShowing(Card.java:38) at Card.isShowing(Card.java:38) at Card.isShowing(Card.java:38) at Card.isShowing(Card.java:38) at Card.isShowing(Card.java:38) at Card.isShowing(Card.java:38) at Card.isShowing(Card.java:38) at Card.isShowing(Card.java:38) at Card.isShowing(Card.java:38) at Card.isShowing(Card.java:38)
JollyGreenGiant JollyGreenGiant

2020/5/3

#
This shows in the terminal.
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;
    
    private GreenfootImage card1 = new GreenfootImage ("g0.gif");
    private GreenfootImage card2 = new GreenfootImage ("g1.gif");
    private GreenfootImage card3 = new GreenfootImage ("g2.gif");
    private GreenfootImage card4 = new GreenfootImage ("g3.gif");
    private GreenfootImage card5 = new GreenfootImage ("g4.gif");
    private GreenfootImage card6 = new GreenfootImage ("g5.gif");
    private GreenfootImage card7 = new GreenfootImage ("g6.gif");
    private GreenfootImage card8 = new GreenfootImage ("g7.gif");
    private GreenfootImage card9 = new GreenfootImage ("g8.gif");
    private GreenfootImage card10 = new GreenfootImage ("g9.gif");
   
    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

#
From line 17 - 26, I added these lines of code as I thought this would fix my current problem.
There are more replies on the next page.
3
4
5
6
7
8
9