Obliczenia brydżowe (Java ME)
Jacek Kowalski
2012-08-07 bf2df04c8c12760206ea1d5be5c7e008123cc695
commit | author | age
2e729d 1 package brydz;
JK 2
3 import javax.microedition.lcdui.*;
4
5 public class BrydzForm extends Form implements CommandListener, ItemStateListener {
6     protected BrydzMIDlet midlet;
bf2df0 7     protected BrydzMenu menu = null;
2e729d 8     
JK 9     protected TextField kontraktField;
10     protected ChoiceGroup kontraktChoice;
11     protected ChoiceGroup kontraChoice;
12     protected ChoiceGroup partiaChoice;
13     protected TextField punktyField;
14     protected TextField lewyField;
15     protected StringItem resultText;
16     
17     protected BrydzLicz liczenie;
18     
19     public BrydzForm(BrydzMIDlet midlet) {
20         super("Obliczenia brydżowe");
21         
22         this.midlet = midlet;
23         this.liczenie = new BrydzLicz();
24         
25         this.kontraktField = new TextField("Kontrakt:", null, 1, TextField.NUMERIC);
26         this.kontraktChoice = new ChoiceGroup(null, List.EXCLUSIVE);
27         this.kontraktChoice.append("♣/♦", null);
28         this.kontraktChoice.append("♥/♠", null);
29         this.kontraktChoice.append("NT", null);
30         
31         this.kontraChoice = new ChoiceGroup(null, ChoiceGroup.EXCLUSIVE);
32         this.kontraChoice.append("norm.", null);
33         this.kontraChoice.append("ktr.", null);
34         this.kontraChoice.append("rektr.", null);
35         
36         this.partiaChoice = new ChoiceGroup(null, ChoiceGroup.EXCLUSIVE);
dcd918 37         this.partiaChoice.append("przed / przed", null);
JK 38         this.partiaChoice.append("przed / po", null);
39         this.partiaChoice.append("po / przed", null);
40         this.partiaChoice.append("po / po", null);
2e729d 41         
JK 42         this.punktyField = new TextField("PC:", null, 2, TextField.NUMERIC);
43         this.lewyField = new TextField("Lew:", null, 2, TextField.NUMERIC);
44         
45         this.resultText = new StringItem(null, "Podaj wszystkie dane");
46         
47         this.append(this.kontraktField);
48         this.append(this.kontraktChoice);
49         this.append(this.kontraChoice);
50         this.append(this.partiaChoice);
51         this.append(this.punktyField);
52         this.append(this.lewyField);
53         this.append(this.resultText);
54         
bf2df0 55         this.addCommand(new Command("Wyczyść", Command.BACK, 1));
JK 56         this.addCommand(new Command("Menu", Command.OK, 1));
57         
58         this.setItemStateListener(this);
59         this.setCommandListener(this);
2e729d 60     }
JK 61     
62     public void commandAction(Command command, Displayable displayable) {
bf2df0 63         if (command.getCommandType() == Command.BACK) {
2e729d 64             this.kontraktField.setString(null);
JK 65             this.kontraktChoice.setSelectedIndex(0, true);
66             this.kontraChoice.setSelectedIndex(0, true);
67             this.partiaChoice.setSelectedIndex(0, true);
68             this.punktyField.setString(null);
69             this.lewyField.setString(null);
70             this.resultText.setText("Podaj wszystkie dane.");
93a300 71             
bf2df0 72             this.midlet.display.setCurrentItem(this.kontraktField);
2e729d 73         }
bf2df0 74         else if (command.getCommandType() == Command.OK) {
JK 75             if(this.menu == null) {
76                 this.menu = new BrydzMenu(this.midlet, this);
77             }
78             this.midlet.display.setCurrent(this.menu);
2e729d 79         }
JK 80     }
81     
82     public void itemStateChanged(Item item) {
83         int kontrakt, typ, kontra, partia, punkty, lewy;
84         
85         try {
86             kontrakt = Integer.parseInt(this.kontraktField.getString());
87             typ = this.kontraktChoice.getSelectedIndex();
88             kontra = this.kontraChoice.getSelectedIndex();
89             partia = this.partiaChoice.getSelectedIndex();
90             punkty = Integer.parseInt(this.punktyField.getString());
91             lewy = Integer.parseInt(this.lewyField.getString());
92         }
93         catch(NumberFormatException e) {
94             this.resultText.setText("Podaj wszystkie dane.");
95             return;
96         }
97         
98         BrydzWynik wynik;
dcd918 99                 
2e729d 100         if( kontrakt < 0  || kontrakt > 7 ) {
JK 101             this.resultText.setText("Niepoprawny kontrakt.");
102             return;
103         }
104         
105         if( typ != 0 && typ != 1 && typ != 2 ) {
106             this.resultText.setText("Niepoprawny typ kontraktu.");
107             return;
108         }
109         
110         if( kontra < 0 || kontra > 2 ) {
111             this.resultText.setText("Niepoprawna informacja o kontrze.");
112             return;
113         }
114         
dcd918 115         if( partia < 0 || partia > 3) {
2e729d 116             this.resultText.setText("Niepoprawna informacja o partii.");
JK 117             return;
118         }
119         
120         if( punkty < 0 || punkty > 40 ) {
121             this.resultText.setText("Niepoprawna ilość punktów");
122             return;
123         }
124         
125         if( lewy < 0 || lewy > 13 ) {
126             this.resultText.setText("Niepoprawna ilość wziętych lew.");
127             return;
128         }
129         
130         if( kontrakt == 0 && punkty < 20 ) {
131             this.resultText.setText("Przy czterech pasach podaj większą liczę punktów.");
132             return;
133         }
134         
135         wynik = this.liczenie.policz(kontrakt, typ, kontra, partia, punkty, lewy);
136         
137         this.resultText.setText((wynik.dla == 0 ? "Zapis dla rozgrywających" : "Zapis dla przeciwników.")+
138                 "\nPunktów (bez PC): "+wynik.punkty_przed+
139                 "\nPunktów (z PC): "+wynik.punkty+
140                 "\nIMP-ów: "+wynik.IMP+
141                 "\nProcent: "+wynik.procent);
142     }
143 }