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 TrumpfRules 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 TrumpfRules(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==null) {
111                            return false;
112                    }
113                    if (cardToCheck.getColor()==trumpfColor_){
114                            return true;
115                    }
116                    if (board.cardsPlayed() > 0) {
117                            Card firstCard = board.get(0);
118                            int firstColor = firstCard.getColor();
119    
120                            // Wenn keine Karten von der startFarbe auf der hand, dann egal
121                            // welche Karte gespielt wird
122                            if (firstColor==trumpfColor_){
123                                    CardList caList = CardTool.getAllOfColor(hand, firstColor);
124                                    if (caList.size()==1 && caList.contains(new Card(trumpfColor_,EValueNames.VJ.ordinal()))){
125                                            return true;
126                                    }
127                            }
128                            
129                            if (CardTool.getAllOfColor(hand, firstColor).size() == 0) {
130                                    return true;
131                            } else {
132                                    // Wenn noch Karten von der startFarbe, dann muss Karte von der
133                                    // gleichen Farbe sein
134                                    if (cardToCheck.getColor() == (firstColor)) {
135                                            return true;
136                                    } else {
137                                            
138                                            return false;
139                                    }
140                            }
141    
142                    }
143    
144                    return true;
145            }
146    
147            
148            public int getValueRank(Card c, Board b) {
149                    if (c.getColor()==trumpfColor_){
150                            for (int i = 0;i<9;i++){
151                                    if (TrumpfHierarchy.getName(i).equals(EValueNames.getName(c.getValue()))) {
152                                            return TrumpfHierarchy.values()[i].ordinal();
153                                    }
154                            }
155                    }
156                    
157                    for (ObenHierarchy o : ObenHierarchy.values()) {
158                            if (o.ordinal() == c.getValue()) {
159                                    return o.ordinal();
160                            }
161                    }
162                    return -1;
163            }
164    
165            
166            public int getColorRank(Card c, Board b) {
167                    if (c.getColor()==trumpfColor_){
168                            
169                            return 5;
170                    }
171                    
172                    if (b.cardsPlayed() > 0) {
173                            Card firstCard = b.get(0);
174                            int firstColor = firstCard.getColor();
175                            
176                            if (firstColor == c.getColor()) {
177                                    
178                                    return 2;
179                            } else {
180                                    return 1;
181                            }
182                    }
183    
184                    return 2;
185            }
186    
187            
188            public boolean compareHigher(Card c1, Board board) {
189            int posc1 = getPower(c1, board);
190            int highestOnBoard = 0;
191    
192            Iterator<Card> boardIter = board.iterator();
193    
194            while (boardIter.hasNext()) {
195                Card nextC = boardIter.next();
196                int posBoard = this.getPower(nextC, board);
197                if (posBoard > highestOnBoard) {
198                    highestOnBoard = posBoard;
199                }
200            }
201    
202            if (posc1 >= highestOnBoard) {
203                return true;
204            }
205            return false;
206    
207        }
208    
209            /* (non-Javadoc)
210             * @see jass.rule.Rules#getName()
211             */
212            public String getName() {
213                    return ESwissColorNames.getName(this.trumpfColor_) + " Trumpf";
214                    
215            }
216            /* (non-Javadoc)
217             * @see jass.rule.Rules#specialColor()
218             */
219            public int specialColor() {
220                    // TODO Auto-generated method stub
221                    return this.trumpfColor_;
222            }
223            
224            public int hashCode()
225            {
226                    int sum = 0;
227                    char carray[] = this.getName().toCharArray();
228                    for (int i = 0;i<carray.length;i++) {
229                            sum += (int) carray[i];
230                    }
231                    
232                    return sum;
233            }
234    
235            public void updateAfterTurn() {
236                    // TODO Auto-generated method stub
237                    
238            }
239    
240    }