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