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

#
numCardsHand ++;
getWorld().addObject(card, (numCardsHand*55) + (getWorld().getHeight()/8), 410);
This is looped 7 times I have other code, like creating the card object, but that's the code that adds it into the world
danpost danpost

2019/6/19

#
CP7 wrote...
<< Code Omitted >> This is looped 7 times
If the gap between cards is 55 (as suggested by the code given, why would you try only moving them back 50 when a card is removed (played)? For when a gap is produced:
for (for i=0; i<playerTwoHand.size(); i++)
{
    Card card = playerTwoHand.get(i);
    card.setLocation(i*55+getWorld().getHeight()/8, card.getY());
}
CP7 CP7

2019/6/19

#
Oh yea I forgot I changed the 50 and 55. Thank you!
CP7 CP7

2019/6/19

#
Wait it's still moving all of the cards Edit: Nvm, I got it, i had to be initialized as 1
CP7 CP7

2019/6/19

#
Sorry to bother you with so many problems, but I have it so when i press the space bar, it's the same as saying you don't have any card to play and you draw a card. However, it's drawing cards multiple times depending on how long I press the space bar. How can I make it so it only happens once, no matter how long you press the space bar?
public void noPlay()
    {
        if (Greenfoot.isKeyDown("space") && noPlayDraw)
        {
            System.out.println("You Draw");
            addCardOne();
            updateCounter();
            noPlayDraw = false;
            allowPlay = false;
            //If drawn card can be played
            Card1 drawCard = playerOneHand.get(playerOneHand.size() - 1);
            if (drawCard.getCardValue() == currentValue || drawCard.getCardColor() == currentColor)
            {
                clickedColor = drawCard.getCardColor();
                clickedValue = drawCard.getCardValue();
                if (drawCard.getCardValue().equals("+2"))
                {
                    //If the clicked card is a +2 card
                    for (int i = 0; i < 2; i++)
                    {
                        addCardTwo();
                    }
                    skipPlayed = true;
                }
                else if (drawCard.getCardValue().equals("Skip"))
                {
                    //If the clicked card is a skip card
                    skipPlayed = true;
                }
                else if (drawCard.getCardValue().equals("+4"))
                {
                    //if the clicked card is a +4 card
                    for (int i = 0; i < 4; i++)
                    {
                        addCardTwo();
                    }
                    String inputColor = JOptionPane.showInputDialog("Choose a color");
                    currentColor = inputColor;
                    skipPlayed = true;
                }
                else if (drawCard.getCardValue().equals("Wild"))
                {
                    //If the clicked card is a Wild card
                    String inputColor = JOptionPane.showInputDialog("Choose a color");
                    currentColor = inputColor;
                }
                drawCard.setLocation(350, 225);
                numCardsHand --;
                updateCounter();
                Greenfoot.playSound("Placing Cards.mp3");
                if (skipPlayed)
                {
                    allowPlay = true;
                    noPlayDraw = true;
                    skipPlayed = false;
                }
            }
        }
    }
danpost danpost

2019/6/19

#
If you are not using getKey elsewhere, you can use:
if ("space".equals(Greenfoot.getKey()) && noPlayDraw)
CP7 CP7

2019/6/19

#
Thank you for all your help!
CP7 CP7

2019/6/19

#
Do you know why the a card in the middle would move to the left after a card was played. This only happens sometimes and I don't know why it happens. For example, let's say the CPU last played a Blue 2. After I play a Blue 4, the Blue 2 would move next to the discard pile
danpost danpost

2019/6/19

#
CP7 wrote...
Do you know why the a card in the middle would move to the left after a card was played. This only happens sometimes and I don't know why it happens. For example, let's say the CPU last played a Blue 2. After I play a Blue 4, the Blue 2 would move next to the discard pile
I think this might deal with some codes not yet posted.
CP7 CP7

2019/6/19

#
This is all the code for the Deck class, but the error is probably somewhere in ifClicked(), cpuTurn(), or noPlay()
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.util.ArrayList;
import javax.swing.JOptionPane;

/**
 * Main class for running the game. Create cards to make up the deck.
 * 
 * @author Mark Lau
 * @version v1 6/9/19
 */
public class Deck1 extends Actor
{
    private String [] colors = {"Yellow", "Blue", "Red", "Green", "Yellow", "Blue", "Red", "Green"};
    private String [] values = {"0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "Reverse", "Skip", "+2"};
    private ArrayList <Card1> cards = new ArrayList <Card1> ();
    private ArrayList <Card1> playerOneHand = new ArrayList <Card1> ();
    private ArrayList <Card1> playerTwoHand = new ArrayList <Card1> ();

    private String clickedValue;
    private String clickedColor;
    private String currentValue;
    private String currentColor;
    private String imageName;

    private int numCardsHand;
    private int numCardsHand2;

    private boolean allowPlay = true;
    private boolean noPlayDraw = true;
    private boolean noPlayDraw2;
    private boolean skipPlayed;
    private boolean skipPlayed2;
    private boolean uno;

    public Deck1()
    {
        generateCards();
    }

    /**
     * Act - do whatever the Deck1 wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        ifClicked();
        uno();
        noPlay();
        cpuTurn();
        checkWin();
    }    

    /**
     * ifClicked - When clicked, places cards in middle
     */
    public void ifClicked()
    {
        if (Greenfoot.mouseClicked(null))
        {
            Object obj = Greenfoot.getMouseInfo().getActor();
            if (obj != null && (obj instanceof Card1) && allowPlay)
            {
                Card1 clickedCard = (Card1)obj;
                //Sets clicked color and value variables
                clickedColor = clickedCard.getCardColor();
                clickedValue = clickedCard.getCardValue();
                //If user did not press enter key when had 1 card left
                if (numCardsHand == 1)
                {
                    if (uno == false)
                    {
                        System.out.println("You forgot to press the Enter key!\nNow you have to draw 2 cards!");
                        for (int i = 0; i < 2; i++)
                        {
                            addCardOne();
                        }
                        allowPlay = false;
                        noPlayDraw = false;
                    }
                }
                //Allows player to use a card if it is either the same number or color
                if (clickedColor.equals(currentColor) || clickedValue.equals(currentValue) || clickedColor.equals("") && allowPlay)
                {
                    System.out.println("You played " + clickedValue + " " + clickedColor);
                    //Sets the current color and value to what was played
                    currentColor = clickedColor;
                    currentValue = clickedValue;
                    if (clickedCard.getCardValue().equals("+2"))
                    {
                        //If the clicked card is a +2 card
                        for (int i = 0; i < 2; i++)
                        {
                            addCardTwo();
                        }
                        skipPlayed = true;
                    }
                    else if (clickedCard.getCardValue().equals("Skip"))
                    {
                        //If the clicked card is a skip card
                        skipPlayed = true;
                    }
                    else if (clickedCard.getCardValue().equals("+4"))
                    {
                        //if the clicked card is a +4 card
                        for (int i = 0; i < 4; i++)
                        {
                            addCardTwo();
                        }
                        inputColors();
                        skipPlayed = true;
                    }
                    else if (clickedCard.getCardValue().equals("Wild"))
                    {
                        //If the clicked card is a Wild card
                        inputColors();
                    }
                    //Allows CPU to play next
                    allowPlay = false;
                    noPlayDraw = false;
                    //Moves other cards to keep hand centered
                    for (int i = playerOneHand.indexOf(clickedCard)+ 1; i < playerOneHand.size(); i++)
                    {
                        Card1 card = playerOneHand.get(i);
                        card.setLocation(card.getX() - 55, card.getY());
                    }
                    //Makes new card on top of the existing card in the middle
                    Card1 card2 = new Card1(currentValue, clickedCard.getCardColor());
                    card2.setImage(currentValue + clickedCard.getCardColor() + ".png");
                    getWorld().addObject(card2, 350, 225);
                    playerOneHand.remove(clickedCard);
                    //Removes the previous counter value and updates it
                    numCardsHand --;
                    updateCounter();
                    Greenfoot.playSound("Placing Cards.mp3");
                    if (skipPlayed)
                    {
                        allowPlay = true;
                        noPlayDraw = true;
                        skipPlayed = false;
                    }
                    getWorld().removeObject(clickedCard);
                }
            }
        }
    }

    /**
     * cpuTurn- When it is the CPU's turn
     */
    public void cpuTurn()
    {
        if (allowPlay == false)
        {
            for (int i = 0; i < playerTwoHand.size(); i++)
            {
                Card1 card = playerTwoHand.get(i);
                if (card.getCardColor().equals(currentColor) || card.getCardValue().equals(currentValue) || card.getCardColor().equals(""))
                {
                    System.out.println("CPU plays " + card.getCardValue() + " " + card.getCardColor());
                    //Changes current variables to color + value of played card
                    currentColor = card.getCardColor();
                    currentValue = card.getCardValue();
                    if (card.getCardValue().equals("+2"))
                    {
                        //If the chosen card is a +2 card
                        for (int j = 0; j < 2; i++)
                        {
                            addCardOne();
                            updateCounter();
                        }
                        skipPlayed2 = true;
                    }
                    else if (card.getCardValue().equals("Skip"))
                    {
                        skipPlayed2 = true;
                    }
                    else if (card.getCardValue().equals("+4"))
                    {
                        //if the chosen card is a +4 card
                        for (int j = 0; i < 4; i++)
                        {
                            addCardOne();
                            updateCounter();
                        }
                        setRandomColor();
                        System.out.println("Color is " + currentColor);
                        skipPlayed2 = true;
                    }
                    else if (card.getCardValue().equals("Wild"))
                    {
                        //If the chosen card is a Wild card
                        setRandomColor();
                        System.out.println("Color is " + currentColor);
                    }
                    //Sets variables to allow for player's turn
                    noPlayDraw2 = false;
                    allowPlay = true;
                    noPlayDraw = true;
                    //Moves other cards to keep hand centered
                    for (int j = playerTwoHand.indexOf(card)+ 1; j < playerTwoHand.size(); j++)
                    {
                        Card1 card1 = playerTwoHand.get(j);
                        card1.setLocation(card1.getX() - 55, card1.getY());
                    }
                    //Makes new card so image shows on top of discard pile
                    Card1 card2 = new Card1(currentValue, card.getCardColor());
                    card2.setImage(currentValue + card.getCardColor() + ".png");
                    getWorld().addObject(card2, 350, 225);
                    playerTwoHand.remove(card);
                    numCardsHand2 --;
                    Greenfoot.playSound("Placing Cards.mp3");
                    if (skipPlayed2)
                    {
                        allowPlay = false;
                        noPlayDraw = false;
                        skipPlayed2 = false;
                    }
                    //Removes card from hand
                    getWorld().removeObject(card);
                    //Stop for loop
                    break;
                }
                else
                {
                    noPlayDraw2 = true;
                }
            }

            if (noPlayDraw2 == true)
            {
                System.out.println("CPU Draws");
                addCardTwo();
                allowPlay = true;
                noPlayDraw = true;
                //If drawn card can be played
                Card1 drawCard = playerTwoHand.get(playerTwoHand.size() - 1);
                if (drawCard.getCardValue() == currentValue || drawCard.getCardColor() == currentColor)
                {
                    System.out.println("CPU plays " + drawCard.getCardValue() + " " + drawCard.getCardColor());
                    drawCard.setImage(drawCard.getCardValue() + drawCard.getCardColor() + ".png");
                    currentColor = drawCard.getCardColor();
                    currentValue = drawCard.getCardValue();
                    if (drawCard.getCardValue().equals("+2"))
                    {
                        //If the drawn card is a +2 card
                        for (int i = 0; i < 2; i++)
                        {
                            addCardTwo();
                        }
                        skipPlayed = true;
                    }
                    else if (drawCard.getCardValue().equals("Skip"))
                    {
                        //If the drawn card is a skip card
                        skipPlayed = true;
                    }
                    else if (drawCard.getCardValue().equals("+4"))
                    {
                        //if the drawn card is a +4 card
                        for (int i = 0; i < 4; i++)
                        {
                            addCardTwo();
                        }
                        setRandomColor();
                        System.out.println("Color is " + currentColor);
                        skipPlayed = true;
                    }
                    else if (drawCard.getCardValue().equals("Wild"))
                    {
                        //If the drawn card is a Wild card
                        setRandomColor();
                        System.out.println("Color is " + currentColor);
                    }
                    drawCard.setLocation(350, 225);
                    numCardsHand2 --;
                    Greenfoot.playSound("Placing Cards.mp3");
                    if (skipPlayed)
                    {
                        allowPlay = false;
                        noPlayDraw = false;
                        skipPlayed = false;
                    }
                }
            }
        }
    }

    /**
     * noPlay - if the player cannot play any card
     */
    public void noPlay()
    {
        if ("space".equals(Greenfoot.getKey()) && noPlayDraw)
        {
            
            addCardOne();
            updateCounter();
            noPlayDraw = false;
            allowPlay = false;
            //If drawn card can be played
            Card1 drawCard = playerOneHand.get(playerOneHand.size() - 1);
            System.out.println("You Draw " + drawCard.getCardValue() + " " + drawCard.getCardColor());
            if (drawCard.getCardValue() == currentValue || drawCard.getCardColor() == currentColor)
            {
                System.out.println("You played " + drawCard.getCardValue() + " " + drawCard.getCardColor());
                clickedColor = drawCard.getCardColor();
                clickedValue = drawCard.getCardValue();
                if (drawCard.getCardValue().equals("+2"))
                {
                    //If the drawn card is a +2 card
                    for (int i = 0; i < 2; i++)
                    {
                        addCardTwo();
                    }
                    skipPlayed = true;
                }
                else if (drawCard.getCardValue().equals("Skip"))
                {
                    //If the drawn card is a skip card
                    skipPlayed = true;
                }
                else if (drawCard.getCardValue().equals("+4"))
                {
                    //if the drawn card is a +4 card
                    for (int i = 0; i < 4; i++)
                    {
                        addCardTwo();
                    }
                    inputColors();
                    skipPlayed = true;
                }
                else if (drawCard.getCardValue().equals("Wild"))
                {
                    //If the drawn card is a Wild card
                    inputColors();
                }
                drawCard.setLocation(350, 225);
                numCardsHand --;
                updateCounter();
                Greenfoot.playSound("Placing Cards.mp3");
                if (skipPlayed)
                {
                    allowPlay = true;
                    noPlayDraw = true;
                    skipPlayed = false;
                }
            }
        }
    }

    /**
     * uno - when player has 1 card left, they must press enter key or they will draw 2 cards
     */
    public void uno()
    {
        if (numCardsHand == 1)
        {
            if (Greenfoot.isKeyDown("enter"))
            {
                System.out.println("Uno!");
                uno = true;
            }
            else
            {
                uno = false;
            }
        }
    }

    /**
     * setGameScreen - Adds card and deck objects when player buttons are clicked
     */
    public void setGameScreen()
    {
        //Starts game with 7 cards in each hand
        while (numCardsHand < 7)
        {
            addCardOne();
            addCardTwo();
        }

        //Starts the game with a card to play on
        Card1 currentCard = cards.get(Greenfoot.getRandomNumber(cards.size()));
        while (currentCard.getCardValue().equals("Wild") || currentCard.getCardValue().equals("+4"))
        {
            currentCard = cards.get(Greenfoot.getRandomNumber(cards.size()));
        }
        cards.remove(currentCard);
        currentCard.setImage(currentCard.getCardValue() + currentCard.getCardColor() + ".png");

        getWorld().addObject(currentCard, 350, 225);

        //Set current values to card that was added to the middle
        currentValue = currentCard.getCardValue();
        currentColor = currentCard.getCardColor();
    }

    /**
     * setRandomColor - When wild or +4 card from cpu is played, chooses random color
     */
    public void setRandomColor()
    {
        int randomNumber = Greenfoot.getRandomNumber(3);    //0 = red, 1 = green, 2 = blue, 3 = yellow
        if (randomNumber == 0)
        {
            currentColor = "Red";
        }
        else if (randomNumber == 1)
        {
            currentColor = "Green";
        }
        else if (randomNumber == 2)
        {
            currentColor = "Blue";
        }
        else if (randomNumber == 3)
        {
            currentColor = "Yellow";
        }
    }

    /**
     * inputColors - sets input colors depending on what user inputs into dialog
     */
    public void inputColors()
    {
        String inputColor = JOptionPane.showInputDialog("Choose a color");
        if (inputColor.equals("Red") || inputColor.equals("red"))
        {
            currentColor = "Red";
        }
        else if (inputColor.equals("Green") || inputColor.equals("green"))
        {
            currentColor = "Green";
        }
        else if (inputColor.equals("Blue") || inputColor.equals("blue"))
        {
            currentColor = "Blue";
        }
        else if (inputColor.equals("Yellow") || inputColor.equals("yellow"))
        {
            currentColor = "Yellow";
        }
        System.out.println("Color is " + currentColor); 
    }

    /**
     * addCardOne - Adds random card to player one
     */
    public void addCardOne()
    {
        //Chooses a random card from the total card list, removes it from that list and adds it to the hand
        Card1 card = cards.get(Greenfoot.getRandomNumber(cards.size()));
        playerOneHand.add(card);
        cards.remove(card);
        numCardsHand ++;
        //Creates the new card and sets its position
        card.setImage(card.getCardValue() + card.getCardColor() + ".png");
        getWorld().addObject(card, (numCardsHand*55) + (getWorld().getHeight()/8), 410);
    }

    /**
     * addCardTwo - Adds random card to player two
     */
    public void addCardTwo()
    {
        //Chooses a random card from the total card list, removes it from that list and adds it to the hand
        Card1 card = cards.get(Greenfoot.getRandomNumber(cards.size()));
        playerTwoHand.add(card);
        cards.remove(card);
        numCardsHand2 ++;
        //Creates the new card and sets its position
        card.setImage("Back.png");
        getWorld().addObject(card, (numCardsHand2*55) + (getWorld().getHeight()/8), 40);
    }

    /**
     * updateCounter - updates the counter
     */
    public void updateCounter()
    {
        getWorld().removeObjects(getWorld().getObjects(Counter.class));
        Counter counter = new Counter(numCardsHand);
        getWorld().addObject(counter, 665, 350);
    }

    /**
     * checkWin - Check if player has won
     */
    public void checkWin()
    {
        if (numCardsHand == 0)
        {
            Instructions instruct = new Instructions("one win");
            getWorld().addObject(instruct, 400, 225);

            getWorld().removeObjects(getWorld().getObjects(Card1.class));
            getWorld().removeObjects(getWorld().getObjects(Counter.class));
            getWorld().removeObjects(getWorld().getObjects(Deck1.class));
            Greenfoot.stop();
        }

        if (numCardsHand2 == 0)
        {
            Instructions instruct = new Instructions("one lose");
            getWorld().addObject(instruct, 400, 225);

            getWorld().removeObjects(getWorld().getObjects(Card1.class));
            getWorld().removeObjects(getWorld().getObjects(Counter.class));
            getWorld().removeObjects(getWorld().getObjects(Deck1.class));
            Greenfoot.stop();
        }
    }

    /**
     * generateCards- Creates deck of cards and puts them in list
     */
    public void generateCards()
    {
        for (int i = 0; i < 8; i++)
        {
            for (int j = 0; j < 13; j++)
            {
                Card1 card = new Card1(values[j], colors[i]);
                cards.add(card);
            }
        }

        for (int k = 0; k < 4; k++)
        {
            Card1 card = new Card1("Wild", "");
            Card1 card1 = new Card1("+4", "");
            cards.add(card);
            cards.add(card1);
        }
    }
}
danpost danpost

2019/6/19

#
It may be the for loop, lines 121 thru 124, in the Deck class. Try setting position of all cards in hand as we did previously.
CP7 CP7

2019/6/19

#
danpost wrote...
It may be the for loop, lines 121 thru 124, in the Deck class. Try setting position of all cards in hand as we did previously.
Whenever I reach 3 cards left in my hand and try to play a card, I get an error saying "java.lang.IllegalStateException: Actor not in world. An attempt was made to use the actor's location while it is not in the world. Either it has not yet been inserted, or it has been removed." The error points to the code of the setLocation in the loop we were talking about. For some reason, numCardsHand is still 4, which causes the issue
danpost danpost

2019/6/19

#
CP7 wrote...
Whenever I reach 3 cards left in my hand and try to play a card, I get an error saying "java.lang.IllegalStateException: Actor not in world. An attempt was made to use the actor's location while it is not in the world. Either it has not yet been inserted, or it has been removed." The error points to the code of the setLocation in the loop we were talking about.
Show revised code. What is the rest of the error message?
For some reason, numCardsHand is still 4, which causes the issue
I do not know why you would have that field to begin with. The number of cards in hand should always be the size of the list of hand cards. It just complicates matters by adding fields whose values can be otherwise be easily determined. You need to constantly update the field and if you miss doing so, you get errors.
CP7 CP7

2019/6/19

#
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) import java.util.ArrayList; import javax.swing.JOptionPane; /** * Main class for running the game. Create cards to make up the deck. * * @author Mark Lau * @version v1 6/9/19 */ public class Deck1 extends Actor { private String colors = {"Yellow", "Blue", "Red", "Green", "Yellow", "Blue", "Red", "Green"}; private String values = {"0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "Reverse", "Skip", "+2"}; private ArrayList <Card1> cards = new ArrayList <Card1> (); private ArrayList <Card1> playerOneHand = new ArrayList <Card1> (); private ArrayList <Card1> playerTwoHand = new ArrayList <Card1> (); private ArrayList <Card1> discardPile = new ArrayList <Card1> (); private String clickedValue; private String clickedColor; private String currentValue; private String currentColor; private String imageName; private int numCardsHand; private int numCardsHand2; private boolean allowPlay = true; private boolean noPlayDraw = true; private boolean noPlayDraw2; private boolean skipPlayed; private boolean skipPlayed2; private boolean uno; public Deck1() { generateCards(); } /** * Act - do whatever the Deck1 wants to do. This method is called whenever * the 'Act' or 'Run' button gets pressed in the environment. */ public void act() { ifClicked(); uno(); noPlay(); cpuTurn(); checkWin(); } /** * ifClicked - When clicked, places cards in middle */ public void ifClicked() { if (Greenfoot.mouseClicked(null)) { Object obj = Greenfoot.getMouseInfo().getActor(); if (obj != null && (obj instanceof Card1) && allowPlay) { Card1 clickedCard = (Card1)obj; //Sets clicked color and value variables clickedColor = clickedCard.getCardColor(); clickedValue = clickedCard.getCardValue(); //If user did not press enter key when had 1 card left if (numCardsHand == 1) { if (uno == false) { System.out.println("You forgot to press the Enter key!\nNow you have to draw 2 cards!"); for (int i = 0; i < 2; i++) { addCardOne(); } allowPlay = false; noPlayDraw = false; } } //Allows player to use a card if it is either the same number or color if (clickedColor.equals(currentColor) && allowPlay || clickedValue.equals(currentValue) && allowPlay || clickedColor.equals("") && allowPlay) { System.out.println("You played " + clickedValue + " " + clickedColor); //Sets the current color and value to what was played currentColor = clickedColor; currentValue = clickedValue; if (clickedCard.getCardValue().equals("+2")) { //If the clicked card is a +2 card for (int i = 0; i < 2; i++) { addCardTwo(); } skipPlayed = true; } else if (clickedCard.getCardValue().equals("Skip")) { //If the clicked card is a skip card skipPlayed = true; } else if (clickedCard.getCardValue().equals("+4")) { //if the clicked card is a +4 card for (int i = 0; i < 4; i++) { addCardTwo(); } inputColors(); skipPlayed = true; } else if (clickedCard.getCardValue().equals("Wild")) { //If the clicked card is a Wild card inputColors(); } //Removes previous card in the discard pile for (int i = 0; i < discardPile.size(); i ++) { Card1 deleteCard = discardPile.get(i); getWorld().removeObject(deleteCard); discardPile.remove(deleteCard); } //Adds clicked card to discard pile discardPile.add(clickedCard); //Allows CPU to play next allowPlay = false; noPlayDraw = false; //Moves other cards to keep hand centered for (int i = playerOneHand.indexOf(clickedCard)+ 1; i < playerOneHand.size(); i++) { if (i >= numCardsHand || i >= playerOneHand.size()) { break; } Card1 card = playerOneHand.get(i); card.setLocation(card.getX() - 55, card.getY()); } //Makes new card on top of the existing card in the middle Card1 card2 = new Card1(currentValue, clickedCard.getCardColor()); card2.setImage(currentValue + clickedCard.getCardColor() + ".png"); getWorld().addObject(card2, 350, 225); playerOneHand.remove(clickedCard); //Removes the previous counter value and updates it numCardsHand --; updateCounter(); Greenfoot.playSound("Placing Cards.mp3"); if (skipPlayed) { allowPlay = true; noPlayDraw = true; skipPlayed = false; } getWorld().removeObject(clickedCard); } } } } /** * cpuTurn- When it is the CPU's turn */ public void cpuTurn() { if (allowPlay == false) { for (int i = 0; i < playerTwoHand.size(); i++) { Card1 card = playerTwoHand.get(i); if (card.getCardColor().equals(currentColor) || card.getCardValue().equals(currentValue) || card.getCardColor().equals("")) { System.out.println("CPU plays " + card.getCardValue() + " " + card.getCardColor()); //Changes current variables to color + value of played card currentColor = card.getCardColor(); currentValue = card.getCardValue(); if (card.getCardValue().equals("+2")) { //If the chosen card is a +2 card for (int j = 0; j < 2; j++) { addCardOne(); updateCounter(); } skipPlayed2 = true; } else if (card.getCardValue().equals("Skip")) { skipPlayed2 = true; } else if (card.getCardValue().equals("+4")) { //if the chosen card is a +4 card for (int j = 0; j < 4; j++) { addCardOne(); updateCounter(); } setRandomColor(); System.out.println("Color is " + currentColor); skipPlayed2 = true; } else if (card.getCardValue().equals("Wild")) { //If the chosen card is a Wild card setRandomColor(); System.out.println("Color is " + currentColor); } //Removes previous card in the discard pile for (int j = 0; j < discardPile.size(); j ++) { Card1 deleteCard = discardPile.get(j); getWorld().removeObject(deleteCard); discardPile.remove(deleteCard); } //Adds chosen card to discard pile discardPile.add(card); //Sets variables to allow for player's turn noPlayDraw2 = false; allowPlay = true; noPlayDraw = true; System.out.println("I have " + numCardsHand2); //Moves other cards to keep hand centered for (int j = playerTwoHand.indexOf(card)+ 1; j < playerTwoHand.size(); j++) { if (j >= numCardsHand2 || j >= playerTwoHand.size()) { break; } System.out.println(j); Card1 card1 = playerTwoHand.get(j); card1.setLocation(card1.getX() - 55, card1.getY()); } //Makes new card so image shows on top of discard pile Card1 card2 = new Card1(currentValue, card.getCardColor()); card2.setImage(currentValue + card.getCardColor() + ".png"); getWorld().addObject(card2, 350, 225); playerTwoHand.remove(card); numCardsHand2 --; Greenfoot.playSound("Placing Cards.mp3"); if (skipPlayed2) { allowPlay = false; noPlayDraw = false; skipPlayed2 = false; } //Removes card from hand getWorld().removeObject(card); //Stop for loop break; } else { noPlayDraw2 = true; } } if (noPlayDraw2 == true) { System.out.println("CPU Draws"); addCardTwo(); allowPlay = true; noPlayDraw = true; //If drawn card can be played Card1 drawCard = playerTwoHand.get(playerTwoHand.size() - 1); if (drawCard.getCardValue().equals(currentValue) || drawCard.getCardColor().equals(currentColor) || drawCard.getCardColor().equals("")) { System.out.println("CPU plays " + drawCard.getCardValue() + " " + drawCard.getCardColor()); drawCard.setImage(drawCard.getCardValue() + drawCard.getCardColor() + ".png"); currentColor = drawCard.getCardColor(); currentValue = drawCard.getCardValue(); if (drawCard.getCardValue().equals("+2")) { //If the drawn card is a +2 card for (int i = 0; i < 2; i++) { addCardTwo(); } skipPlayed = true; } else if (drawCard.getCardValue().equals("Skip")) { //If the drawn card is a skip card skipPlayed = true; } else if (drawCard.getCardValue().equals("+4")) { //if the drawn card is a +4 card for (int i = 0; i < 4; i++) { addCardTwo(); } setRandomColor(); System.out.println("Color is " + currentColor); skipPlayed = true; } else if (drawCard.getCardValue().equals("Wild")) { //If the drawn card is a Wild card setRandomColor(); System.out.println("Color is " + currentColor); } //Removes previous card in the discard pile for (int i = 0; i < discardPile.size(); i ++) { Card1 deleteCard = discardPile.get(i); getWorld().removeObject(deleteCard); discardPile.remove(deleteCard); } //Adds drawn card to discard pile discardPile.add(drawCard); drawCard.setLocation(350, 225); numCardsHand2 --; Greenfoot.playSound("Placing Cards.mp3"); if (skipPlayed) { allowPlay = false; noPlayDraw = false; skipPlayed = false; } } } } } /** * noPlay - if the player cannot play any card */ public void noPlay() { if ("space".equals(Greenfoot.getKey()) && noPlayDraw) { addCardOne(); updateCounter(); noPlayDraw = false; allowPlay = false; //If drawn card can be played Card1 drawCard = playerOneHand.get(playerOneHand.size() - 1); System.out.println("You Draw " + drawCard.getCardValue() + " " + drawCard.getCardColor()); if (drawCard.getCardValue().equals(currentValue) || drawCard.getCardColor().equals(currentColor) || drawCard.getCardColor().equals("")) { System.out.println("You played " + drawCard.getCardValue() + " " + drawCard.getCardColor()); currentColor = drawCard.getCardColor(); currentValue = drawCard.getCardValue(); if (drawCard.getCardValue().equals("+2")) { //If the drawn card is a +2 card for (int i = 0; i < 2; i++) { addCardTwo(); } skipPlayed = true; } else if (drawCard.getCardValue().equals("Skip")) { //If the drawn card is a skip card skipPlayed = true; } else if (drawCard.getCardValue().equals("+4")) { //if the drawn card is a +4 card for (int i = 0; i < 4; i++) { addCardTwo(); } inputColors(); skipPlayed = true; } else if (drawCard.getCardValue().equals("Wild")) { //If the drawn card is a Wild card inputColors(); } //Removes previous card in the discard pile for (int i = 0; i < discardPile.size(); i ++) { Card1 deleteCard = discardPile.get(i); getWorld().removeObject(deleteCard); discardPile.remove(deleteCard); } //Adds drawn card to discard pile discardPile.add(drawCard); drawCard.setLocation(350, 225); numCardsHand --; updateCounter(); Greenfoot.playSound("Placing Cards.mp3"); if (skipPlayed) { allowPlay = true; noPlayDraw = true; skipPlayed = false; } } } } /** * uno - when player has 1 card left, they must press enter key or they will draw 2 cards */ public void uno() { if (numCardsHand == 1) { if (Greenfoot.isKeyDown("enter")) { System.out.println("Uno!"); uno = true; } } } /** * setGameScreen - Adds card and deck objects when player buttons are clicked */ public void setGameScreen() { //Starts game with 7 cards in each hand while (numCardsHand < 7) { addCardOne(); addCardTwo(); } //Starts the game with a card to play on Card1 currentCard = cards.get(Greenfoot.getRandomNumber(cards.size())); while (currentCard.getCardValue().equals("Wild") || currentCard.getCardValue().equals("+4")) { currentCard = cards.get(Greenfoot.getRandomNumber(cards.size())); } cards.remove(currentCard); discardPile.add(currentCard); currentCard.setImage(currentCard.getCardValue() + currentCard.getCardColor() + ".png"); getWorld().addObject(currentCard, 350, 225); //Set current values to card that was added to the middle currentValue = currentCard.getCardValue(); currentColor = currentCard.getCardColor(); } /** * setRandomColor - When wild or +4 card from cpu is played, chooses random color */ public void setRandomColor() { int randomNumber = Greenfoot.getRandomNumber(3); //0 = red, 1 = green, 2 = blue, 3 = yellow if (randomNumber == 0) { currentColor = "Red"; } else if (randomNumber == 1) { currentColor = "Green"; } else if (randomNumber == 2) { currentColor = "Blue"; } else if (randomNumber == 3) { currentColor = "Yellow"; } } /** * inputColors - sets input colors depending on what user inputs into dialog */ public void inputColors() { String inputColor = JOptionPane.showInputDialog("Choose a color"); if (inputColor.equals("Red") || inputColor.equals("red")) { currentColor = "Red"; } else if (inputColor.equals("Green") || inputColor.equals("green")) { currentColor = "Green"; } else if (inputColor.equals("Blue") || inputColor.equals("blue")) { currentColor = "Blue"; } else if (inputColor.equals("Yellow") || inputColor.equals("yellow")) { currentColor = "Yellow"; } System.out.println("Color is " + currentColor); } /** * addCardOne - Adds random card to player one */ public void addCardOne() { //Chooses a random card from the total card list, removes it from that list and adds it to the hand Card1 card = cards.get(Greenfoot.getRandomNumber(cards.size())); playerOneHand.add(card); cards.remove(card); numCardsHand ++; //Creates the new card and sets its position card.setImage(card.getCardValue() + card.getCardColor() + ".png"); getWorld().addObject(card, (numCardsHand*55) + (getWorld().getHeight()/8), 410); } /** * addCardTwo - Adds random card to player two */ public void addCardTwo() { //Chooses a random card from the total card list, removes it from that list and adds it to the hand Card1 card = cards.get(Greenfoot.getRandomNumber(cards.size())); playerTwoHand.add(card); cards.remove(card); numCardsHand2 ++; //Creates the new card and sets its position card.setImage("Back.png"); getWorld().addObject(card, (numCardsHand2*55) + (getWorld().getHeight()/8), 40); } /** * updateCounter - updates the counter */ public void updateCounter() { getWorld().removeObjects(getWorld().getObjects(Counter.class)); Counter counter = new Counter(numCardsHand); getWorld().addObject(counter, 665, 350); } /** * checkWin - Check if player has won */ public void checkWin() { if (numCardsHand == 0) { Instructions instruct = new Instructions("one win"); getWorld().addObject(instruct, 400, 225); getWorld().removeObjects(getWorld().getObjects(Card1.class)); getWorld().removeObjects(getWorld().getObjects(Counter.class)); getWorld().removeObjects(getWorld().getObjects(Deck1.class)); Greenfoot.stop(); } if (numCardsHand2 == 0) { Instructions instruct = new Instructions("one lose"); getWorld().addObject(instruct, 400, 225); getWorld().removeObjects(getWorld().getObjects(Card1.class)); getWorld().removeObjects(getWorld().getObjects(Counter.class)); getWorld().removeObjects(getWorld().getObjects(Deck1.class)); Greenfoot.stop(); } } /** * generateCards- Creates deck of cards and puts them in list */ public void generateCards() { for (int i = 0; i < 8; i++) { for (int j = 0; j < 13; j++) { Card1 card = new Card1(values, colors); cards.add(card); } } for (int k = 0; k < 4; k++) { Card1 card = new Card1("Wild", ""); Card1 card1 = new Card1("+4", ""); cards.add(card); cards.add(card1); } } }
CP7 CP7

2019/6/19

#
java.lang.IllegalStateException: Actor not in world. An attempt was made to use the actor's location while it is not in the world. Either it has not yet been inserted, or it has been removed. at greenfoot.Actor.failIfNotInWorld(Actor.java:711) at greenfoot.Actor.getX(Actor.java:164) at Deck1.ifClicked(Deck1.java:138) at Deck1.act(Deck1.java:47) at greenfoot.core.Simulation.actActor(Simulation.java:604) at greenfoot.core.Simulation.runOneLoop(Simulation.java:562) at greenfoot.core.Simulation.runContent(Simulation.java:221) at greenfoot.core.Simulation.run(Simulation.java:211)
There are more replies on the next page.
1
2
3