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

2011/12/20

How to make actors go on top of each other?

1
2
3
kiarocks kiarocks

2011/12/22

#
Using the cards addedToWorld to select their pad right now.
danpost danpost

2011/12/23

#
I played around with my own version of solitaire (do not worry, it is not intended to be published on the site). Anyway, I started by creating an int and set each int = n; to represent the 52 cards. Then I created a method to shuffle the cards (swapping randomly chosen integers in the int). Next I created a single pad object to be the draw pile and made an addCard(int cardnumber) which creates the cards and adds them to the world, and updates its card list and card count. Using a for loop, I sent each card to the draw pile by way of pad.addCard(cardNum). The following step was to create a method in the world to deal the cards from the draw pile to there respective pads, so... I created the seven pads needed to deal the cards out and created a method to deal one card to a pad. I ran a double for loop to deal the cards out using this new method. That is where I am at right now. The cards are all in there respective place, properly on top of each other, and on their correct sides.
danpost danpost

2011/12/23

#
kiarocks kiarocks

2011/12/23

#
Pic is missing
danpost danpost

2011/12/23

#
Yea, I know. I guess I do not know how to link. Where do I upload the image to?
kiarocks kiarocks

2011/12/23

#
Photo bucket or another image uploading place
danpost danpost

2011/12/23

#
How do you get to photo bucket?
danpost danpost

2011/12/23

#
Got it!!
kiarocks kiarocks

2011/12/23

#
Can I see an example of your code? I have another idea but I would like to take a look.
danpost danpost

2011/12/23

#
What part would you like to see? I have the CardWorld (world) class, the Pad and Card (actors) classes.
kiarocks kiarocks

2011/12/23

#
All!
danpost danpost

2011/12/23

#
For the world class, I have:
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.util.List;

public class CardWorld extends World
{
    public int[] deck = new int[54];
    public int cardCount = 52;

    public CardWorld()
    {    
        super(800, 600, 1);
        for (int i = 0; i < cardCount; i++) deck[i] = i; // Creates a list of integers (0 to 51) to represent the cards
        shuffle(cardCount); // Calls a method to randomly swap integers (shuffles the order in which the cards are created)
        Pad pad = new Pad(-1); // Create the draw pile
        addObject(pad, 45, 60); // Puts the draw pile in the world
        for (int i = 0; i < 52; i++) pad.addCard(deck[i]); // Pad creates the deck and adds the cards to the draw pile
        for (int i = 0; i < 7; i++) addObject(new Pad(i), 220 + 85 * i, 170); // Adds the places to deal the cards to
        for (int i = 0; i < 4; i++) addObject(new Pad(100 + i), 266, 60); // Adds the places the cards (hopefully) will end up (if game is won)
        for (int j = 0; j < 7; j++) // (begin dealing the cards)  for each row (across) of cards
        {
            for (int i = 0; i < 7 - j; i++) //  for each card to go in this row
            {
                boolean flip = false; if (i == 0) flip = true;  // sets flip to true if it is the first card dealt in this row
                boolean cardDealt = dealToPad(i + j, flip); // deals the card to its pad
            }
        } // Dealing complete           
    }
    
    // The method 'dealToPad' recieves a pad number and a flag, and takes the top card from the draw pile and assigns it to the specified pad, flipping the card if neccessary.
    //    The pad number is the number of the pad that the top card on the draw pile is going to
    //    The flag determines if the card should be flipped up (true) or stay face-down (false)
    // The method returns a boolean to inform the caller if a card was dealt (true) or not (false)
    private boolean dealToPad(int padNum, boolean flipFlag)
    {
        Pad drawPile = getPad(-1);  // Gets a reference to the draw pile
        if (drawPile.cardList.isEmpty()) return false; // If no cards to deal, inform no card dealt
        Card card = drawPile.cardList.get(drawPile.cardCount - 1); // Gets a reference to top card of draw pile
        boolean dummy = drawPile.cardList.remove(card); // Removes the card from the array list of the draw pile pad
        drawPile.cardCount--; // Adjusts the draw pile pad's card count
        if (flipFlag) card.flip(); // Flips the card, in required
        getPad(padNum).addCard(card); // Has the recieving pad add the card to itself
        return true; // Informs a card was dealt
    }

    // The method 'shuffle' randomly swaps the integers (card numbers) in the 'deck' array
    private void shuffle(int num)
    {
        for (int i = 0; i < 800; i++)
        {
            int c1 = Greenfoot.getRandomNumber(num);
            int c2 = Greenfoot.getRandomNumber(num);
            int hold = deck[c2];
            deck[c2] = deck[c1];
            deck[c1] = hold;
        }
    }
    
    // The method 'getPad' does a methodic search for a pad with the specified pad number
    //   The pad number is the pad whose reference is returned (if found)
    // The method returns a reference to the Pad specified (if found) or null (if not found)
    private Pad getPad(int padNum)
    {
        List<Pad> pads = getObjects(Pad.class);
        Pad pad;
        for (int i = 0; i < pads.size(); i++)
        {
            pad = pads.get(i);
            if (pad.value == padNum) return pad;
        }
        System.out.println("Pad number " + padNum + " not found in world.");
        Greenfoot.stop();
        return null;
    }
}
For the Pad class:
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.util.*;

public class Pad extends Actor
{
    public List<Card> cardList;
    public int cardCount = 0;
    public int value;

    public Pad(int val)
    {
        getImage().clear();
        value = val;
        cardList = new ArrayList(54);
    }

    public void addCard(int cardNum)
    {
        if (value == -1)
        {
            Card card = new Card(cardNum, 0);
            card.pad = this;
            cardList.add(cardCount++, card);
            getWorld().addObject(card, getX(), getY());
        }            
    }
    
    public void addCard(Card card)
    {
        card.pad = this;
        cardList.add(cardCount, card);
        int cardVal = card.value;
        int cardShow = card.showVal;
        getWorld().removeObject(card);
        int stagger = 0; if (value > -1 && value < 7) stagger = 3;
        getWorld().addObject(new Card(cardVal, cardShow), getX() + (cardCount % 2) * stagger, 
            cardCount * 5 + upCardCount() * 22 + getY());
        cardCount++;
    }
    
    private int upCardCount()
    {
        int ct = 0;
        for (int i = 0; i < cardCount; i++) ct += cardList.get(i).showVal;
        return ct;
    }
}
And, for the Card class:
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

public class Card extends Actor
{
    private GreenfootImage[] image = new GreenfootImage[2];
    public int value = -1;
    public int showVal = 0;
    public Pad pad;

    public Card(int num, int show)
    {
        image[0] = new GreenfootImage("images/b1fv.png");
        switch ((num - (num % 13)) / 13)
        {
            case 0:
                image[1] = new GreenfootImage("images/Clubs/" + Integer.toString((num % 13) + 1) + ".png");
                break;
            case 1:
                image[1]  = new GreenfootImage("images/Diamonds/" + Integer.toString((num % 13) + 1) + ".png");
                break;
            case 2:
                image[1] = new GreenfootImage("images/Hearts/" + Integer.toString((num % 13) + 1) + ".png");
                break;
            case 3:
                image[1] = new GreenfootImage("images/Spades/" + Integer.toString((num % 13) + 1) + ".png");
                break;
        }
        showVal = show;
        if (showVal == 0) setImage(image[0]); else setImage(image[1]);
        value = num;
    }
    
    public void act()
    {
    }
    
    public void flip()
    {
        showVal = (showVal + 1) % 2;
        setImage(image[showVal]);
    }
}
kiarocks kiarocks

2011/12/23

#
Ok, I will use some of that like the shuffle() method and pad nums.
danpost danpost

2011/12/23

#
Already made some minor adjustments: (1) removed the 'if' at line 19 in Pad class, as it is not needed (the only time we add using int instead of card is for the initial creation of the card to the draw pile) (2) eliminated line 23 of world class and change 'flip' to '(i == 0)' in line 24 (3) changed line 29 in Card class to 'setImage(image);' Think I will have Pad's act() method ensure top-most card in flipped, so it would not have to be dealt with elsewhere.
kiarocks kiarocks

2011/12/23

#
ok, will keep in mind
There are more replies on the next page.
1
2
3