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?

2
3
4
5
6
7
8
JollyGreenGiant JollyGreenGiant

2020/5/1

#
The world class compiles fine. However when i do compile it the card class opens up showing syntax errors.
JollyGreenGiant JollyGreenGiant

2020/5/1

#
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 static Card firstPicked;
    public static int matchTries;
    public static int matchCount; 
 
    private GreenfootImage front;
    private boolean showing;
    private int value;
    
    
    public Card(String fname, int val)
    {
        value = val;
        front = new GreenfootImage(fname+".gif");
        setImage(back);
        
    }
    
     public void showCard()
    {
         if (showing)
           System.out.print(String.format("%10s",back));
        else
            System.out.print(String.format("%10s","["+front+"]"));
    }
    
    public void SetShowingStatus()
    {
        if(showing)
            showing = false;
        else
            showing = true;
    }
    
    private void flipCard()
    {
     showing = ! showing;
     setImage(showing ? front : back);
    }
    
    public void firstClicked()
    {
    this.flip();
    Greenfoot.delay(40);
    if (firstClicked == null)
    {
        firstClicked = this;
    }
    else
    {
        String image1 = firstClicked.getImage().toString().substring(17);;
        image1 = image1.substring(0, image1.indexOf("   "));
        String image2 = this.getImage().toString().substring(17);;
        image2 = image2.substring(0, image2.indexOf("   "));
        if (image1.equals(image2))
        {
            Greenfoot.playSound("fanfare.wav"); // example code
            addObject(new Correct(), getX(), getY()+100); // or wherever
        }
        else
        {
            Greenfoot.playSound("wrong.wav"); // example code
            firstClicked.flip();
            this.flip();
        }
        firstClicked = null;
    }
   }
 

    private void flip()
   {
    imageIndex = (imageIndex+1)%2;
    setImage(images[imageIndex]);
   }
   
    public void ifCorrect()
   {
    if((card1Flipped == true) && (card2Flipped == true))
    {
        Greenfoot.delay(30);
        displayCorrect();
        getWorld().removeObjects(getWorld().getObjects(Card2.class));
        getImage().setTransparency(0);
    }
     else if((card3Flipped == true) && (card4Flipped == true))
    {
        Greenfoot.delay(30);
        displayCorrect();
        getWorld().removeObjects(getWorld().getObjects(Card3.class));
        getWorld().removeObjects(getWorld().getObjects(Card4.class));
 
    }
    else
    {
        flipBack();
    }
    
    if (secondCard == null)
    {
    Cards card = pickedCard();
    if (firstCard == null)
    {
        firstCard = card;
    }
    else
    {
        secondCard = card;
        if (firstCardImage().equals(secondCardImage()))
        {    
            correctSequence = true;
            initializeCorrectSequence();
        }
        else
        {
            wrongSequence = true;
            initializeWrongSequence();
        }
    }
    }
    else
    {
    if (wrongSequence)
    {
        performWrongSequenceStep();
        if (lastWrongsequenceStep())
        {
            wrongSequence = false;
            firstCard = null;
            secondCard = null;
        }
    }
    else if (correctSequence)
    {
        performCorrectSequenceStep();
        if (lastCorrectSequenceStep())
        {
            correctSequence = false;
            firstCard = null;
            secondCard = null;
        }
    }
   }
 }
}
JollyGreenGiant JollyGreenGiant

2020/5/1

#
Btw the way thank you so much for helping me, I wouldn't even be close to a solution if you wasn't for your help.
danpost danpost

2020/5/1

#
You have a lot of game control code in your Card class that should be in your world class. My last 3 code posts near the bottom of page 2 of this discussion is ALL you need in your Card class. In fact, the static fields should be in your world class instead of the Card class. Once that is done, all that needs worked on is the act method of your world class.
JollyGreenGiant JollyGreenGiant

2020/5/2

#
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 static Card firstPicked;
    public static int matchTries;
    public static int matchCount; 
 
    private GreenfootImage front;
    private boolean showing;
    private int value;
    
    
    private void flipCard()
    {
    showing = ! showing;
    setImage(showing ? front : back);
   }
    
   
}
JollyGreenGiant JollyGreenGiant

2020/5/2

#
    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];
       
    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);
            
            private static GreenfootImage back = new GreenfootImage("cover.gif");
 
            private static Card firstPicked;
            public static int matchTries;
            public static int matchCount; 
 
            private GreenfootImage front;
            private boolean showing;
            private int value;
    }
    
    
    public Card(String fname, int val)
    {
        value = val;
        front = new GreenfootImage(fname+".gif");
        setImage(back);
        
    }
    
     public void showCard()
    {
         if (showing)
           System.out.print(String.format("%10s",back));
        else
            System.out.print(String.format("%10s","["+front+"]"));
    }
    
    public void SetShowingStatus()
    {
        if(showing)
            showing = false;
        else
            showing = true;
    }
    
    private void flipCard()
    {
     showing = ! showing;
     setImage(showing ? front : back);
    }
    
    public void firstClicked()
    {
    this.flip();
    Greenfoot.delay(40);
    if (firstClicked == null)
    {
        firstClicked = this;
    }
    else
    {
        String image1 = firstClicked.getImage().toString().substring(17);;
        image1 = image1.substring(0, image1.indexOf("   "));
        String image2 = this.getImage().toString().substring(17);;
        image2 = image2.substring(0, image2.indexOf("   "));
        if (image1.equals(image2))
        {
            Greenfoot.playSound("fanfare.wav"); // example code
            addObject(new Correct(), getX(), getY()+100); // or wherever
        }
        else
        {
            Greenfoot.playSound("wrong.wav"); // example code
            firstClicked.flip();
            this.flip();
        }
        firstClicked = null;
    }
   }
 

    private void flip()
   {
    imageIndex = (imageIndex+1)%2;
    setImage(images[imageIndex]);
   }
   
    public void ifCorrect()
   {
    if((card1Flipped == true) && (card2Flipped == true))
    {
        Greenfoot.delay(30);
        displayCorrect();
        getWorld().removeObjects(getWorld().getObjects(Card2.class));
        getImage().setTransparency(0);
    }
     else if((card3Flipped == true) && (card4Flipped == true))
    {
        Greenfoot.delay(30);
        displayCorrect();
        getWorld().removeObjects(getWorld().getObjects(Card3.class));
        getWorld().removeObjects(getWorld().getObjects(Card4.class));
 
    }
    else
    {
        flipBack();
    }
    
    if (secondCard == null)
    {
    Cards card = pickedCard();
    if (firstCard == null)
    {
        firstCard = card;
    }
    else
    {
        secondCard = card;
        if (firstCardImage().equals(secondCardImage()))
        {    
            correctSequence = true;
            initializeCorrectSequence();
        }
        else
        {
            wrongSequence = true;
            initializeWrongSequence();
        }
    }
    }
    else
    {
    if (wrongSequence)
    {
        performWrongSequenceStep();
        if (lastWrongsequenceStep())
        {
            wrongSequence = false;
            firstCard = null;
            secondCard = null;
        }
    }
    else if (correctSequence)
    {
        performCorrectSequenceStep();
        if (lastCorrectSequenceStep())
        {
            correctSequence = false;
            firstCard = null;
            secondCard = null;
        }
    }
   }
 }
}
JollyGreenGiant JollyGreenGiant

2020/5/2

#
Hi Dan, could you point out which lines in the world class I need to remove/change, thanks.
danpost danpost

2020/5/2

#
JollyGreenGiant wrote...
Hi Dan, could you point out which lines in the world class I need to remove/change, thanks.
In Card class, remove lines 13 thru 15 and add the following method:
public void getValue()
{
    return value;
}
In MemoryGame class, remove line 23 and lines 29 thru 31. Cut and paste lines 25 thru 27 to be before line 12. Then remove everything from line 35 to line 167. It should then compile and run with no error and you will be ready to work on the act method for game control.
JollyGreenGiant JollyGreenGiant

2020/5/2

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

2020/5/2

#
public static int matchTries;
       public static int matchCount
JollyGreenGiant JollyGreenGiant

2020/5/2

#
Im having errors with these two lines of code.
JollyGreenGiant JollyGreenGiant

2020/5/2

#
illegal character: '\u00a0' This is what shows in the syntax errors
JollyGreenGiant JollyGreenGiant

2020/5/2

#
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 showing;
    private int value;
    
    
    private void flipCard()
    {
        showing = ! showing;
        setImage(showing ? front : back);
    }
    
    public void getValue()
    {
        return value;
    }
}
JollyGreenGiant JollyGreenGiant

2020/5/2

#
On line 26 of the card class, it has a syntax error saying: incompatible types: unexpected return value
danpost danpost

2020/5/2

#
JollyGreenGiant wrote...
On line 26 of the card class, it has a syntax error saying: incompatible types: unexpected return value
Change 'void' on line 24 to 'int'.
There are more replies on the next page.
2
3
4
5
6
7
8