Obliczenia brydżowe (Android)
Jacek Kowalski
2015-01-17 7a519ea493c2c38d602dcd3045da81d4d4c0ce2c
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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
package net.jacekk.bridge;
 
public class BridgeCompute {
    // Points for tricks
    protected int tricks[][] = {
            {0, 20, 40, 60, 80, 100, 120, 140},  // minor suits
            {0, 30, 60, 90, 120, 150, 180, 210}, // major suits
            {0, 40, 70, 100, 130, 160, 190, 220} // no trump
    };
 
    // Points for [i] undertricks
    protected int undertricks[][][] = {
            {   // non vulnerable
                    {0, 50, 100, 150, 200, 250, 300, 350, 400, 450, 500, 550, 600, 650},            // normal
                    {0, 100, 300, 500, 700, 900, 1100, 1300, 1500, 1700, 1900, 2100, 2300, 2500},   // double
                    {0, 200, 600, 1000, 1400, 1800, 2200, 2600, 3000, 3400, 3800, 4200, 4600, 5000} // redouble
            },
            {   // vulnerable
                    {0, 100, 200, 300, 400, 500, 600, 700, 800, 900, 1000, 1100, 1200, 1300},       // normal
                    {0, 200, 500, 800, 1100, 1400, 1700, 2000, 2300, 2600, 2900, 3200, 3500, 3800}, // double
                    {0, 400, 1000, 1600, 2200, 2800, 3400, 4000, 4600, 5200, 5800, 6400, 7000, 7600}// redouble
            }
    };
 
    // Points required per Milton Work Point Count
    protected int PC[][][] = {
            {   // We: non vulnerable, they: non vulnerable
                    {-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},
                // We: non vulnerable, they: vulnerable
                    {-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}
            },
            {   // We: vulnerable, they: non vulnerable
                    {-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},
                // We: vulnerable, they: vulnerable
                    {-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}
            }
    };
 
    // Points required for [i] IMPs
    protected int IMPs[] = { 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};
 
 
    // percent* properties are related to scoring system by Adam Królik
    // See: http://www.gplewniak.republika.pl/strony/zapis.html
 
    // Points received - [i] used to access row from percent variable
    protected int percent_points[][] = {
            {0, 50, 90, 120, 150, 180, 210, 300, 400, 430, 460, 490, 520, 800, 920, 940, 980, 990,
                    1020, 1400, 1440, 1520, 1530, 9999999}, // non vulnerable
            {0, 50, 90, 120, 150, 180, 210, 500, 600, 360, 660, 690, 720, 810, 1370, 1390, 1430,
                    1440, 1470, 1700, 2000, 2220, 2230, 9999999} // vulnerable
    };
 
    // Work Point Count - [i] used to access column from property percent
    protected int percent_PC[] = {0, 6, 10, 16, 21, 25, 31, 35, 9999999};
 
    // Percent score per [i] - points scored, [j] - PC had
    protected int percent[][] = {
            {-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}
    };
 
    // Finds IMP score for points given (using IMPs property)
    protected int IMP(int points) {
        int i = 0;
        while (IMPs[i + 1] <= points) {
            i++;
        }
        return i;
    }
 
    // Finds percent score for data given (using percent* properties)
    protected int percent(int PC, int points, int vulnerability) {
        int points_i = 0;
        int PC_i = 0;
 
        while (this.percent_points[vulnerability][points_i + 1] <= points) {
            points_i++;
        }
 
        while (this.percent_PC[PC_i + 1] <= PC) {
            PC_i++;
        }
 
        return this.percent[points_i][PC_i];
    }
 
    public BridgeResult compute(int bid, int color, int dbl, int vulnerability, int PC, int tricks) {
        BridgeResult result = new BridgeResult();
        int weVulnerable = vulnerability / 2;
        int theyVulnerable = vulnerability % 2;
 
        // 4 passes
        if (bid == 0) {
            result.pointsFor = BridgeResult.PointsFor.Them;
            result.points = this.PC[weVulnerable][theyVulnerable][PC];
            result.IMPs = this.IMP(result.points);
            result.percent = 100 - this.percent(PC, 0, weVulnerable);
 
            return result;
        }
 
 
        // Result in points
        int resultPoints;
        // Points for tricks
        int pointsForTricks;
        // Result minus points required, according to PC property
        int pointsForScoring;
 
        // Number of overtricks (or undertricks if negative)
        int additionalTricks = tricks - 6 - bid;
 
        // Double/redouble multiplier
        int multiplier = dbl + 1;
        if (multiplier == 3) {
            multiplier = 4;
        }
 
        // Contract made
        if (additionalTricks >= 0) {
            // Ordinary contract
            if (multiplier == 1) {
                pointsForTricks = this.tricks[color][bid];
                resultPoints = this.tricks[color][tricks - 6];
            }
            // Contract doubled/redoubled
            else {
                pointsForTricks = this.tricks[color][bid] * multiplier;
                resultPoints = pointsForTricks;
 
                // Bonus for winning doubled/redoubled contract
                resultPoints += 25 * multiplier;
                // For overtricks
                resultPoints += additionalTricks * (weVulnerable == 0 ? 50 : 100) * multiplier;
            }
 
            if (bid == 6) {
                // Bonus for small slam
                resultPoints += (weVulnerable == 0 ? 500 : 750);
            } else if (bid == 7) {
                // Bonus for grand slam
                resultPoints += (weVulnerable == 0 ? 1000 : 1500);
            }
 
            if (pointsForTricks >= 100) {
                // Bonus for game
                resultPoints += (weVulnerable == 0 ? 300 : 500);
            } else {
                // Bonus for part-game
                resultPoints += 50;
            }
        }
        // Contract defeated
        else {
            // Points for undertricks (for defending side)
            resultPoints = -1 * this.undertricks[weVulnerable][dbl][-1 * additionalTricks];
        }
 
        // Deduct points required
        pointsForScoring = resultPoints - this.PC[weVulnerable][theyVulnerable][PC];
 
        // Score for defeaters
        if (pointsForScoring < 0) {
            result.pointsFor = BridgeResult.PointsFor.Them;
            result.pointsBefore = -1 * resultPoints;
            result.points = -1 * pointsForScoring;
            result.IMPs = IMP(result.points);
            if (resultPoints < 0) {
                result.percent = percent(40 - PC, -1 * resultPoints, theyVulnerable);
            } else {
                result.percent = 100 - percent(PC, resultPoints, weVulnerable);
            }
        }
        // Score for declarers
        else {
            result.pointsFor = BridgeResult.PointsFor.Us;
            result.pointsBefore = resultPoints;
            result.points = pointsForScoring;
            result.IMPs = IMP(result.points);
            if (resultPoints > 0) {
                result.percent = percent(PC, resultPoints, weVulnerable);
            } else {
                result.percent = 100 - percent(40 - PC, -1 * resultPoints, theyVulnerable);
            }
        }
 
        return result;
    }
}