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