001    package jagafa.scores;
002    
003    import jagafa.rule.RuleSet;
004    
005    import java.util.LinkedHashMap;
006    import java.util.LinkedList;
007    import java.util.List;
008    import java.util.Map;
009    
010    public abstract class AbstractScoreTable implements ScoreTable {
011            private static final int PLAYERS = 4;
012    
013            private List<Object> roundScores_;
014    
015            private List<RuleSet> roundRules_;
016    
017            private Map<String, RuleSet> availableRules_;
018    
019            private int scores_[];
020    
021            private int startPlayer_;
022    
023            private List<Integer> roundChooser_;
024    
025            protected int choosingPlayer_;
026    
027            private boolean canChangePlayer_ = true;
028    
029            public AbstractScoreTable() {
030                    scores_ = new int[PLAYERS];
031                    this.roundRules_ = new LinkedList<RuleSet>();
032                    this.availableRules_ = new LinkedHashMap<String, RuleSet>();
033                    this.roundScores_ = new LinkedList<Object>();
034                    this.roundChooser_ = new LinkedList<Integer>();
035                    this.startPlayer_ = 0;
036            }
037    
038            public boolean isRuleAvailable(RuleSet rule) {
039    
040                    return (this.availableRules_.get(rule.getName()) != null);
041            }
042    
043            public void activateRule(RuleSet rule) {
044                    this.availableRules_.put(rule.getName(), rule);
045            }
046    
047            public void deactivateRule(RuleSet rule) {
048                    this.availableRules_.put(rule.getName(), null);
049    
050            }
051    
052            public List<RuleSet> getRules() {
053                    List<RuleSet> l = new LinkedList<RuleSet>();
054                    RuleSet arr[] = new RuleSet[this.availableRules_.size()];
055    
056                    this.availableRules_.values().toArray(arr);
057    
058                    for (int i = 0; i < arr.length; i++) {
059                            l.add(arr[i]);
060                    }
061    
062                    return l;
063            }
064    
065            public void addScores(int[] scores, RuleSet rules) {
066                    if (scores.length != scores_.length) {
067                            System.err.println("Error: Not a valid score array");
068                    }
069                    this.roundScores_.add(scores);
070                    this.roundRules_.add(rules);
071                    this.roundChooser_.add(this.getStartPlayer());
072                    for (int i = 0; i < scores_.length; i++) {
073                            this.scores_[i] += scores[i];
074                    }
075            }
076    
077            public int[] getScores() {
078                    return this.scores_;
079            }
080    
081            public int getStartPlayer() {
082                    return this.startPlayer_;
083            }
084    
085            public void setStartPlayer(int pindex) {
086                    if (pindex >= 4) {
087                            pindex -= 4;
088                    }
089                    this.startPlayer_ = pindex;
090                    this.choosingPlayer_ = this.startPlayer_;
091            }
092    
093            public void resetScores() {
094                    scores_ = new int[PLAYERS];
095                    this.roundRules_ = new LinkedList<RuleSet>();
096                    this.roundScores_ = new LinkedList<Object>();
097                    this.roundChooser_ = new LinkedList<Integer>();
098                    this.startPlayer_ = 0;
099                    this.setChoosingPlayer(this.startPlayer_);
100            
101            }
102    
103            protected void setChoosingPlayer(int i) {
104                    this.choosingPlayer_ = i;
105                    
106            }
107    
108            public int roundsPlayed() {
109    
110                    return this.roundScores_.size();
111            }
112    
113            public String toString() {
114                    String res = new String();
115                    float mean1 = 0;
116                    float mean2 = 0;
117                    for (int i = 0; i < this.roundRules_.size(); i++) {
118                            RuleSet roundRule = this.roundRules_.get(i);
119                            int scores[] = (int[]) this.roundScores_.get(i);
120    
121                            int team1 = scores[0] + scores[2];
122                            int team2 = scores[1] + scores[3];
123    
124                            res += "Round   " + i + "\tTeam1: " + team1;
125                            res += " Team2: " + (team2);
126                            res += "\tChoice: " + this.roundChooser_.get(i) + "\t" + roundRule.getName();
127                            res += "\n";
128    
129                            int chooser = this.roundChooser_.get(i);
130                            if (chooser == 0 || chooser == 2) {
131                                    mean1 += team1;
132                            } else {
133                                    mean2 += team2;
134                            }
135                    }
136    
137                    mean1 /= this.roundsPlayed() / 2;
138                    mean2 /= this.roundsPlayed() / 2;
139    
140                    int team1 = this.getScores()[0] + this.getScores()[2];
141                    int team2 = this.getScores()[1] + this.getScores()[3];
142    
143                    res += "---------------------------------------------------------------------------------------------\n";
144                    res += "Sum:      " + "\tTeam1: " + (team1);
145                    res += " Team2: " + (team2) + "\n";
146    
147                    res += "Mean:     " + "\tTeam1: " + (mean1);
148                    res += " Team2: " + (mean2) + "\n";
149    
150                    return res;
151            }
152    
153            /**
154             * Does nothing by default
155             */
156            public boolean changeChoosingPlayer() {
157                    return false;
158            }
159    
160            public boolean canChangeChoosingPlayer() {
161                    
162                    return this.canChangePlayer_;
163            }
164            
165            protected void setCanChangePlayer(boolean canChange) {
166                    this.canChangePlayer_ = canChange;
167            }
168            
169            
170    }