001 /**
002 *
003 */
004 package jagafa.object;
005
006 import java.util.LinkedList;
007 import java.util.List;
008
009 /**
010 * Distrubutor shuffles the cards and distributes them to a list of Players
011 * Abstract helper class (cannot be instantiated)
012 */
013 public abstract class Distributor {
014 private static final int HAND_SIZE = 9;
015 private static final int COLORS = 4;
016 private static List<Card> cardList_;
017
018 /**
019 * Distrubute the Cards to the Players in the List given
020 * @param playerList The List of Players (java.util.list)
021 *
022 */
023 public static void distributeCardsTo(List<Player> playerList) {
024 cardList_ = new LinkedList<Card>();
025 createCardList();
026
027 for (int i = 0; i < COLORS; i++) {
028 Player p = playerList.get(i);
029 for (int j = 0; j < HAND_SIZE; j++) {
030 // p.getHand().add(cardList_.get(i*9+j));
031 int random = (int) ((Math.random()) * cardList_.size());
032 p.getHand().add(cardList_.get(j+i*HAND_SIZE));
033 //cardList_.remove(random);
034 }
035 }
036 }
037
038 /**
039 * Create the cards and shuffle them
040 */
041 private static void createCardList() {
042 cardList_ = new LinkedList<Card>();
043 for (int i = 0; i < COLORS * HAND_SIZE; i++) {
044 cardList_.add(null);
045 }
046
047 for (int i = 0; i < COLORS; i++) {
048 for (int j = 0; j < HAND_SIZE; j++) {
049 Card c = new Card(i, j);
050 int random = (int) ((Math.random()) * COLORS * HAND_SIZE);
051 while (cardList_.get(random) != null) {
052 random = (int) (Math.random() * COLORS * HAND_SIZE);
053 }
054 cardList_.set(random, c);
055 }
056 }
057
058 }
059 }