Allow to reset application state using "back" button & fix toggles state detection.
5 files added
1 files modified
New file |
| | |
| | | package net.jacekk.bridge; |
| | | |
| | | /** |
| | | * Created by jacek on 18.01.15. |
| | | */ |
| | | public class BridgeComputeTest { |
| | | } |
New file |
| | |
| | | /* |
| | | * Copyright (c) 2012, Jacek Kowalski (http://jacekk.info) |
| | | * Wszystkie prawa zastrzeżone |
| | | * |
| | | * Licencja w pliku BSD-3.txt |
| | | */ |
| | | |
| | | package net.jacekk.bridge; |
| | | |
| | | public class BrydzLicz { |
| | | protected int lewy[][] = { |
| | | {0, 20, 40, 60, 80, 100, 120, 140}, // młodsze |
| | | {0, 30, 60, 90, 120, 150, 180, 210}, // starsze |
| | | {0, 40, 70, 100, 130, 160, 190, 220} // bez atu |
| | | }; |
| | | |
| | | protected int wpadki[][][] = { |
| | | { // przed partią |
| | | {0, 50, 100, 150, 200, 250, 300, 350, 400, 450, 500, 550, 600, 650}, // normalnie |
| | | {0, 100, 300, 500, 700, 900, 1100, 1300, 1500, 1700, 1900, 2100, 2300, 2500}, // kontra |
| | | {0, 200, 600, 1000, 1400, 1800, 2200, 2600, 3000, 3400, 3800, 4200, 4600, 5000} // rekontra |
| | | }, |
| | | { // po partii |
| | | {0, 100, 200, 300, 400, 500, 600, 700, 800, 900, 1000, 1100, 1200, 1300}, // normalnie |
| | | {0, 200, 500, 800, 1100, 1400, 1700, 2000, 2300, 2600, 2900, 3200, 3500, 3800}, // kontra |
| | | {0, 400, 1000, 1600, 2200, 2800, 3400, 4000, 4600, 5200, 5800, 6400, 7000, 7600} // rekontra |
| | | } |
| | | }; |
| | | |
| | | protected int PC[][][] = { |
| | | { // MY: przed, WY: przed partią |
| | | {-1400, -1400, -1400, -1400, -1200, -1100, -1000, -900, -700, -600, -490, -460, -430, -400, -350, -300, -200, -110, -70, -50, 0, 50, 70, 110, 200, 300, 350, 400, 430, 460, 490, 600, 700, 900, 1000, 1100, 1200, 1400, 1400, 1400, 1400}, |
| | | // MY: przed, WY: po partii |
| | | {-2100, -2100, -2100, -2100, -1800, -1650, -1500, -1350, -1050, -800, -690, -660, -630, -600, -520, -440, -290, -110, -70, -50, 0, 50, 70, 110, 200, 300, 350, 400, 430, 460, 490, 600, 700, 900, 1000, 1100, 1200, 1400, 1400, 1400, 1400} |
| | | }, |
| | | { // MY: po, WY: przed partią |
| | | {-1400, -1400, -1400, -1400, -1200, -1100, -1000, -900, -700, -600, -490, -460, -430, -400, -350, -300, -200, -110, -70, -50, 0, 50, 70, 110, 290, 440, 520, 600, 630, 660, 690, 800, 1050, 1350, 1500, 1650, 1800, 2100, 2100, 2100, 2100}, |
| | | // MY: po, WY: po partii |
| | | {-2100, -2100, -2100, -2100, -1800, -1650, -1500, -1350, -1050, -800, -690, -660, -630, -600, -520, -440, -290, -110, -70, -50, 0, 50, 70, 110, 290, 440, 520, 600, 630, 660, 690, 800, 1050, 1350, 1500, 1650, 1800, 2100, 2100, 2100, 2100} |
| | | } |
| | | }; |
| | | |
| | | protected int IMP[] = {0, 20, 50, 90, 130, 170, 220, 270, 320, 370, 430, 500, 600, 750, 900, 1100, 1300, 1500, 1750, 2000, 2250, 2500, 3000, 3500, 4000, 9999999}; |
| | | |
| | | protected int procenty_punkty[][] = { |
| | | {0, 50, 90, 120, 150, 180, 210, 300, 400, 430, 460, 490, 520, 800, 920, 940, 980, 990, 1020, 1400, 1440, 1520, 1530, 9999999}, // przed |
| | | {0, 50, 90, 120, 150, 180, 210, 500, 600, 360, 660, 690, 720, 810, 1370, 1390, 1430, 1440, 1470, 1700, 2000, 2220, 2230, 9999999} //po |
| | | }; |
| | | |
| | | protected int procenty_PC[] = {0, 6, 10, 16, 21, 25, 31, 35, 9999999}; |
| | | |
| | | protected int procenty[][] = { |
| | | {-1, -1, -1, 50, 44, 26, 8, 0}, |
| | | {83, 74, 65, 56, 47, 29, 11, 0}, |
| | | {86, 77, 68, 59, 50, 32, 14, 0}, |
| | | {89, 80, 71, 62, 53, 35, 17, 0}, |
| | | {92, 83, 74, 65, 56, 38, 20, 2}, |
| | | {95, 86, 77, 68, 59, 41, 23, 5}, |
| | | {98, 89, 80, 71, 62, 44, 26, 8}, |
| | | {100, 92, 83, 74, 65, 47, 29, 11}, |
| | | {100, 95, 86, 77, 68, 50, 32, 14}, |
| | | {100, 98, 89, 80, 71, 53, 35, 17}, |
| | | {100, 100, 92, 83, 74, 56, 38, 20}, |
| | | {100, 100, 95, 86, 77, 59, 41, 23}, |
| | | {100, 100, 98, 89, 80, 62, 44, 26}, |
| | | {100, 100, 100, 92, 83, 65, 47, 29}, |
| | | {100, 100, 100, 95, 86, 68, 50, 32}, |
| | | {100, 100, 100, 98, 89, 71, 53, 35}, |
| | | {100, 100, 100, 100, 92, 74, 56, 38}, |
| | | {100, 100, 100, 100, 95, 77, 59, 41}, |
| | | {100, 100, 100, 100, 98, 80, 62, 44}, |
| | | {100, 100, 100, 100, 100, 83, 65, 47}, |
| | | {100, 100, 100, 100, 100, 86, 68, 50}, |
| | | {100, 100, 100, 100, 100, 89, 71, 53}, |
| | | {100, 100, 100, 100, 100, 100, 95, 90} |
| | | }; |
| | | |
| | | protected int IMP(int punkty) { |
| | | int i = 0; |
| | | while( IMP[i+1] <= punkty ) { |
| | | i++; |
| | | } |
| | | return i; |
| | | } |
| | | |
| | | protected int procenty(int PC, int punkty, int partia) { |
| | | int punkty_i = 0; |
| | | int PC_i = 0; |
| | | |
| | | while(this.procenty_punkty[partia][punkty_i+1] <= punkty) { |
| | | punkty_i++; |
| | | } |
| | | |
| | | while(this.procenty_PC[PC_i+1] <= PC) { |
| | | PC_i++; |
| | | } |
| | | |
| | | return this.procenty[punkty_i][PC_i]; |
| | | } |
| | | |
| | | public BrydzWynik policz(int kontrakt, int kolor, int kontra, int partia, int PC, int lewy) { |
| | | BrydzWynik wynik = new BrydzWynik(); |
| | | int my_partia = partia / 2; |
| | | int wy_partia = partia % 2; |
| | | |
| | | // 4 pasy |
| | | if( kontrakt == 0 ) { |
| | | wynik.dla = 1; |
| | | wynik.punkty = this.PC[my_partia][wy_partia][PC]; |
| | | wynik.IMP = this.IMP(wynik.punkty); |
| | | wynik.procent = 100-this.procenty(PC, 0, my_partia); |
| | | |
| | | return wynik; |
| | | } |
| | | |
| | | |
| | | // Wynik w punktach |
| | | int wynik_punkty; |
| | | // Punkty za lewy kontraktowe |
| | | int wynik_lewy; |
| | | // Wynik w punktach, uwzględniając ilość PC |
| | | int wynik_punkty_PC; |
| | | |
| | | // Ilość nadróbek |
| | | int nadrobki = lewy - 6 - kontrakt; |
| | | |
| | | // Mnożnik przy kontrze |
| | | int mnoznik = kontra+1; |
| | | if( mnoznik == 3 ) { |
| | | mnoznik = 4; |
| | | } |
| | | |
| | | // Kontrakt ugrany |
| | | if( nadrobki >= 0 ) { |
| | | // "Nadróbki: jak za lewy" |
| | | if( mnoznik == 1 ) { |
| | | wynik_lewy = this.lewy[kolor][kontrakt]; |
| | | wynik_punkty = this.lewy[kolor][lewy - 6]; |
| | | } |
| | | // Kontra lub rekontra |
| | | else |
| | | { |
| | | wynik_lewy = this.lewy[kolor][kontrakt] * mnoznik; |
| | | wynik_punkty = wynik_lewy; |
| | | |
| | | // Za wygranie z kontrą |
| | | wynik_punkty += 25 * mnoznik; |
| | | // Za nadróbki |
| | | wynik_punkty += nadrobki * (my_partia == 0 ? 50 : 100) * mnoznik; |
| | | } |
| | | |
| | | if(kontrakt == 6) { |
| | | // Za szlemika |
| | | wynik_punkty += (my_partia == 0 ? 500 : 750); |
| | | } |
| | | else if(kontrakt == 7) { |
| | | // Za szlema |
| | | wynik_punkty += (my_partia == 0 ? 1000 : 1500); |
| | | } |
| | | |
| | | if(wynik_lewy >= 100) { |
| | | // Za partię |
| | | wynik_punkty += (my_partia == 0 ? 300 : 500); |
| | | } |
| | | else |
| | | { |
| | | // Za częściówkę |
| | | wynik_punkty += 50; |
| | | } |
| | | } |
| | | // Wpadka |
| | | else |
| | | { |
| | | // Punkty za wpadkę |
| | | wynik_punkty = -1 * this.wpadki[my_partia][kontra][-1 * nadrobki]; |
| | | } |
| | | |
| | | // Uwzględniamy ilość punktów, którą trzeba było zdobyć |
| | | wynik_punkty_PC = wynik_punkty - this.PC[my_partia][wy_partia][PC]; |
| | | |
| | | // Zapis dla przeciwników |
| | | if( wynik_punkty_PC < 0 ) { |
| | | wynik.dla = 1; |
| | | wynik.punkty_przed = -1 * wynik_punkty; |
| | | wynik.punkty = -1 * wynik_punkty_PC; |
| | | wynik.IMP = IMP(wynik.punkty); |
| | | if( wynik_punkty < 0 ) { |
| | | wynik.procent = procenty(40 - PC, -1 * wynik_punkty, wy_partia); |
| | | } |
| | | else |
| | | { |
| | | wynik.procent = 100 - procenty(PC, wynik_punkty, my_partia); |
| | | } |
| | | } |
| | | // Zapis dla rozgrywających |
| | | else |
| | | { |
| | | wynik.dla = 0; |
| | | wynik.punkty_przed = wynik_punkty; |
| | | wynik.punkty = wynik_punkty_PC; |
| | | wynik.IMP = IMP(wynik.punkty); |
| | | if( wynik_punkty > 0 ) { |
| | | wynik.procent = procenty(PC, wynik_punkty, my_partia); |
| | | } |
| | | else |
| | | { |
| | | wynik.procent = 100 - procenty(40 - PC, -1 * wynik_punkty, wy_partia); |
| | | } |
| | | } |
| | | |
| | | return wynik; |
| | | } |
| | | } |
New file |
| | |
| | | package net.jacekk.bridge; |
| | | |
| | | /** |
| | | * Created by jacek on 18.01.15. |
| | | */ |
| | | public class BridgeInput { |
| | | } |
New file |
| | |
| | | package net.jacekk.bridge; |
| | | |
| | | /** |
| | | * Created by jacek on 18.01.15. |
| | | */ |
| | | public class BridgeInputException { |
| | | } |
New file |
| | |
| | | package net.jacekk.bridge.android; |
| | | |
| | | /** |
| | | * Created by jacek on 18.01.15. |
| | | */ |
| | | public class BridgeInputExceptionMapper { |
| | | } |
| | |
| | | import android.os.Bundle; |
| | | import android.text.Editable; |
| | | import android.text.TextWatcher; |
| | | import android.view.View; |
| | | import android.widget.CompoundButton; |
| | | import android.widget.EditText; |
| | | import android.widget.SeekBar; |
| | | import android.widget.TextView; |
| | |
| | | InitializeApp(); |
| | | } |
| | | |
| | | @Override |
| | | public void onBackPressed() { |
| | | // Reset |
| | | BidSlider.setProgress(0); |
| | | MinorSuitsToggle.setChecked(true); |
| | | ContractToggle.setChecked(true); |
| | | NNToggle.setChecked(true); |
| | | PCInput.setText(""); |
| | | TricksInput.setText(""); |
| | | } |
| | | |
| | | private void InitializeApp() { |
| | | bridgeCompute = new BridgeCompute(); |
| | | |
| | |
| | | |
| | | resultText = (TextView) findViewById(R.id.ResultsText); |
| | | |
| | | CompoundButton.OnCheckedChangeListener changeListener; |
| | | |
| | | BidSlider.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() { |
| | | @Override |
| | | public void onProgressChanged(SeekBar seekBar, int i, boolean b) { |
| | |
| | | } |
| | | }); |
| | | |
| | | 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 |