| | |
| | | package net.jacekk.bridge.android; |
| | | |
| | | import android.app.Activity; |
| | | import android.content.Intent; |
| | | import android.os.Bundle; |
| | | import android.text.Editable; |
| | | import android.text.TextWatcher; |
| | | import android.view.View; |
| | | import android.view.Menu; |
| | | import android.view.MenuItem; |
| | | import android.widget.CompoundButton; |
| | | import android.widget.EditText; |
| | | import android.widget.SeekBar; |
| | | import android.widget.TextView; |
| | | import android.widget.ToggleButton; |
| | | |
| | | import net.jacekk.bridge.R; |
| | | import net.jacekk.bridge.BridgeCompute; |
| | | import net.jacekk.bridge.BridgeInput; |
| | | import net.jacekk.bridge.BridgeInputException; |
| | | import net.jacekk.bridge.BridgeResult; |
| | | import net.jacekk.bridge.R; |
| | | |
| | | public class MainActivity extends Activity { |
| | | private SeekBar BidSlider; |
| | |
| | | private EditText PCInput; |
| | | private EditText TricksInput; |
| | | |
| | | private TextView resultText; |
| | | private TextView ResultsText; |
| | | |
| | | public BridgeCompute bridgeCompute; |
| | | |
| | |
| | | InitializeApp(); |
| | | } |
| | | |
| | | private void InitializeApp() { |
| | | public boolean onCreateOptionsMenu(Menu menu) { |
| | | getMenuInflater().inflate(R.menu.main, menu); |
| | | return super.onCreateOptionsMenu(menu); |
| | | } |
| | | |
| | | public boolean onOptionsItemSelected(MenuItem item) { |
| | | switch (item.getItemId()) { |
| | | case R.id.main_action_about: |
| | | Intent about = new Intent(getApplicationContext(), AboutActivity.class); |
| | | startActivity(about); |
| | | return true; |
| | | case R.id.main_action_reset: |
| | | resetInputs(); |
| | | return true; |
| | | } |
| | | |
| | | return super.onOptionsItemSelected(item); |
| | | } |
| | | |
| | | private void resetInputs() { |
| | | BidSlider.setProgress(0); |
| | | MinorSuitsToggle.setChecked(true); |
| | | ContractToggle.setChecked(true); |
| | | NNToggle.setChecked(true); |
| | | PCInput.setText(""); |
| | | TricksInput.setText(""); |
| | | ResultsText.setText(getString(R.string.enter_data)); |
| | | } |
| | | |
| | | protected void InitializeApp() { |
| | | bridgeCompute = new BridgeCompute(); |
| | | |
| | | BidSlider = (SeekBar) findViewById(R.id.BidSlider); |
| | |
| | | PCInput = (EditText) findViewById(R.id.PCInput); |
| | | TricksInput = (EditText) findViewById(R.id.TricksInput); |
| | | |
| | | resultText = (TextView) findViewById(R.id.ResultsText); |
| | | ResultsText = (TextView) findViewById(R.id.ResultsText); |
| | | |
| | | CompoundButton.OnCheckedChangeListener changeListener; |
| | | |
| | | BidSlider.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() { |
| | | @Override |
| | |
| | | } |
| | | }); |
| | | |
| | | ToggleButton.OnClickListener colorClickListener = new ToggleButton.OnClickListener() { |
| | | changeListener = new CompoundButton.OnCheckedChangeListener() { |
| | | @Override |
| | | public void onClick(View view) { |
| | | ToggleButton toggle = (ToggleButton) view; |
| | | public void onCheckedChanged(CompoundButton button, boolean isChecked) { |
| | | if (!isChecked) { |
| | | if (!MinorSuitsToggle.isChecked() && !MajorSuitsToggle.isChecked() |
| | | && !NoTrumpToggle.isChecked()) { |
| | | MinorSuitsToggle.setChecked(true); |
| | | } |
| | | return; |
| | | } |
| | | |
| | | if (!toggle.equals(MinorSuitsToggle)) |
| | | if (!button.equals(MinorSuitsToggle)) |
| | | MinorSuitsToggle.setChecked(false); |
| | | if (!toggle.equals(MajorSuitsToggle)) |
| | | if (!button.equals(MajorSuitsToggle)) |
| | | MajorSuitsToggle.setChecked(false); |
| | | if (!toggle.equals(NoTrumpToggle)) |
| | | if (!button.equals(NoTrumpToggle)) |
| | | NoTrumpToggle.setChecked(false); |
| | | |
| | | updateResults(); |
| | | } |
| | | }; |
| | | |
| | | MinorSuitsToggle.setOnClickListener(colorClickListener); |
| | | MajorSuitsToggle.setOnClickListener(colorClickListener); |
| | | NoTrumpToggle.setOnClickListener(colorClickListener); |
| | | MinorSuitsToggle.setOnCheckedChangeListener(changeListener); |
| | | MajorSuitsToggle.setOnCheckedChangeListener(changeListener); |
| | | NoTrumpToggle.setOnCheckedChangeListener(changeListener); |
| | | |
| | | ToggleButton.OnClickListener bidClickListener = new ToggleButton.OnClickListener() { |
| | | changeListener = new CompoundButton.OnCheckedChangeListener() { |
| | | @Override |
| | | public void onClick(View view) { |
| | | ToggleButton toggle = (ToggleButton) view; |
| | | public void onCheckedChanged(CompoundButton button, boolean isChecked) { |
| | | if (!isChecked) { |
| | | if (!ContractToggle.isChecked() && !DoubleToggle.isChecked() |
| | | && !RedoubleToggle.isChecked()) { |
| | | ContractToggle.setChecked(true); |
| | | } |
| | | return; |
| | | } |
| | | |
| | | if (!toggle.equals(ContractToggle)) |
| | | if (!button.equals(ContractToggle)) |
| | | ContractToggle.setChecked(false); |
| | | if (!toggle.equals(DoubleToggle)) |
| | | if (!button.equals(DoubleToggle)) |
| | | DoubleToggle.setChecked(false); |
| | | if (!toggle.equals(RedoubleToggle)) |
| | | if (!button.equals(RedoubleToggle)) |
| | | RedoubleToggle.setChecked(false); |
| | | |
| | | updateResults(); |
| | | } |
| | | }; |
| | | |
| | | ContractToggle.setOnClickListener(bidClickListener); |
| | | DoubleToggle.setOnClickListener(bidClickListener); |
| | | RedoubleToggle.setOnClickListener(bidClickListener); |
| | | ContractToggle.setOnCheckedChangeListener(changeListener); |
| | | DoubleToggle.setOnCheckedChangeListener(changeListener); |
| | | RedoubleToggle.setOnCheckedChangeListener(changeListener); |
| | | |
| | | ToggleButton.OnClickListener baClickListener = new ToggleButton.OnClickListener() { |
| | | public void onClick(View view) { |
| | | ToggleButton toggle = (ToggleButton) view; |
| | | changeListener = new CompoundButton.OnCheckedChangeListener() { |
| | | @Override |
| | | public void onCheckedChanged(CompoundButton button, boolean isChecked) { |
| | | if (!isChecked) { |
| | | if (!VVToggle.isChecked() && !VNToggle.isChecked() |
| | | && !NVToggle.isChecked() && !NNToggle.isChecked()) { |
| | | NNToggle.setChecked(true); |
| | | } |
| | | return; |
| | | } |
| | | |
| | | if (!toggle.equals(VVToggle)) |
| | | VVToggle.setChecked(false); |
| | | if (!toggle.equals(VNToggle)) |
| | | VNToggle.setChecked(false); |
| | | if (!toggle.equals(NVToggle)) |
| | | NVToggle.setChecked(false); |
| | | if (!toggle.equals(NNToggle)) |
| | | if (!button.equals(NNToggle)) |
| | | NNToggle.setChecked(false); |
| | | if (!button.equals(NVToggle)) |
| | | NVToggle.setChecked(false); |
| | | if (!button.equals(VNToggle)) |
| | | VNToggle.setChecked(false); |
| | | if (!button.equals(VVToggle)) |
| | | VVToggle.setChecked(false); |
| | | |
| | | updateResults(); |
| | | } |
| | | }; |
| | | |
| | | VVToggle.setOnClickListener(baClickListener); |
| | | VNToggle.setOnClickListener(baClickListener); |
| | | NVToggle.setOnClickListener(baClickListener); |
| | | NNToggle.setOnClickListener(baClickListener); |
| | | VVToggle.setOnCheckedChangeListener(changeListener); |
| | | VNToggle.setOnCheckedChangeListener(changeListener); |
| | | NVToggle.setOnCheckedChangeListener(changeListener); |
| | | NNToggle.setOnCheckedChangeListener(changeListener); |
| | | |
| | | TextWatcher textWatcher = new TextWatcher() { |
| | | @Override |
| | |
| | | } |
| | | |
| | | public void updateResults() { |
| | | BridgeInput input = getBridgeInput(); |
| | | |
| | | BridgeResult result; |
| | | try { |
| | | result = bridgeCompute.getResultForInput(input); |
| | | } catch (BridgeInputException e) { |
| | | int errorStringId = BridgeInputExceptionMapper.map(e); |
| | | ResultsText.setText(getString(errorStringId)); |
| | | return; |
| | | } |
| | | |
| | | ResultsText.setText( |
| | | (result.winners == BridgeResult.Side.We ? getString(R.string.result_for_us) |
| | | : getString(R.string.result_for_them)) + |
| | | "\n" + getString(R.string.result_pts_wo_pc) + " " + result.pointsBefore + |
| | | "\n" + getString(R.string.result_pts) + " " + result.points + |
| | | "\n" + getString(R.string.result_imps) + " " + result.IMPs + |
| | | "\n" + getString(R.string.result_percent) + " " + result.percent); |
| | | } |
| | | |
| | | private BridgeInput getBridgeInput() { |
| | | int bid = BidSlider.getProgress(); |
| | | |
| | | int color = 0; |
| | |
| | | } catch (NumberFormatException ignored) { |
| | | } |
| | | |
| | | if (bid < 0 || bid > 7) { |
| | | resultText.setText(getString(R.string.invalid_bid)); |
| | | return; |
| | | } |
| | | |
| | | if (color != 0 && color != 1 && color != 2) { |
| | | resultText.setText(getString(R.string.invalid_color)); |
| | | return; |
| | | } |
| | | |
| | | if (dbl < 0 || dbl > 2) { |
| | | resultText.setText(getString(R.string.invalid_dbl)); |
| | | return; |
| | | } |
| | | |
| | | if (vulnerability < 0 || vulnerability > 3) { |
| | | resultText.setText(getString(R.string.invalid_baba)); |
| | | return; |
| | | } |
| | | |
| | | if (PC < 0 || PC > 40) { |
| | | resultText.setText(getString(R.string.invalid_pc)); |
| | | return; |
| | | } |
| | | |
| | | if (tricks < 0 || tricks > 13) { |
| | | resultText.setText(getString(R.string.invalid_tricks)); |
| | | return; |
| | | } |
| | | |
| | | if (bid == 0 && PC < 20) { |
| | | resultText.setText(getString(R.string.invalid_pass)); |
| | | return; |
| | | } |
| | | |
| | | BridgeResult result = bridgeCompute.compute(bid, color, dbl, vulnerability, PC, tricks); |
| | | resultText.setText( |
| | | (result.pointsFor == BridgeResult.PointsFor.Us ? getString(R.string.result_for_us) : getString(R.string.result_for_them)) + |
| | | "\n" + getString(R.string.result_pts_wo_pc) + " " + result.pointsBefore + |
| | | "\n" + getString(R.string.result_pts) + " " + result.points + |
| | | "\n" + getString(R.string.result_imps) + " " + result.IMPs + |
| | | "\n" + getString(R.string.result_percent) + " " + result.percent); |
| | | BridgeInput input = new BridgeInput(); |
| | | input.bid = bid; |
| | | input.suit = BridgeInput.Suit.values()[color]; |
| | | input.contract = BridgeInput.Contract.values()[dbl]; |
| | | input.weVulnerable = vulnerability / 2 >= 1; |
| | | input.theyVulnerable = vulnerability % 2 >= 1; |
| | | input.PC = PC; |
| | | input.tricks = tricks; |
| | | return input; |
| | | } |
| | | } |