commit | author | age
|
b20e6d
|
1 |
package net.jacekk.brydz.android; |
JK |
2 |
|
|
3 |
import android.app.Activity; |
|
4 |
import android.os.Bundle; |
|
5 |
import android.text.Editable; |
|
6 |
import android.text.TextWatcher; |
|
7 |
import android.view.View; |
|
8 |
import android.widget.EditText; |
|
9 |
import android.widget.SeekBar; |
|
10 |
import android.widget.TextView; |
|
11 |
import android.widget.ToggleButton; |
|
12 |
import net.jacekk.brydz.BridgeCompute; |
|
13 |
import net.jacekk.brydz.BridgeResult; |
|
14 |
|
|
15 |
public class MainActivity extends Activity { |
|
16 |
private SeekBar BidSlider; |
|
17 |
private TextView BidText; |
|
18 |
|
|
19 |
private ToggleButton CDToggle; |
|
20 |
private ToggleButton HSToggle; |
|
21 |
private ToggleButton NTToggle; |
|
22 |
|
|
23 |
private ToggleButton NormToggle; |
|
24 |
private ToggleButton DblToggle; |
|
25 |
private ToggleButton ReDblToggle; |
|
26 |
|
|
27 |
private ToggleButton BBToggle; |
|
28 |
private ToggleButton BAToggle; |
|
29 |
private ToggleButton ABToggle; |
|
30 |
private ToggleButton AAToggle; |
|
31 |
|
|
32 |
private EditText PCInput; |
|
33 |
private EditText TricksInput; |
|
34 |
|
|
35 |
private TextView resultText; |
|
36 |
|
|
37 |
public BridgeCompute bridgeCompute; |
|
38 |
|
|
39 |
/** |
|
40 |
* Called when the activity is first created. |
|
41 |
*/ |
|
42 |
@Override |
|
43 |
public void onCreate(Bundle savedInstanceState) { |
|
44 |
super.onCreate(savedInstanceState); |
|
45 |
setContentView(R.layout.main); |
|
46 |
|
|
47 |
InitializeApp(); |
|
48 |
} |
|
49 |
|
|
50 |
private void InitializeApp() { |
|
51 |
bridgeCompute = new BridgeCompute(); |
|
52 |
|
|
53 |
BidSlider = (SeekBar) findViewById(R.id.BidSlider); |
|
54 |
BidText = (TextView) findViewById(R.id.BidText); |
|
55 |
|
|
56 |
CDToggle = (ToggleButton) findViewById(R.id.CDToggle); |
|
57 |
HSToggle = (ToggleButton) findViewById(R.id.HSToggle); |
|
58 |
NTToggle = (ToggleButton) findViewById(R.id.NTToggle); |
|
59 |
|
|
60 |
NormToggle = (ToggleButton) findViewById(R.id.NormToggle); |
|
61 |
DblToggle = (ToggleButton) findViewById(R.id.DblToggle); |
|
62 |
ReDblToggle = (ToggleButton) findViewById(R.id.ReDblToggle); |
|
63 |
|
|
64 |
BBToggle = (ToggleButton) findViewById(R.id.BBToggle); |
|
65 |
BAToggle = (ToggleButton) findViewById(R.id.BAToggle); |
|
66 |
ABToggle = (ToggleButton) findViewById(R.id.ABToggle); |
|
67 |
AAToggle = (ToggleButton) findViewById(R.id.AAToggle); |
|
68 |
|
|
69 |
PCInput = (EditText) findViewById(R.id.PCInput); |
|
70 |
TricksInput = (EditText) findViewById(R.id.TricksInput); |
|
71 |
|
|
72 |
resultText = (TextView) findViewById(R.id.resultText); |
|
73 |
|
|
74 |
BidSlider.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() { |
|
75 |
@Override |
|
76 |
public void onProgressChanged(SeekBar seekBar, int i, boolean b) { |
|
77 |
BidText.setText(Integer.toString(seekBar.getProgress())); |
|
78 |
updateResults(); |
|
79 |
} |
|
80 |
|
|
81 |
@Override |
|
82 |
public void onStartTrackingTouch(SeekBar seekBar) { |
|
83 |
} |
|
84 |
|
|
85 |
@Override |
|
86 |
public void onStopTrackingTouch(SeekBar seekBar) { |
|
87 |
} |
|
88 |
}); |
|
89 |
|
|
90 |
ToggleButton.OnClickListener colorClickListener = new ToggleButton.OnClickListener() { |
|
91 |
@Override |
|
92 |
public void onClick(View view) { |
|
93 |
ToggleButton toggle = (ToggleButton) view; |
|
94 |
|
|
95 |
if (!toggle.equals(CDToggle)) |
|
96 |
CDToggle.setChecked(false); |
|
97 |
if (!toggle.equals(HSToggle)) |
|
98 |
HSToggle.setChecked(false); |
|
99 |
if (!toggle.equals(NTToggle)) |
|
100 |
NTToggle.setChecked(false); |
|
101 |
|
|
102 |
updateResults(); |
|
103 |
} |
|
104 |
}; |
|
105 |
|
|
106 |
CDToggle.setOnClickListener(colorClickListener); |
|
107 |
HSToggle.setOnClickListener(colorClickListener); |
|
108 |
NTToggle.setOnClickListener(colorClickListener); |
|
109 |
|
|
110 |
ToggleButton.OnClickListener bidClickListener = new ToggleButton.OnClickListener() { |
|
111 |
@Override |
|
112 |
public void onClick(View view) { |
|
113 |
ToggleButton toggle = (ToggleButton) view; |
|
114 |
|
|
115 |
if (!toggle.equals(NormToggle)) |
|
116 |
NormToggle.setChecked(false); |
|
117 |
if (!toggle.equals(DblToggle)) |
|
118 |
DblToggle.setChecked(false); |
|
119 |
if (!toggle.equals(ReDblToggle)) |
|
120 |
ReDblToggle.setChecked(false); |
|
121 |
|
|
122 |
updateResults(); |
|
123 |
} |
|
124 |
}; |
|
125 |
|
|
126 |
NormToggle.setOnClickListener(bidClickListener); |
|
127 |
DblToggle.setOnClickListener(bidClickListener); |
|
128 |
ReDblToggle.setOnClickListener(bidClickListener); |
|
129 |
|
|
130 |
ToggleButton.OnClickListener baClickListener = new ToggleButton.OnClickListener() { |
|
131 |
public void onClick(View view) { |
|
132 |
ToggleButton toggle = (ToggleButton) view; |
|
133 |
|
|
134 |
if (!toggle.equals(AAToggle)) |
|
135 |
AAToggle.setChecked(false); |
|
136 |
if (!toggle.equals(ABToggle)) |
|
137 |
ABToggle.setChecked(false); |
|
138 |
if (!toggle.equals(BAToggle)) |
|
139 |
BAToggle.setChecked(false); |
|
140 |
if (!toggle.equals(BBToggle)) |
|
141 |
BBToggle.setChecked(false); |
|
142 |
|
|
143 |
updateResults(); |
|
144 |
} |
|
145 |
}; |
|
146 |
|
|
147 |
AAToggle.setOnClickListener(baClickListener); |
|
148 |
ABToggle.setOnClickListener(baClickListener); |
|
149 |
BAToggle.setOnClickListener(baClickListener); |
|
150 |
BBToggle.setOnClickListener(baClickListener); |
|
151 |
|
|
152 |
PCInput.addTextChangedListener( |
|
153 |
new TextWatcher() { |
|
154 |
@Override |
|
155 |
public void beforeTextChanged(CharSequence charSequence, int i, int i2, int i3) { |
|
156 |
} |
|
157 |
|
|
158 |
@Override |
|
159 |
public void onTextChanged(CharSequence charSequence, int i, int i2, int i3) { |
|
160 |
} |
|
161 |
|
|
162 |
@Override |
|
163 |
public void afterTextChanged(Editable editable) { |
|
164 |
updateResults(); |
|
165 |
} |
|
166 |
} |
|
167 |
); |
|
168 |
|
|
169 |
TricksInput.addTextChangedListener( |
|
170 |
new TextWatcher() { |
|
171 |
@Override |
|
172 |
public void beforeTextChanged(CharSequence charSequence, int i, int i2, int i3) { |
|
173 |
} |
|
174 |
|
|
175 |
@Override |
|
176 |
public void onTextChanged(CharSequence charSequence, int i, int i2, int i3) { |
|
177 |
} |
|
178 |
|
|
179 |
@Override |
|
180 |
public void afterTextChanged(Editable editable) { |
|
181 |
updateResults(); |
|
182 |
} |
|
183 |
} |
|
184 |
); |
|
185 |
} |
|
186 |
|
|
187 |
public void updateResults() { |
|
188 |
int bid = BidSlider.getProgress(); |
|
189 |
|
|
190 |
int color = 0; |
|
191 |
if (HSToggle.isChecked()) |
|
192 |
color = 1; |
|
193 |
if (NTToggle.isChecked()) |
|
194 |
color = 2; |
|
195 |
|
|
196 |
int dbl = 0; |
|
197 |
if (DblToggle.isChecked()) |
|
198 |
dbl = 1; |
|
199 |
if (ReDblToggle.isChecked()) |
|
200 |
dbl = 2; |
|
201 |
|
|
202 |
int baba = 0; |
|
203 |
if (BAToggle.isChecked()) |
|
204 |
baba = 1; |
|
205 |
if (ABToggle.isChecked()) |
|
206 |
baba = 2; |
|
207 |
if (AAToggle.isChecked()) |
|
208 |
baba = 3; |
|
209 |
|
|
210 |
int PC = -1; |
|
211 |
try { |
|
212 |
PC = Integer.parseInt(PCInput.getText().toString()); |
|
213 |
} catch (NumberFormatException e) { |
|
214 |
} |
|
215 |
|
|
216 |
int tricks = -1; |
|
217 |
try { |
|
218 |
tricks = Integer.parseInt(TricksInput.getText().toString()); |
|
219 |
} catch (NumberFormatException e) { |
|
220 |
} |
|
221 |
|
|
222 |
if (bid < 0 || bid > 7) { |
|
223 |
resultText.setText(getString(R.string.invalid_bid)); |
|
224 |
return; |
|
225 |
} |
|
226 |
|
|
227 |
if (color != 0 && color != 1 && color != 2) { |
|
228 |
resultText.setText(getString(R.string.invalid_color)); |
|
229 |
return; |
|
230 |
} |
|
231 |
|
|
232 |
if (dbl < 0 || dbl > 2) { |
|
233 |
resultText.setText(getString(R.string.invalid_dbl)); |
|
234 |
return; |
|
235 |
} |
|
236 |
|
|
237 |
if (baba < 0 || baba > 3) { |
|
238 |
resultText.setText(getString(R.string.invalid_baba)); |
|
239 |
return; |
|
240 |
} |
|
241 |
|
|
242 |
if (PC < 0 || PC > 40) { |
|
243 |
resultText.setText(getString(R.string.invalid_pc)); |
|
244 |
return; |
|
245 |
} |
|
246 |
|
|
247 |
if (tricks < 0 || tricks > 13) { |
|
248 |
resultText.setText(getString(R.string.invalid_tricks)); |
|
249 |
return; |
|
250 |
} |
|
251 |
|
|
252 |
if (bid == 0 && PC < 20) { |
|
253 |
resultText.setText(getString(R.string.invalid_pass)); |
|
254 |
return; |
|
255 |
} |
|
256 |
|
|
257 |
BridgeResult result = bridgeCompute.compute(bid, color, dbl, baba, PC, tricks); |
|
258 |
resultText.setText( |
|
259 |
(result.pointsFor == BridgeResult.PointsFor.Us ? getString(R.string.result_for_us) : getString(R.string.result_for_them)) + |
|
260 |
"\n" + getString(R.string.result_pts_wo_pc) + " " + result.pointsBefore + |
|
261 |
"\n" + getString(R.string.result_pts) + " " + result.points + |
|
262 |
"\n" + getString(R.string.result_imps) + " " + result.IMPs + |
|
263 |
"\n" + getString(R.string.result_percent) + " " + result.percent); |
|
264 |
} |
|
265 |
} |