| | |
| | | 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.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(); |
| | | } |
| | | |
| | | @Override |
| | | public void onBackPressed() { |
| | | // Reset |
| | | 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)); |
| | | } |
| | | |
| | | private void InitializeApp() { |
| | | 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; |
| | | |
| | |
| | | } |
| | | |
| | | 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; |
| | | } |
| | | } |