001    /**
002     * 
003     */
004    package jagafa.rule;
005    
006    import jagafa.naming.ESwissColorNames;
007    import jagafa.naming.EValueNames;
008    import jagafa.object.Board;
009    import jagafa.object.Card;
010    import jagafa.object.CardList;
011    import jagafa.object.Hand;
012    import jagafa.stats.CardTool;
013    
014    import java.util.Iterator;
015    
016    /**
017     * @author Besitzer
018     * 
019     */
020    public class KijRules implements RuleSet {
021            private int trumpfColor_;
022            
023            private enum ObenHierarchy {
024                    CAS, CK, CQ, CJ, C10, C9, C8, C7, C6;
025            }
026            
027            private enum TrumpfHierarchy {
028                    V6,V7,V8,V10,VQ,VK,VAS,V9,VJ;
029    
030                    
031                    
032                    /**
033                     * @return
034                     */
035                    public static String getName(int i) {
036                            
037                                    return values()[i].name().substring(1);
038                            }
039                    
040            
041            }
042    
043            public KijRules(int trumpfColor){
044                    this.trumpfColor_ = trumpfColor;
045            }
046            
047            public Card getHigherCard(Card c1, Card c2) {
048                    Board dummy = new Board();
049                    if (this.getPower(c1, dummy) > this.getPower(c2, dummy)) {
050                            return c1;
051                    } else {
052                            return c2;
053                    }
054            }
055    
056            public int getPower(Card c, Board b) {
057                    int power = 0;
058    
059                    power = this.getValueRank(c, b);
060    
061                    power += 15 * this.getColorRank(c, b);
062                    
063                    /*if (c.getColor()==trumpfColor_){
064                            int value = c.getValue();
065                            if (value == ObenHierarchy.CJ.ordinal()){
066                                    power *= 500;
067                            }
068                            if (value == ObenHierarchy.C9.ordinal()){
069                                    power *= 250;
070                            }
071                    
072                    }*/
073                    return power;
074            }
075    
076            public int getScore(Card c) {
077                    int color = c.getColor();
078                    switch (ObenHierarchy.values()[c.getValue()]) {
079                    case C6:
080                            return 0;
081                    case C7:
082                            return 0;
083                    case C8:
084                            return 0;
085                    case C9:
086                            if (color==trumpfColor_){
087                                    return 14;
088                            }
089                            return 0;
090                    case C10:
091                            return 10;
092                    case CJ:
093                            if (color==trumpfColor_){
094                                    return 20;
095                            }
096                            
097                            return 2;
098                    case CQ:
099                            return 3;
100                    case CK:
101                            return 4;
102                    case CAS:
103                            return 11;
104                    }
105    
106                    return 0;
107            }
108    
109            public boolean isValid(Card cardToCheck, Hand hand, Board board) {
110                    if (cardToCheck.getColor()==trumpfColor_){
111                            return true;
112                    }
113                    if (board.cardsPlayed() > 0) {
114                            Card firstCard = board.get(0);
115                            int firstColor = firstCard.getColor();
116    
117                            // Wenn keine Karten von der startFarbe auf der hand, dann egal
118                            // welche Karte gespielt wird
119                            if (firstColor==trumpfColor_){
120                                    CardList caList = CardTool.getAllOfColor(hand, firstColor);
121                                    if (caList.size()==1 && caList.contains(new Card(trumpfColor_,EValueNames.VJ.ordinal()))){
122                                            return true;
123                                    }
124                            }
125                            
126                            if (CardTool.getAllOfColor(hand, firstColor).size() == 0) {
127                                    return true;
128                            } else {
129                                    // Wenn noch Karten von der startFarbe, dann muss Karte von der
130                                    // gleichen Farbe sein
131                                    if (cardToCheck.getColor() == (firstColor)) {
132                                            return true;
133                                    } else {
134                                            
135                                            return false;
136                                    }
137                            }
138    
139                    }
140    
141                    return true;
142            }
143    
144            
145            public int getValueRank(Card c, Board b) {
146                    if (c.getColor()==trumpfColor_){
147                            for (int i = 0;i<9;i++){
148                                    if (TrumpfHierarchy.getName(i).equals(EValueNames.getName(c.getValue()))) {
149                                            return TrumpfHierarchy.values()[i].ordinal();
150                                    }
151                            }
152                    }
153                    
154                    for (ObenHierarchy o : ObenHierarchy.values()) {
155                            if (o.ordinal() == c.getValue()) {
156                                    return 8-o.ordinal();
157                            }
158                    }
159                    return -1;
160            }
161    
162            
163            public int getColorRank(Card c, Board b) {
164                    if (c.getColor()==trumpfColor_){
165                            
166                            return 5;
167                    }
168                    
169                    if (b.cardsPlayed() > 0) {
170                            Card firstCard = b.get(0);
171                            int firstColor = firstCard.getColor();
172                            
173                            if (firstColor == c.getColor()) {
174                                    
175                                    return 2;
176                            } else {
177                                    return 1;
178                            }
179                    }
180    
181                    return 2;
182            }
183    
184            
185            public boolean compareHigher(Card c1, Board board) {
186            int posc1 = getPower(c1, board);
187            int highestOnBoard = 0;
188    
189            Iterator<Card> boardIter = board.iterator();
190    
191            while (boardIter.hasNext()) {
192                Card nextC = boardIter.next();
193                int posBoard = this.getPower(nextC, board);
194                if (posBoard > highestOnBoard) {
195                    highestOnBoard = posBoard;
196                }
197            }
198    
199            if (posc1 >= highestOnBoard) {
200                return true;
201            }
202            return false;
203    
204        }
205    
206            /* (non-Javadoc)
207             * @see jass.rule.Rules#getName()
208             */
209            public String getName() {
210                    return ESwissColorNames.getName(this.trumpfColor_) + " Kij";
211                    
212            }
213            /* (non-Javadoc)
214             * @see jass.rule.Rules#specialColor()
215             */
216            public int specialColor() {
217                    // TODO Auto-generated method stub
218                    return this.trumpfColor_;
219            }
220            
221            public int hashCode()
222            {
223                    int sum = 0;
224                    char carray[] = this.getName().toCharArray();
225                    for (int i = 0;i<carray.length;i++) {
226                            sum += (int) carray[i];
227                    }
228                    
229                    return sum;
230            }
231    
232            public void updateAfterTurn() {
233                    // TODO Auto-generated method stub
234                    
235            }
236    
237    }