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

2011/12/31

Fixing bugs in Solitaire

kiarocks kiarocks

2011/12/31

#
I am trying to make a Solitaire game. The Game In the comments, under "Bugs", the one with the ace problem, is the one this thread is about. this is the code for the card class.
public void checkDrag()
    {
        int clicks = 0;
        if(isDraggable && Greenfoot.mouseClicked(this) && !dragging)
        {
            dragging = true;
            sw.cardDragging = true;
            startx = getX();
            starty = getY();
            reAdd();
            if(pad != null) pad.removeCard(this);
            if(pad != null) prevpad = pad;
            clicks++;
        }
        else if(dragging)
        {
            MouseInfo mouse = Greenfoot.getMouseInfo();
            if(mouse != null)
                setLocation(mouse.getX(),mouse.getY());

        }
        if(dragging && Greenfoot.mouseClicked(this) && clicks == 0)
        {
            dragging = false;
            sw.cardDragging = false;
            if(!onValidCard() && !onValidPile() && !onValidPad())
            {
                setLocation(startx,starty);
                pad = prevpad;
                if(pad != null) pad.addCard(this);
                return;
            }
            else if(onValidCard() && !onValidPile())
            {
                Card landing = (Card) getOneIntersectingObject(Card.class);
                pad = landing.pad;
                setLocation(pad.topCard.getX(),pad.topCard.getY()+20);
                pad.addCard(this);
                if(location)
                {
                    location = false;
                    List<Card> list = new ArrayList<Card>(Arrays.asList(Deck.deckOrder));
                    list.remove(this);
                    Deck.deckOrder = list.toArray(EMPTY_CARD_ARRAY);
                    Deck.lastCardAdded--;

                }
                return;
            }
            else if(onValidPile())
            {
                if(location)
                {
                    location = false;
                    List<Card> list = new ArrayList<Card>(Arrays.asList(Deck.deckOrder));
                    list.remove(this);
                    Deck.deckOrder = list.toArray(EMPTY_CARD_ARRAY);
                    Deck.lastCardAdded--;

                }
                return;
            }
            else if(onValidPad())
            {
                Pad p = (Pad) getOneIntersectingObject(Pad.class);
                pad = p;
                pad.addCard(this);
                setLocation(pad.getX(),pad.getY());
                if(location)
                {
                    location = false;
                    List<Card> list = new ArrayList<Card>(Arrays.asList(Deck.deckOrder));
                    list.remove(this);
                    Deck.deckOrder = list.toArray(EMPTY_CARD_ARRAY);
                    Deck.lastCardAdded--;

                }
            }
        }
    }

    public boolean onValidPad()
    {
        Pad p = (Pad) getOneIntersectingObject(Pad.class);
        if(p != null)
        {
            if(p.topCard == null && value == 13)
            {

                return true;
            }
            else return false;
        }
        else return false;
    }

    public boolean onValidPile()
    {
        CardPile pile = (CardPile) getOneIntersectingObject(CardPile.class);
        if(pile != null)
        {
            return pile.tryCardPlace(this);
        }
        else { return false; }
    }

    public boolean onValidCard()
    {
        Card landing = (Card) getOneIntersectingObject(Card.class);
        if(landing != null)
        {
            if(landing.pad != null)
            {
                return checkOurSuitAndVal(landing.pad.topCard);
            }
            else { return false; }
        }
        else { return false; }
    }

    public boolean checkOurSuitAndVal(Card card)
    {
        if(card != null)
        {
            if(color == "red")
            {
                if(card.color == "black" && card.value == value+1) { return true; } else { return false; }
            }
            else if(color == "black")
            {
                if(card.color == "red" && card.value == value+1) { return true; } else { return false; }
            }
            else
                return false;
        }
        else return false;
    }
kiarocks kiarocks

2011/12/31

#
Any help danpost???
mjrb4 mjrb4

2011/12/31

#
If you explained what you were trying to do and put the above code in context somewhere there are those of us that might be able to help! Just starting an unexplained thread directed at one particular member isn't generally a recipe for getting the help you need fast....
kiarocks kiarocks

2011/12/31

#
I had been talking to him in a previous thread and i wanted to continue here. For more info look in the comments of my Solitaire game Under "bugs"
kiarocks kiarocks

2011/12/31

#
If you need more the source is there
danpost danpost

2011/12/31

#
The problem with the transfer from Deck to CardPile and the cards going back into the deck is NOT in lines 11 and 12, but is related to it. Those two lines remove the card from a Pad object, but the Deck is not a Pad object. You will have to deal with this seperately, or make Deck a sub-class of Pad (I do not know how easy the latter would be to do).
kiarocks kiarocks

2011/12/31

#
when a card is added from a deck it sets location to true. run if(location) statments. deckOrder is an array
danpost danpost

2012/1/2

#
I think I see the problem, but I am going to adjust the code and check it out. I believe it has to do with calling Greenfoot.mouseClicked(this) twice. Sorry, the problem still persists. :+(
kiarocks kiarocks

2012/1/2

#
Ok, i see now that location is set to TRUE when the card goes on the pile, and i have some revised code that isnt working.
public void checkDrag()
    {
        int clicks = 0;
        if(isDraggable && Greenfoot.mouseClicked(this) && !dragging)
        {
            dragging = true;
            sw.cardDragging = true;
            startx = getX();
            starty = getY();
            reAdd();
            if(pad != null) pad.removeCard(this);
            if(pad != null) prevpad = pad;
            clicks++;
        }
        else if(dragging)
        {
            MouseInfo mouse = Greenfoot.getMouseInfo();
            if(mouse != null)
                setLocation(mouse.getX(),mouse.getY());

        }
        if(dragging && Greenfoot.mouseClicked(this) && clicks == 0)
        {
            dragging = false;
            sw.cardDragging = false;
            if(!onValidCard() && !onValidPile() && !onValidPad())
            {
                setLocation(startx,starty);
                pad = prevpad;
                if(pad != null) pad.addCard(this);
                return;
            }
            if(onValidCard() && !onValidPile())
            {
                Card landing = (Card) getOneIntersectingObject(Card.class);
                pad = landing.pad;
                setLocation(pad.topCard.getX(),pad.topCard.getY()+20);
                pad.addCard(this);
                if(location)
                {
                    location = false;
                    List<Card> list = new ArrayList<Card>(Arrays.asList(Deck.deckOrder));
                    list.remove(this);
                    Deck.deckOrder = list.toArray(EMPTY_CARD_ARRAY);
                    Deck.lastCardAdded--;

                }
                return;
            }
            if(onValidPile())
            {
                if(location)
                {
                    location = false;
                    List<Card> list = new ArrayList<Card>(Arrays.asList(Deck.deckOrder));
                    list.remove(this);
                    Deck.deckOrder = list.toArray(EMPTY_CARD_ARRAY);
                    Deck.lastCardAdded--;

                }
                return;
            }
            if(onValidPad())
            {
                Pad p = (Pad) getOneIntersectingObject(Pad.class);
                pad = p;
                pad.addCard(this);
                setLocation(pad.getX(),pad.getY());
                if(location)
                {
                    location = false;
                    List<Card> list = new ArrayList<Card>(Arrays.asList(Deck.deckOrder));
                    list.remove(this);
                    Deck.deckOrder = list.toArray(EMPTY_CARD_ARRAY);
                    Deck.lastCardAdded--;

                }
            }
        }
    }
kiarocks kiarocks

2012/1/2

#
I fixed it!!! New revised code:
    public void checkDrag()
    {
        int clicks = 0;
        if(isDraggable && Greenfoot.mouseClicked(this) && !dragging)
        {
            dragging = true;
            sw.cardDragging = true;
            startx = getX();
            starty = getY();
            reAdd();
            if(pad != null) pad.removeCard(this);
            if(pad != null) prevpad = pad;
            clicks++;
        }
        else if(dragging)
        {
            MouseInfo mouse = Greenfoot.getMouseInfo();
            if(mouse != null)
                setLocation(mouse.getX(),mouse.getY());

        }
        if(dragging && Greenfoot.mouseClicked(this) && clicks == 0)
        {
            dragging = false;
            sw.cardDragging = false;
            if(!onValidCard() && !onValidPile() && !onValidPad())
            {
                setLocation(startx,starty);
                pad = prevpad;
                if(pad != null) pad.addCard(this);
            }
            if(onValidCard() && !onValidPile())
            {
                Card landing = (Card) getOneIntersectingObject(Card.class);
                pad = landing.pad;
                setLocation(pad.topCard.getX(),pad.topCard.getY()+20);
                pad.addCard(this);
                if(location)
                {
                    location = false;
                    List<Card> list = new ArrayList<Card>(Arrays.asList(Deck.deckOrder));
                    list.remove(this);
                    Deck.deckOrder = list.toArray(EMPTY_CARD_ARRAY);
                    Deck.lastCardAdded--;

                }
            }
            if(onValidPile())
            {
                if(location)
                {
                    location = false;
                    List<Card> list = new ArrayList<Card>(Arrays.asList(Deck.deckOrder));
                    list.remove(this);
                    Deck.deckOrder = list.toArray(EMPTY_CARD_ARRAY);
                    Deck.lastCardAdded--;

                }
            }
            if(onValidPad())
            {
                Pad p = (Pad) getOneIntersectingObject(Pad.class);
                pad = p;
                pad.addCard(this);
                setLocation(pad.getX(),pad.getY());
                if(location)
                {
                    location = false;
                    List<Card> list = new ArrayList<Card>(Arrays.asList(Deck.deckOrder));
                    list.remove(this);
                    Deck.deckOrder = list.toArray(EMPTY_CARD_ARRAY);
                    Deck.lastCardAdded--;

                }
            }
            if(myPile != null) // this is what made it work
            {
                if(location)
                {
                    location = false;
                    List<Card> list = new ArrayList<Card>(Arrays.asList(Deck.deckOrder));
                    list.remove(this);
                    Deck.deckOrder = list.toArray(EMPTY_CARD_ARRAY);
                    Deck.lastCardAdded--;

                }
            }
        }
    }
You need to login to post a reply.