This site requires JavaScript, please enable it in your browser!
Greenfoot back
carter
carter wrote ...

2016/4/26

Making a Memory Game

1
2
3
4
carter carter

2016/4/26

#
I'm making a game with cards where you match them and they are face down. I am very new to coding and this is my first Greenfoot project. I don't have any code for this part, but I am wondering how I could make it so only 2 cards can be flipped at the same time. I have Card1-4 and booleans that define if the cards are flipped or not. Also, is it fine if I post all the questions I have related to this project on this discussion? I will have a lot of questions.
danpost danpost

2016/4/26

#
You could have continued in the other discussion; since it relates to the same project and most of the relevant code was already posted there. Also, I explained what needed to be done as far as the matching of two cards. The first one clicked should be saved in a field. When one is clicked with one already saved in the field, compare the one clicked with the one in the field. Your only resource for comparing the two may be as follows from line 20 to 24:
/** in Cards class */
// class field
private static Cards firstClicked;

// instance fields
protected GreenfootImage[] images; // to be set by subclasses
protected int imagesIndex;

// in act or method called by act
if (imagesIndex == 0 && Greenfoot.mouseClicked(this))
{
    this.flip();
    Greenfoot.delay(40); // adjust as needed (so card is shown at least briefly if not a match)
    if (firstClicked == null)
    {
        firstClicked = this;
    }
    else
    {
        String image1 = firstClicked.toString().substring(17);
        image1 = image1.substring(0, image1.indexOf("   "));
        String image2 = this.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;
    }
}

// using a method to flip the cards
private void flip()
{
    imageIndex = (imageIndex+1)%2;
    setImage(images[imageIndex]);
}
carter carter

2016/4/26

#
I'm sorry, I don't know what any of that means, as I said before I am very new to coding and Greenfoot.
danpost danpost

2016/4/26

#
carter wrote...
I'm sorry, I don't know what any of that means, as I said before I am very new to coding and Greenfoot.
Name some of the things you are not privy to and I will try to explain what they mean (or lead you to some place where you can acquire the understanding of what they mean).
carter carter

2016/4/26

#
Well first off, where would I put the code?
danpost danpost

2016/4/26

#
carter wrote...
Well first off, where would I put the code?
Read the first line in the code window.
danpost danpost

2016/4/27

#
I missed something in the code given. Replace line 20 with this:
String image1 = firstClicked.getImage().toString().substring(17);
and replace line 22 with this:
String image2 = this.getImage().toString().substring(17);
carter carter

2016/4/27

#
Ok I have another question:
    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();
        }
So I want to know how I can "disable" the first if, because if card1 and card2 are picked first, then when selecting card3 and card4, the Greenfoot.delay(30); still happens, and it messes up the timing. Thanks in advance
carter carter

2016/4/27

#
danpost wrote...
carter wrote...
Well first off, where would I put the code?
Read the first line in the code window.
Do I put the image files in the quotes?
danpost danpost

2016/4/27

#
I believe I mentioned in your other discussion thread that the images need to be assigned their images by the constructor of the subclasses.
carter carter

2016/4/27

#
I'm still very confused, how would I make it so if 2 cards are flipped, another card cannot be flipped until one of the old cards are removed or unflipped?
danpost danpost

2016/4/27

#
Only allow unflipped cards to be flipped by mouse clicks. Flipped cards will be unflipped together only when they are the currently chosen ones and they do not match. Like I said before, in you put the first flipped card in a field and then compare the second flipped card to it, then after comparison, they will both stay flipped up or they will both be flipped back down. Either way, if you set the field back to 'null', then a new sequence will begin. The first card chosen when the field is 'null'; the second card is chosen when the field holds a card. If you wish, or if necessary, you can another field for the second card. Then, only draw a card if the second card is 'null'; but, still, if the first is 'null' the chosen card goes there. Set the second card back to 'null' when any animation or actions between sets of cards is complete. The following is a summary breakdown of the sequence for the act method. This is not to be used, as is; there are a lot of generalizations (method calls to non-existent methods and field names of undefined fields). It is a general format, if you will:
// breakdown of sequence (in pseudo-code) for actions
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;
        }
    }
}
carter carter

2016/4/27

#
And all of that determines if the 2 cards picked are correct or incorrect right?
danpost danpost

2016/4/28

#
carter wrote...
And all of that determines if the 2 cards picked are correct or incorrect right?
No. That would be line 12, and line 12 only. Lines 20 through 24 of my first code post in this discussion thread would be what is represented by that one line.
carter carter

2016/4/28

#
Just to clear it up, I am trying to have it so if 2 cards are clicked, and they are not matching, they are flipped back. When I was using code before, I could get 1 set right, and then I would click on another card, and the program would stop and I would receive an error.
There are more replies on the next page.
1
2
3
4