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