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/28

#
New problem.
static public int counter = 0;    

public void changeImage()
        {
        if(Greenfoot.mouseClicked(this))
        {
            if(card1Flipped)
            {
                counter = counter - 1;
                setImage(front);
                card1Flipped = false;
            }
            else
            {
                if(counter < 2)
                {
                    counter = counter + 1;
                    setImage(wolf);
                    card1Flipped = true;
                    ifWrong();
                }
            }
        }
        if((card1Flipped == true) && (card2Flipped == true))
        {
                       Greenfoot.delay(30);
                       displayCorrect();
                       getWorld().removeObjects(getWorld().getObjects(Card2.class));
                       getWorld().removeObjects(getWorld().getObjects(Card1.class));
                    }
            }
    public void ifWrong()
    {
        if(counter == 2)
        {
            if((card1Flipped == true) && (card3Flipped == true))
            {
                Greenfoot.delay(30);
                card1Flipped = false;
                card3Flipped = false;
                Card1 card1 = (Card1) getWorld().getObjects(Card1.class).get(0);
                card1.setImage(front);
                Card3 card3 = (Card3) getWorld().getObjects(Card3.class).get(0);
                card3.setImage(front);
                counter = 0;
            }
            else if ((card1Flipped == true) && (card4Flipped == true))
            {
                Greenfoot.delay(30);
                card2Flipped = false;
                card4Flipped = false;
                Card1 card1 = (Card1) getWorld().getObjects(Card1.class).get(0);
                card1.setImage(front);
                Card4 card4 = (Card4) getWorld().getObjects(Card4.class).get(0);
                card4.setImage(front);
                counter = 0;
            }
        }
    }
I have that code to keep only 2 cards clicked at once. When I click a wrong pair, I would assume the counter be set to zero, but if I click another wrong pair, the cards do not reset. I think it is a problem with the numbers in the counter, but I can't figure it out. changeImage(); is in every card.
carter carter

2016/4/28

#
Nevermind I got it, line 50 should be card1Flipped = false;
danpost danpost

2016/4/28

#
Let me give you an explanation of each line in the summary code: ******************************************************* Line 2: basically asks if cards still need to be picked; if a second card is not referenced, then that is the case; if a second card is referenced, continue at line 26 // picking cards Line 4: pick a card: if an unflipped card is clicked on, then 'card' will reference that card; otherwise, 'card' will be set to 'null' (again) Line 4.5 (missed if block with starting line of 'if (card != null)', blocking line 5 through 22): asks if a card has been picked; if 'card' has a value of 'null', no card has been picked, yet, so, exit given code (for this act cycle) // unflipped card was picked Line 5: asks if this would be the first card picked; if not, continue at line 11 // picked first card Line 7: save first card picked in field 'firstCard' and exit given code (for this act cycle) // picked second card Line 11: save second card picked in field 'secondCard' Line 12: asks if the images of both the first and second picked cards match; if not, continue at line 19 // cards match Line 14: sets a Boolean field to true to indicate that things that take some time are being done because a match was made Line 15: gets things going as far as sound and visual animations due to a match and exits given code (for this act cycle) // cards do not match Line 19: sets a Boolean field to true to indicate that thins that take some act cycles to complete are being done because of a mismatch Line 20: gets things going as far as sound and visual animations due to a mismatch and exits given code (for this act cycle) // cards have previously been chosen (in the middle of sounds and visual animations due to either a match or a mismatch) Line 26: asks if finishing up on mismatch actions; if not, continue at line 36 // mismatch actions Line 28: continue whatever needs finished due to a mismatch (passing time/animations/etc.) Line 29: asks if mismatch actions are complete; if not, exit given code (for this act cycle) // mismatch actions completed Line 31: clears the indicator that mismatch action are in process (sets boolean back to false) Line 31.5 (missed): flips the unmatched cards over on their backs again Line 32 and 33: clears the references to the previously picked set of cards to start a new set of picks and exits given code (for this act cycle) // not unmatched actions Line 36: asks if finishing up on matched actions; if not, exit given code (for this act cycle) // matched actions Line 38: continue whatever needs finished due to a match (passing time/animations/etc.) Line 39: asks if matched actions are complete; if not, exit given code (for this act cycle) // matched actions completed Line 41: clears the indicator that matched action are in process (sets boolean back to false) Line 42 and 43: clears the references to the previously picked set of cards to start a new set of picks and exits given code (for this act cycle) **************** end of line details *********** So, if you continuously repeat going through this, you will notice that everything needed (as far as the flow dynamics) is there.
carter wrote...
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.
Posting a copy of the error message and the code you had at that time might have helped. Anytime you get an error message with something you cannot resolve yourself -- stop, and before changing anything, copy/paste the error message and post it with the related code here for help. Once the code is changed, is could be a lot more difficult to track down the problem with the previous error message.
carter carter

2016/4/28

#
Ok. I have the first level figured out by hard coding each outcome. For the 2nd level, I want there to be 6 cards. I was told I need an array for the cards. I have never used an array before, so I need help. I want the array to have 2 columns and 3 rows. I need to be able to compare each possible outcome (card1 & card2, card1 & card3, card2 & card5, etc.) How would I do so?
danpost danpost

2016/4/29

#
carter wrote...
For the 2nd level, I want there to be 6 cards. I was told I need an array for the cards. I have never used an array before, so I need help. I want the array to have 2 columns and 3 rows. I need to be able to compare each possible outcome (card1 & card2, card1 & card3, card2 & card5, etc.) How would I do so?
You will need to display an array of cards, but you do not necessarily need to use a data array in your code. You can do just what you did in the first level; there will just be more coding -- especially as far as comparing all the possible outcomes.
carter carter

2016/4/29

#
Well when I get to level 3 (10 cards on screen), hard coding that would be a hassle. Having card1 & card3, card1 & card4, card1 & card5, card1 & card6, blah blah blah
danpost danpost

2016/4/29

#
carter wrote...
Well when I get to level 3 (10 cards on screen), hard coding that would be a hassle. Having card1 & card3, card1 & card4, card1 & card5, card1 & card6, blah blah blah
Yeah -- I know. That is like 45 different pairings -- ( 9 * ( 9 + 1 ) ) /2 -- or, the sum of all the counting numbers from one to nine. It might be easier to have each card hold a reference to its match. That way, when the second card is clicked, it only needs to compare the first clicked card with card held by its pre-determined match.
carter carter

2016/4/29

#
The problem isn't determining if the cards are correct, it's determining if the 2 cards clicked are incorrect.
danpost danpost

2016/4/29

#
carter wrote...
The problem isn't determining if the cards are correct, it's determining if the 2 cards clicked are incorrect.
If the test to see if the second card is correct fails, then you know is it incorrect. Pseudo-code summary:
if (matching()) // correct
{
    // some code
}
else // incorrect
{
    // some code
}
carter carter

2016/4/29

#
Ok, another new question. I want to randomize the positions that the cards are placed, how would I do so?
danpost danpost

2016/4/29

#
Place the cards in an array as you create them. Then shuffle them and display them in order.
// create the array
Cards[] cards = new Card[4];

// add the cards to the array
cards[0] = new Card1();
cards[1] = new Card2();
cards[2] = new Card3();
cards[3] = new Card4();

// shuffle the cards
java.util.Collections.shuffle(java.util.Arrays.asList(cards));

// add cards into world
addObject(cards[0], 100, 200);
addObject(cards[1], 200, 200);
addObject(cards[2], 300, 200);
addObject(cards[3], 400, 200);
carter carter

2016/4/29

#
It works great, Thanks!
carter carter

2016/4/29

#
Back to the problem comparing cards. If I get an array with card1-card6 in it, can I do if(card1 & card2), else if(card1 & card3), etc. without hard coding everything?
danpost danpost

2016/4/30

#
carter wrote...
Back to the problem comparing cards. If I get an array with card1-card6 in it, can I do if(card1 & card2), else if(card1 & card3), etc. without hard coding everything?
You should not need an array to hold the cards. The array was just temporary, to aide in shuffling the cards. Just giving each card a field to hold its matching card is all you need. What I would do is first off, use only one card class. After filling the initial array and before shuffling the cards during preparation of the world, take each pair of cards (first and second, third and fourth, etc. and call them matches, give the same image to each pair of cards to use when face up and give them references to each other. The backs can be randomized in either the world class during preparation or in the Card class during construction. Then, when you shuffle, the pairs will be split apart randomly.
carter carter

2016/5/1

#
//card1, card3, card5
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class Card1 here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */

public class Card1 extends Cards
{
    /**
     * Act - do whatever the Card1 wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment. 
     */
    public void act() 
    {
        if(cards == 6)
        {
            changeImage();
        }
        else if(cards == 4)
        {
            changeImage();
        }
        else if(cards == 2)
        {
            changeImageEnd();
        }
    }    

    private void changeImage()
    {
        if(Greenfoot.mouseClicked(this))
        {
            if((card1Flipped == true) && (counter > 0))
            {
                counter = counter - 1;
                setImage(front);
                card1Flipped = false;
            }
            else
            {
                if(counter < 2)
                {
                    counter = counter + 1;
                    if(level == 1)
                    {
                        setImage(wolf);
                        card1Flipped = true;
                    }
                    else if(level == 2)
                    {  
                        setImage(lion);
                        card1Flipped = true;
                    }
                    else if(level == 3)
                    {
                        setImage(elephant);
                        card1Flipped = true;
                    }
                }
            }
        }
        if((card1Flipped == true) && (card2Flipped == true))
        {
            Greenfoot.delay(30);
            displayCorrect();
            getWorld().removeObjects(getWorld().getObjects(Card2.class));
            getWorld().removeObjects(getWorld().getObjects(Card1.class));
            counter = 0;
            cards = cards - 2;
        }
        else if(counter == 2)
        {
            Greenfoot.delay(15);
            setImage(front);
            card1Flipped = false;
            counter = counter - 1;
        }
    }

    public void changeImageEnd()
    {
        if(Greenfoot.mouseClicked(this))
        {
            if((card1Flipped == true) && (counter > 0))
            {
                counter = counter - 1;
                setImage(front);
                card1Flipped = false;
            }
            else if(level == 1)
            {
                setImage(wolf);
                card1Flipped = true;
            }
            else if(level == 2)
            {
                setImage(lion);
                card1Flipped = true;
            }
            else if(level == 3)
            {
                setImage(elephant);
                card1Flipped = true;
            }
        }
        if((card1Flipped) && (card2Flipped))
        {
            Greenfoot.delay(30);
            displayCorrect();
            getWorld().removeObjects(getWorld().getObjects(Card2.class));
            getWorld().removeObjects(getWorld().getObjects(Card1.class));
            counter = 0;
            if(level == 1)
            {
                cards = 4;
                level = 2;
                Greenfoot.setWorld(new Level2());
            }
            else if(level == 2)
            {
                cards = 6;
                level = 3;
                Greenfoot.setWorld(new Level3());
            }
            card1Flipped = false;
            card2Flipped = false;
            card3Flipped = false;
            card4Flipped = false;
            card5Flipped = false;
            card6Flipped = false;
        }
    }

}
//card2, card4, card6
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class Card2 here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Card2 extends Cards
{
    /**
     * Act - do whatever the Card2 wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        if(cards == 6)
        {
            changeImage();
        }
        else if(cards == 4)
        {
            changeImage();
        }
        else if(cards == 2)
        {
            changeImage();
        }
    }    

    private void changeImage()
    {
        if(Greenfoot.mouseClicked(this))
        {
            if((card2Flipped == true) && (counter > 0))
            {
                counter = counter - 1;
                setImage(front);
                card2Flipped = false;
            }
            else
            {
                if(counter < 2)
                {
                    counter = counter + 1;
                    if(level == 1)
                    {
                        setImage(wolf);
                        card2Flipped = true;
                    }
                    else if(level == 2)
                    {  
                        setImage(lion);
                        card2Flipped = true;
                    }
                    else if(level == 3)
                    {
                        setImage(elephant);
                        card2Flipped = true;
                    }
                }
            }
            if(counter == 2)
            {
                if((card1Flipped == true) && (card2Flipped == true))
                {

                }
                else
                {
                    Greenfoot.delay(15);
                    setImage(front);
                    card2Flipped = false;
                    counter = counter - 1;
                }
            }
        }
    }

}
A lot of code, sorry. So instead of doing if(card1 & card3 flipped), if(card1 & card4 flipped), I did:
if(2 cards flipped)
{
    if(correct combo)
    {
         correctMethod();
    }
    else 
    {
        delay(15);
        flip card back;
        thisCardFlipped = false;
        counter - 1;

    }}
Now when I click 2 incorrect cards. The card clicked first will stay, while the card clicked second will wait a small amount, and then flip back, while the card clicked first is still showing the image. I changed a lot of code and probably messed up bad somewhere, but cannot find where. Thanks for your help beforehand
There are more replies on the next page.
1
2
3
4