001    /**
002     * 
003     */
004    package jagafa.view.newui;
005    
006    import jagafa.JassRound;
007    import jagafa.ai.RuleSetChoiceAI;
008    import jagafa.object.Player;
009    import jagafa.rule.RuleSet;
010    import jagafa.scores.ScoreTable;
011    import jagafa.util.view.TimedDialog;
012    
013    import java.awt.Color;
014    import java.awt.Dimension;
015    import java.awt.GridLayout;
016    import java.awt.event.ActionEvent;
017    import java.awt.event.ActionListener;
018    import java.util.Enumeration;
019    import java.util.Iterator;
020    import java.util.List;
021    
022    import javax.swing.AbstractButton;
023    import javax.swing.BorderFactory;
024    import javax.swing.ButtonGroup;
025    import javax.swing.JButton;
026    import javax.swing.JComponent;
027    import javax.swing.JFrame;
028    import javax.swing.JLabel;
029    import javax.swing.JPanel;
030    import javax.swing.JRadioButton;
031    import javax.swing.border.Border;
032    
033    /**
034     * @author Besitzer
035     *
036     */
037    public class RuleChoicePanel extends JPanel implements ActionListener {
038            private static final String SCHIEBEN = "Hand choice over";
039    
040            private static final String LET_AI_CHOOSE = "Let AI choose";
041    
042            private static final String DEAL_CARDS = "Deal";
043    
044            private static final String START_ROUND = "Set Rule";
045    
046            private static final String RESET_SCORETABLE = "Reset scores";
047    
048            private static final int RCHOICE_YPOS = 25;
049    
050            private static final int RCHOICE_WIDTH = 140;
051    
052            private static final int ADDITIONAL_CONTROLS = 6;
053    
054            private static final long serialVersionUID = 2918074812070652105L;
055    
056            private static final String TITLE = "Rule choice";
057    
058            private ButtonGroup group_;
059    
060            private int selectedRule_;
061    
062            private JassRound round_;
063    
064            private ScoreTable scoreTable_;
065    
066            private List<RuleSet> rules_;
067    
068            private JLabel ruleSetLabel_;
069    
070            private JComponent roundUI_;
071    
072            public RuleChoicePanel(JassRound round, JComponent roundUI) {
073                    this.roundUI_ = null;
074                    this.round_ = round;
075                    this.scoreTable_ = round.getScoreTable();
076    
077                    this.rules_ = scoreTable_.getRules();
078    
079                    initControls();
080    
081                    this.setLocation(0, RCHOICE_YPOS);
082                    this.setPreferredSize(new Dimension(RCHOICE_WIDTH, this.group_.getButtonCount()
083                                    * 15 + ADDITIONAL_CONTROLS * 20));
084    
085            }
086    
087            public void setRound(JassRound round) {
088                    this.round_ = round;
089                    this.scoreTable_ = round.getScoreTable();
090                    this.rules_ = this.scoreTable_.getRules();
091            }
092    
093            private void initControls() {
094                    this.addTitleBorder(TITLE);
095                    GridLayout layout = new GridLayout(rules_.size() + ADDITIONAL_CONTROLS, 1);
096                    this.setLayout(layout);
097    
098                    this.group_ = new ButtonGroup();
099    
100                    Iterator<RuleSet> ruleIter = rules_.iterator();
101                    while (ruleIter.hasNext()) {
102                            createOption(ruleIter.next());
103                    }
104    
105                    this.group_.getElements().nextElement().setSelected(true);
106                    ruleSetLabel_ = new JLabel("Trump: " + this.round_.getRules());
107    
108                    JButton okButton = new JButton(START_ROUND);
109                    JButton dealButton = new JButton(DEAL_CARDS);
110                    JButton aicButton = new JButton(LET_AI_CHOOSE);
111                    JButton resButton = new JButton(RESET_SCORETABLE);
112                    JButton schiebeButton = new JButton(SCHIEBEN);
113    
114                    this.add(ruleSetLabel_);
115                    this.add(okButton);
116                    this.add(dealButton);
117                    this.add(aicButton);
118                    this.add(resButton);
119                    this.add(schiebeButton);
120    
121                    okButton.addActionListener(this);
122                    aicButton.addActionListener(this);
123                    dealButton.addActionListener(this);
124                    resButton.addActionListener(this);
125                    schiebeButton.addActionListener(this);
126            }
127    
128            private void createOption(RuleSet rule) {
129                    int multiplier = this.scoreTable_.getMultiplier(rule);
130    
131                    JRadioButton ruleRadio = new JRadioButton(rule.getName() + " " + multiplier + "x");
132                    ruleRadio.setName(String.valueOf(group_.getButtonCount()));
133                    this.add(ruleRadio);
134    
135                    this.group_.add(ruleRadio);
136            }
137    
138            private int getSelectedIndex() {
139                    Enumeration<AbstractButton> elements = this.group_.getElements();
140                    int selIndex = 0;
141                    while (elements.hasMoreElements()) {
142                            AbstractButton button = elements.nextElement();
143                            if (button.isSelected()) {
144                                    selectedRule_ = selIndex;
145                                    break;
146                            }
147                            selIndex++;
148                    }
149                    return selectedRule_;
150            }
151    
152            public void actionPerformed(ActionEvent e) {
153                    /* TODO: RuleChoicePanel should only be UI and not start any Rounds!!! */
154    
155                    if (!this.round_.isRoundOver() && this.round_.isRunning()) {
156    
157                            //System.out.println("Running!");
158                            new TimedDialog("Game is already running!",roundUI_);
159                            return;
160                    }
161    
162                    String command = e.getActionCommand();
163    
164                    if (command.equals(START_ROUND)) {
165                            startRoundHumanChoice();
166                    } else if (command.equals(DEAL_CARDS)) {
167                            dealCards();
168                            this.startRoundComputerChoice();
169                    } else if (command.equals(LET_AI_CHOOSE)) {
170                            startRoundComputerChoice();
171    
172                    } else if (command.equals(RESET_SCORETABLE)) {
173                            resetScores();
174                    } else if (command.equals(SCHIEBEN)) {
175                            if (!this.scoreTable_.changeChoosingPlayer()) {
176    
177                                    /*System.out
178                                     .println("Es kann nur einmal geschoben werden! Bitte Trumpf bestimmen!");*/
179                                    //System.out.println("You can only hand over once! Please choose!");
180                                    new TimedDialog("You can only hand over once! Please choose!", roundUI_);
181                                    
182                            } else {
183                                    printAIHint();
184                                    this.startRoundComputerChoice();
185                            }
186                    }
187    
188                    if (this.round_.getRules() != null) {
189                            this.ruleSetLabel_.setText("Current: " + this.round_.getRules().getName());
190                    
191                    }
192            }
193    
194            public void updateRuleLabel() {
195                    if (this.round_.getRules() != null) {
196                            this.ruleSetLabel_.setText("Current: " + this.round_.getRules().getName());
197                    }
198            }
199    
200            /**
201             * Reset scoretable
202             *
203             */
204            private void resetScores() {
205                    this.scoreTable_.resetScores();
206                    this.round_.reset();
207                    new TimedDialog("Scores Reset to 0!",roundUI_);
208                    this.dealCards();
209            }
210    
211            /**
212             * Start the round with AI chosen RuleSet
213             *
214             */
215            private void startRoundComputerChoice() {
216                    Player startPlayer = this.round_.getPlayer(this.scoreTable_.getStartPlayer());
217                    Player choosingPlayer = this.round_.getPlayer(this.scoreTable_
218                                    .getChoosingPlayer());
219    
220                    if (choosingPlayer.isComputer()) {
221                            RuleSet choice = choosingPlayer.getAI().chooseRuleSet();
222    
223                            if (choice == null) {
224                                    this.scoreTable_.changeChoosingPlayer();
225                                    choosingPlayer = this.round_.getPlayer(this.scoreTable_
226                                                    .getChoosingPlayer());
227                                    if (!choosingPlayer.isComputer()) {
228                                            new TimedDialog("Your Partner handed the choice over to you! Please choose a Trump!",roundUI_);
229                                            return;
230                                    }
231                                    
232                                    startPlayer = this.round_.getPlayer(this.scoreTable_.getStartPlayer());
233    
234                                    choice = choosingPlayer.getAI().chooseRuleSet();
235                            }
236    
237                            this.round_.setRules(choice);
238                            new TimedDialog("Player " + choosingPlayer + " says : " + choice.getName()
239                                            + "\n",roundUI_);
240    
241                            /*System.out.println("Player " + choosingPlayer + " says : " + choice.getName()
242                                            + "\n");
243                            */
244                            this.round_.start(startPlayer);
245    
246                            this.round_.toggleNotify();
247                    } else {
248                            //System.out.println("Spieler ist kein Computer! Bitte Trumpf bestimmen!");
249                            //System.out.println("Please choose a Trump!");
250                            new TimedDialog("Please choose a Trump!",roundUI_);
251                            
252                            
253                            
254                    }
255            }
256    
257            /**
258             * Deal the cards
259             *
260             */
261            private void dealCards() {
262                    this.round_.setRules(this.rules_.get(getSelectedIndex()));
263                    this.round_.reset();
264                    Player startPlayer = this.round_.getPlayer(this.scoreTable_.getStartPlayer());
265    
266                    
267    
268                    
269    
270                    this.round_.toggleNotify();
271                    printAIHint();
272    
273            }
274    
275            private void printAIHint() {
276                    Player startPlayer = this.round_.getPlayer(this.scoreTable_.getStartPlayer());
277                    Player choosingPlayer = this.round_.getPlayer(this.scoreTable_
278                                    .getChoosingPlayer());
279    
280                    RuleSet choice = RuleSetChoiceAI.chooseRuleSet(choosingPlayer, this.round_);
281                    this.round_.setRules(choice);
282    
283                    if (!choosingPlayer.isComputer()) {
284                    if (this.round_.getRules() != null) {
285                            //Dump.displayMessage("AI hint:\t " + choice.getName(),this);
286                            new TimedDialog("AI hint: " + choice.getName(),roundUI_);
287                            //System.out.println("AI hint: " + choice.getName());
288                            
289                    } else {
290                            //Dump.displayMessage("AI hint:\t Hand over!",this);
291                            new TimedDialog("AI hint: Hand over!\n",roundUI_);
292                            //System.out.println("AI hint: Hand over!");
293                    }}
294            }
295    
296            /**
297             * Start the round with the user-selected RuleSet
298             *
299             */
300            private void startRoundHumanChoice() {
301    
302                    Player startPlayer = this.round_.getPlayer(this.scoreTable_.getStartPlayer());
303                    Player choosingPlayer = this.round_.getPlayer(this.scoreTable_
304                                    .getChoosingPlayer());
305    
306                    if (choosingPlayer.isComputer()) {
307                            this.startRoundComputerChoice();
308                    } else {
309                            this.round_.setRules(this.rules_.get(getSelectedIndex()));
310                            //System.out.println("Trump: " + this.rules_.get(getSelectedIndex()).getName());
311                            new TimedDialog("Trump is now: "
312                                            + this.rules_.get(getSelectedIndex()).getName(),roundUI_);
313                            this.round_.start(startPlayer);
314                            this.round_.toggleNotify();
315                    }
316            }
317    
318            private void addTitleBorder(String string) {
319                    Border b = BorderFactory.createLineBorder(Color.BLACK);
320    
321                    Border titleB = BorderFactory.createTitledBorder(b, string);
322                    this.setBorder(titleB);
323    
324            }
325    
326            public void setRoundUI(JFrame frame) {
327                    this.roundUI_ = frame.getRootPane();
328                    
329            }
330    
331            
332    
333            
334    }