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

2019/6/19

Card not removing from hand

1
2
3
CP7 CP7

2019/6/19

#
So I have 7 cards in my hand, a game of UNO, so if I have the same color or number, I can play the card in the middle and the card is removed from my hand. Problem is, the card isnt being removed and I can't figure out why. I'm doing this through a CPU, so it checks every card in the list one by one, to see if it matches the card in the middle. Also, the code I have to move the rest of the cards in the hand to make it so there isnt a gap remaining after the CPU plays the card also does not work.
public void cpuTurn()
    {
        if (allowPlay == false)
        {
            for (int i = 0; i < playerTwoHand.size(); i++)
            {
                Card1 card = cards.get(i);
                if (card.getCardColor().equals(currentColor) || card.getCardValue().equals(currentValue) || card.getCardColor().equals(""))
                {
                    if (card.getCardValue().equals("+2"))
                    {
                        //If the clicked card is a +2 card
                        for (int j = 0; i < 2; i++)
                        {
                            addCardOne();
                            updateCounter();
                        }
                    }
                    else if (card.getCardValue().equals("Skip"))
                    {
                        skipPlayed2 = true;
                    }
                    else if (card.getCardValue().equals("+4"))
                    {
                        //if the clicked card is a +4 card
                        for (int j = 0; i < 4; i++)
                        {
                            addCardOne();
                            updateCounter();
                        }
                        setRandomColor();
                        System.out.println("Color is " + currentColor);
                    }
                    else if (card.getCardValue().equals("Wild"))
                    {
                        //If the clicked card is a Wild card
                        setRandomColor();
                        System.out.println("Color is " + currentColor);
                    }
                    noPlayDraw2 = false;
                    allowPlay = true;
                    noPlayDraw = true;
                    currentColor = card.getCardColor();
                    currentValue = card.getCardValue();
                    //Moves other cards to keep hand centered
                    for (int j = playerOneHand.indexOf(card) + 1; j < playerTwoHand.size(); j++)     //This part moves the cards that weren't played so there isn't a gap
                    {
                        Card1 card1 = playerTwoHand.get(j);
                        card1.setLocation(card1.getX() - 50, card1.getY());
                    }
                    Card1 card2 = new Card1(currentValue, currentColor);
                    card2.setImage(currentValue + currentColor + ".png");
                    getWorld().addObject(card2, 350, 225);
                    playerTwoHand.remove(card);
                    numCardsHand2 --;
                    Greenfoot.playSound("Placing Cards.mp3");
                    if (skipPlayed2)
                    {
                        allowPlay = false;
                        noPlayDraw = false;
                        skipPlayed2 = false;
                    }
                    getWorld().removeObject(card);       //This is not working
                }
                else
                {
                    noPlayDraw2 = true;
                }
            }

            if (noPlayDraw2 == true)
            {
                addCardTwo();
                allowPlay = true;
                noPlayDraw = true;
            }
        }
    }
Also, my code is running all at once, so the user can't really see what's going on. How can I make it so the user can see what's happening?
danpost danpost

2019/6/19

#
The parameters for the for loop at line 46 use two different lists -- one for the starting value and another one for the loop conditional.
CP7 CP7

2019/6/19

#
Oh oops that was my mistake lol
CP7 CP7

2019/6/19

#
Now it's moving all of the cards instead of just the cards after the one that was played
danpost danpost

2019/6/19

#
CP7 wrote...
my code is running all at once, so the user can't really see what's going on. How can I make it so the user can see what's happening?
You are already making use of println statements in your code. You could further use it to let the player know what is going on or you could animate actions being performed. Even with animation, you would still want indicators to show the current state of the game.
danpost danpost

2019/6/19

#
CP7 wrote...
Now it's moving all of the cards instead of just the cards after the one that was played
Show your revised code.
CP7 CP7

2019/6/19

#
 for (int j = playerTwoHand.indexOf(card) + 1; j < playerTwoHand.size(); j++)
                    {
                        Card1 card1 = playerTwoHand.get(j);
                        card1.setLocation(card1.getX() - 50, card1.getY());
                    }
CP7 CP7

2019/6/19

#
danpost wrote...
CP7 wrote...
my code is running all at once, so the user can't really see what's going on. How can I make it so the user can see what's happening?
You are already making use of println statements in your code. You could further use it to let the player know what is going on or you could animate actions being performed. Even with animation, you would still want indicators to show the current state of the game.
Wouldn't everything still be running at the same time? So for example, lets say I didn't have a card to play, so I drew a card and then I was able to play the drawn card. It would only show the card played after you draw from the deck, not draw --> into hand --> play
danpost danpost

2019/6/19

#
CP7 wrote...
<< Code Omitted >>
Are the card positioned in the world in the same exact order as they are in the list? Truthfully, I would suggest that you remove all the cards in the hand from the world and add them back into the world in their new positions. Actually, I wouldn't -- but, it would be the easy thing to do. I would animate the cards moving to close the gap.
CP7 CP7

2019/6/19

#
They're positioned left to right, 0-however long the list is
danpost danpost

2019/6/19

#
CP7 wrote...
Wouldn't everything still be running at the same time? So for example, lets say I didn't have a card to play, so I drew a card and then I was able to play the drawn card. It would only show the card played after you draw from the deck, not draw --> into hand --> play
Without animation -- yes. Again, the use of println can be used to show "CPU draws card and plays drawn card."
CP7 CP7

2019/6/19

#
danpost wrote...
CP7 wrote...
<< Code Omitted >>
Are the card positioned in the world in the same exact order as they are in the list? Truthfully, I would suggest that you remove all the cards in the hand from the world and add them back into the world in their new positions. Actually, I wouldn't -- but, it would be the easy thing to do. I would animate the cards moving to close the gap.
The cards move correctly, but all the cards move instead of only the cards after the index of the card that was played
danpost danpost

2019/6/19

#
danpost wrote...
I would suggest that you remove all the cards in the hand from the world and add them back into the world in their new positions.
Even better, just set the new position of all the cards in the hand (even if they are not to be moved).
CP7 CP7

2019/6/19

#
danpost wrote...
danpost wrote...
I would suggest that you remove all the cards in the hand from the world and add them back into the world in their new positions.
Even better, just set the new position of all the cards in the hand (even if they are not to be moved).
Not sure what you mean by this, could you provide the code for it?
danpost danpost

2019/6/19

#
CP7 wrote...
Not sure what you mean by this, could you provide the code for it?
How are the hand cards initially placed into the world when dealing the 7 initial cards (show code).
There are more replies on the next page.
1
2
3