package cfvbaibai.cardfantasy.engine;

import java.util.ArrayList;
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;

import cfvbaibai.cardfantasy.CardFantasyRuntimeException;

public abstract class CardPile {

    private List<CardInfo> cards;

    protected CardPile() {
        this.cards = new LinkedList<CardInfo>();
    }

    protected List<CardInfo> getCards() {
        return this.cards;
    }
    
    public List<CardInfo> toList() {
        return new ArrayList<CardInfo>(this.getCards());
    }

    public void addCard(CardInfo newCard) {
        checkCardExistence(newCard);
        this.cards.add(newCard);
    }

    private void checkCardExistence(CardInfo newCard) {
        if (cards.contains(newCard)) {
            throw new CardFantasyRuntimeException("Cannot add an existing card to CardPile. "
                    + newCard.getShortDesc());
        }
    }

    public int size() {
        return this.cards.size();
    }

    public List<CardInfo> pickRandom(int max, boolean noEmptyPosition, CardInfo exclusion) {
        if (max == 0) {
            return new ArrayList<CardInfo>();
        }
        if (max < 0) {
            max = cards.size();
        }
        List<CardInfo> clone = new ArrayList<CardInfo>(cards);
        Collections.shuffle(clone);
        List<CardInfo> result = new LinkedList<CardInfo>();
        for (CardInfo card : clone) {
            if (card == null && noEmptyPosition) {
                continue;
            }
            if (card == exclusion) {
                continue;
            }
            result.add(card);
            if (result.size() == max) {
                break;
            }
        }
        return result;
    }
    
    public List<CardInfo> pickRandom(int max, boolean noEmptyPosition) {
        return pickRandom(max, noEmptyPosition, null);
    }
}

@@ -94,16 +94,46 @@
 tions;%0D%0A
+import java.util.Comparator;%0D%0A
 import j
@@ -1290,32 +1290,38 @@
      %7D%0D%0A        
+final 
 List%3CCardInfo%3E c
@@ -1785,32 +1785,269 @@
   %7D%0D%0A        %7D%0D%0A
+        Collections.sort(result, new Comparator%3CCardInfo%3E() %7B%0D%0A            @Override%0D%0A            public int compare(CardInfo o1, CardInfo o2) %7B%0D%0A                return clone.indexOf(o1) - clone.indexOf(o2);%0D%0A            %7D%0D%0A        %7D);%0D%0A
         return r

Fix bug #55: Recover targets order after random pick by shuffling.