Using the cards addedToWorld to select their pad right now.
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;
}
}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;
}
}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]);
}
}