001    /*
002     * Created on 16.03.2005 Filename: AIMethods.java
003     */
004    package jagafa.stats;
005    
006    import jagafa.object.Board;
007    import jagafa.object.Card;
008    import jagafa.object.CardList;
009    import jagafa.object.Hand;
010    import jagafa.rule.RuleSet;
011    
012    import java.util.Iterator;
013    
014    /**
015     * CardTool contains some helper Methods to deal with cards and CardLists
016     */
017    public final class CardTool {
018            
019            /**
020             * Get all Cards in the CardList with the color given
021             * @return A CardList containing all Cards with the color specified from another CardList
022             * @param cards The source CardsList
023             * @param color The relevant color
024             */
025            public static CardList getAllOfColor(CardList cards, int color) {
026                    boolean debug = true;
027    
028                    CardList colorList = new CardList();
029                    Iterator<Card> cardIter = cards.iterator();
030    
031                    while (cardIter.hasNext()) {
032                            Card nextC = cardIter.next();
033    
034                            if (nextC.getColor() == (color)) {
035                                    colorList.add(nextC);
036                            }
037    
038                    }
039                    return colorList;
040            }
041    
042            /** 
043             * Check wether the card is a Bock of level n or not
044             * @return True, if the Card can only be beaten by n(th) Cards left in the game
045             * @param nth The level of the Bock
046             * @param c The card to check
047             * @param gone A CardList of all cards gone in the game
048             * @return rules The RuleSet to apply
049             */
050            public static boolean isBock(int nth, Card c, CardList gone, RuleSet rules) {
051                    Iterator<Card> goneIter = gone.iterator();
052                    int numberOfHigherGone = 0;
053                    Board board = new Board();
054                    board.addCard(c, null);
055    
056                    while (goneIter.hasNext()) {
057                            Card nextC = goneIter.next();
058                            if (nextC != null) {
059                                    if (rules.compareHigher(nextC, board)
060                                                    && nextC.getColor() == c.getColor()) {
061                                            numberOfHigherGone++;
062                                    }
063                            }
064                    }
065    
066                    if (numberOfHigherGone == (8 - nth) - rules.getValueRank(c, board)) {
067                            return true;
068                    } else {
069                            return false;
070                    }
071            }
072    
073            /**
074             * Get all Valid Cards out of the Hand for the board with 
075             * the Ruleset specified
076             * @return A CardList containing all card out of A hand which can be played onto the board
077             * @param hand The relevant hand
078             * @param board The relevant board
079             * @param rules The RuleSet to apply
080             */
081            public static CardList getValidCards(Hand hand, Board board, RuleSet rules) {
082                    CardList cardL = new CardList();
083    
084                    for (int i = 0; i < hand.size(); i++) {
085                            Card c = hand.get(i);
086                            if (rules.isValid(c, hand, board)) {
087                                    cardL.add(c);
088                            }
089                    }
090    
091                    return cardL;
092            }
093    
094    }