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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
| package net.jacekk.bridge;
|
| import java.util.Arrays;
|
| public class BridgeCompute {
|
| // Points for tricks (minor, major, no trump)
| protected int tricks[] = {20, 30, 30};
|
| // 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 (from 20 up)
| protected int PC[][] = {
| {0, 50, 70, 110, 200, 300, 350, 400, 430, 460, 490, 600, 700, 900,
| 1000, 1100, 1200, 1400, 1400, 1400, 1400
| },
| {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 percentPoints[][] = {
| {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, 630, 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 percentPCs[] = {0, 6, 10, 16, 21, 25, 31, 35, 9999999};
|
| // Percent score per [i] - points scored, [j] - PCs 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 getIMPs(int points) {
| int position = Arrays.binarySearch(this.IMPs, points);
| if (position < 0) {
| return -position - 2;
| }
| return position;
| }
|
| // Finds percent score for data given (using percent* properties)
| protected int getPercent(int PC, int points, int vulnerability) {
| int positionPoints = Arrays.binarySearch(this.percentPoints[vulnerability], points);
| int positionPCs = Arrays.binarySearch(this.percentPCs, PC);
| if (positionPoints < 0) positionPoints = -positionPoints - 2;
| if (positionPCs < 0) positionPCs = -positionPCs - 2;
|
| return this.percent[positionPoints][positionPCs];
| }
|
| public void validateInput(BridgeInput input) throws BridgeInputException {
| if (input.bid < 0 || input.bid > 7) {
| throw new BridgeInputException(BridgeInputException.Error.CONTRACT_LEVEL_INVALID);
| }
|
| if (input.suit == null) {
| throw new BridgeInputException(BridgeInputException.Error.CONTRACT_SUIT_INVALID);
| }
|
| if (input.contract == null) {
| throw new BridgeInputException(BridgeInputException.Error.CONTRACT_DOUBLE_INVALID);
| }
|
| if (input.PC < 0 || input.PC > 40) {
| throw new BridgeInputException(BridgeInputException.Error.PC_INVALID);
| }
|
| if (input.tricks < 0 || input.tricks > 13) {
| throw new BridgeInputException(BridgeInputException.Error.TRICKS_INVALID);
| }
|
| if (input.bid == 0 && input.PC < 20) {
| throw new BridgeInputException(BridgeInputException.Error.PASSES_INVALID);
| }
| }
|
| public BridgeResult getResultForInput(BridgeInput input) throws BridgeInputException {
| validateInput(input);
|
| int weVulnerable = (input.weVulnerable) ? 1 : 0;
| int theyVulnerable = (input.theyVulnerable) ? 1 : 0;
| int bid = input.bid;
|
| BridgeResult result = new BridgeResult();
|
| // 4 passes
| if (input.bid == 0) {
| result.winners = BridgeResult.Side.They;
| result.points = this.PC[weVulnerable][input.PC - 20];
| result.IMPs = this.getIMPs(result.points);
| result.percent = 100 - this.getPercent(input.PC, 0, weVulnerable);
| return result;
| }
|
|
| int multiplier = input.contract.multiplier;
| int dbl = input.contract.tableIndex;
| int tricks = input.tricks;
|
| // Result in points
| int points;
| // Points for tricks bid
| int pointsForTricksBid;
| // Result minus points required, according to PC property
| int pointsForScoring;
|
| // Number of overtricks (or undertricks if negative)
| int additionalTricks = tricks - 6 - bid;
|
| // Contract made
| if (additionalTricks >= 0) {
| pointsForTricksBid = this.tricks[input.suit.tableIndex] * bid * multiplier;
| if (input.suit.equals(BridgeInput.Suit.NOTRUMP)) {
| // First trick in no trump game is worth 10 points more
| pointsForTricksBid += 10 * multiplier;
| }
| points = pointsForTricksBid;
|
| // Add points for overtricks
|
| // Ordinary contract
| if (input.contract.equals(BridgeInput.Contract.NORMAL)) {
| points += this.tricks[input.suit.tableIndex] * additionalTricks;
| }
| // Contract doubled/redoubled
| else {
| points += (!input.weVulnerable ? 50 : 100) * multiplier * additionalTricks;
|
| // Bonus for winning doubled/redoubled contract
| points += 25 * multiplier;
| }
|
| if (bid == 6) {
| // Bonus for small slam
| points += (!input.weVulnerable ? 500 : 750);
| } else if (bid == 7) {
| // Bonus for grand slam
| points += (!input.weVulnerable ? 1000 : 1500);
| }
|
| if (pointsForTricksBid >= 100) {
| // Bonus for game
| points += (!input.weVulnerable ? 300 : 500);
| } else {
| // Bonus for part-game
| points += 50;
| }
| }
| // Contract defeated
| else {
| // Points for undertricks (for defending side)
| points = -1 * this.undertricks[weVulnerable][dbl][-1 * additionalTricks];
| }
|
| // Deduct points required
| if (input.PC >= 20) {
| pointsForScoring = points - this.PC[weVulnerable][input.PC - 20];
| } else {
| pointsForScoring = points + this.PC[theyVulnerable][20 - input.PC];
| }
|
| // Score for defeaters
| if (pointsForScoring < 0) {
| result.winners = BridgeResult.Side.They;
| result.pointsBefore = -1 * points;
| result.points = -1 * pointsForScoring;
|
| if (points < 0) {
| result.percent = getPercent(40 - input.PC, -points, theyVulnerable);
| } else {
| result.percent = 100 - getPercent(input.PC, points, weVulnerable);
| }
| }
| // Score for declarers
| else {
| result.winners = BridgeResult.Side.We;
| result.pointsBefore = points;
| result.points = pointsForScoring;
|
| if (points > 0) {
| result.percent = getPercent(input.PC, points, weVulnerable);
| } else {
| result.percent = 100 - getPercent(40 - input.PC, -points, theyVulnerable);
| }
| }
|
| result.IMPs = getIMPs(result.points);
|
|
| return result;
| }
| }
|
|