commit | author | age
|
7a519e
|
1 |
package net.jacekk.bridge; |
JK |
2 |
|
|
3 |
public class BridgeCompute { |
|
4 |
// Points for tricks |
|
5 |
protected int tricks[][] = { |
|
6 |
{0, 20, 40, 60, 80, 100, 120, 140}, // minor suits |
|
7 |
{0, 30, 60, 90, 120, 150, 180, 210}, // major suits |
|
8 |
{0, 40, 70, 100, 130, 160, 190, 220} // no trump |
|
9 |
}; |
|
10 |
|
|
11 |
// Points for [i] undertricks |
|
12 |
protected int undertricks[][][] = { |
|
13 |
{ // non vulnerable |
|
14 |
{0, 50, 100, 150, 200, 250, 300, 350, 400, 450, 500, 550, 600, 650}, // normal |
|
15 |
{0, 100, 300, 500, 700, 900, 1100, 1300, 1500, 1700, 1900, 2100, 2300, 2500}, // double |
|
16 |
{0, 200, 600, 1000, 1400, 1800, 2200, 2600, 3000, 3400, 3800, 4200, 4600, 5000} // redouble |
|
17 |
}, |
|
18 |
{ // vulnerable |
|
19 |
{0, 100, 200, 300, 400, 500, 600, 700, 800, 900, 1000, 1100, 1200, 1300}, // normal |
|
20 |
{0, 200, 500, 800, 1100, 1400, 1700, 2000, 2300, 2600, 2900, 3200, 3500, 3800}, // double |
|
21 |
{0, 400, 1000, 1600, 2200, 2800, 3400, 4000, 4600, 5200, 5800, 6400, 7000, 7600}// redouble |
|
22 |
} |
|
23 |
}; |
|
24 |
|
|
25 |
// Points required per Milton Work Point Count |
|
26 |
protected int PC[][][] = { |
|
27 |
{ // We: non vulnerable, they: non vulnerable |
|
28 |
{-1400, -1400, -1400, -1400, -1200, -1100, -1000, -900, -700, -600, -490, |
|
29 |
-460, -430, -400, -350, -300, -200, -110, -70, -50, 0, |
|
30 |
50, 70, 110, 200, 300, 350, 400, 430, 460, 490, |
|
31 |
600, 700, 900, 1000, 1100, 1200, 1400, 1400, 1400, 1400}, |
|
32 |
// We: non vulnerable, they: vulnerable |
|
33 |
{-2100, -2100, -2100, -2100, -1800, -1650, -1500, -1350, -1050, -800, -690, |
|
34 |
-660, -630, -600, -520, -440, -290, -110, -70, -50, 0, |
|
35 |
50, 70, 110, 200, 300, 350, 400, 430, 460, 490, |
|
36 |
600, 700, 900, 1000, 1100, 1200, 1400, 1400, 1400, 1400} |
|
37 |
}, |
|
38 |
{ // We: vulnerable, they: non vulnerable |
|
39 |
{-1400, -1400, -1400, -1400, -1200, -1100, -1000, -900, -700, -600, -490, |
|
40 |
-460, -430, -400, -350, -300, -200, -110, -70, -50, 0, |
|
41 |
50, 70, 110, 290, 440, 520, 600, 630, 660, 690, |
|
42 |
800, 1050, 1350, 1500, 1650, 1800, 2100, 2100, 2100, 2100}, |
|
43 |
// We: vulnerable, they: vulnerable |
|
44 |
{-2100, -2100, -2100, -2100, -1800, -1650, -1500, -1350, -1050, -800, -690, |
|
45 |
-660, -630, -600, -520, -440, -290, -110, -70, -50, 0, |
|
46 |
50, 70, 110, 290, 440, 520, 600, 630, 660, 690, |
|
47 |
800, 1050, 1350, 1500, 1650, 1800, 2100, 2100, 2100, 2100} |
|
48 |
} |
|
49 |
}; |
|
50 |
|
|
51 |
// Points required for [i] IMPs |
|
52 |
protected int IMPs[] = { 0, 20, 50, 90, 130, 170, 220, 270, 320, 370, 430, 500, 600, 750, 900, |
|
53 |
1100, 1300, 1500, 1750, 2000, 2250, 2500, 3000, 3500, 4000, 9999999}; |
|
54 |
|
|
55 |
|
|
56 |
// percent* properties are related to scoring system by Adam Królik |
|
57 |
// See: http://www.gplewniak.republika.pl/strony/zapis.html |
|
58 |
|
|
59 |
// Points received - [i] used to access row from percent variable |
|
60 |
protected int percent_points[][] = { |
|
61 |
{0, 50, 90, 120, 150, 180, 210, 300, 400, 430, 460, 490, 520, 800, 920, 940, 980, 990, |
|
62 |
1020, 1400, 1440, 1520, 1530, 9999999}, // non vulnerable |
|
63 |
{0, 50, 90, 120, 150, 180, 210, 500, 600, 360, 660, 690, 720, 810, 1370, 1390, 1430, |
|
64 |
1440, 1470, 1700, 2000, 2220, 2230, 9999999} // vulnerable |
|
65 |
}; |
|
66 |
|
|
67 |
// Work Point Count - [i] used to access column from property percent |
|
68 |
protected int percent_PC[] = {0, 6, 10, 16, 21, 25, 31, 35, 9999999}; |
|
69 |
|
|
70 |
// Percent score per [i] - points scored, [j] - PC had |
|
71 |
protected int percent[][] = { |
|
72 |
{-1, -1, -1, 50, 44, 26, 8, 0}, |
|
73 |
{83, 74, 65, 56, 47, 29, 11, 0}, |
|
74 |
{86, 77, 68, 59, 50, 32, 14, 0}, |
|
75 |
{89, 80, 71, 62, 53, 35, 17, 0}, |
|
76 |
{92, 83, 74, 65, 56, 38, 20, 2}, |
|
77 |
{95, 86, 77, 68, 59, 41, 23, 5}, |
|
78 |
{98, 89, 80, 71, 62, 44, 26, 8}, |
|
79 |
{100, 92, 83, 74, 65, 47, 29, 11}, |
|
80 |
{100, 95, 86, 77, 68, 50, 32, 14}, |
|
81 |
{100, 98, 89, 80, 71, 53, 35, 17}, |
|
82 |
{100, 100, 92, 83, 74, 56, 38, 20}, |
|
83 |
{100, 100, 95, 86, 77, 59, 41, 23}, |
|
84 |
{100, 100, 98, 89, 80, 62, 44, 26}, |
|
85 |
{100, 100, 100, 92, 83, 65, 47, 29}, |
|
86 |
{100, 100, 100, 95, 86, 68, 50, 32}, |
|
87 |
{100, 100, 100, 98, 89, 71, 53, 35}, |
|
88 |
{100, 100, 100, 100, 92, 74, 56, 38}, |
|
89 |
{100, 100, 100, 100, 95, 77, 59, 41}, |
|
90 |
{100, 100, 100, 100, 98, 80, 62, 44}, |
|
91 |
{100, 100, 100, 100, 100, 83, 65, 47}, |
|
92 |
{100, 100, 100, 100, 100, 86, 68, 50}, |
|
93 |
{100, 100, 100, 100, 100, 89, 71, 53}, |
|
94 |
{100, 100, 100, 100, 100, 100, 95, 90} |
|
95 |
}; |
|
96 |
|
|
97 |
// Finds IMP score for points given (using IMPs property) |
|
98 |
protected int IMP(int points) { |
|
99 |
int i = 0; |
|
100 |
while (IMPs[i + 1] <= points) { |
|
101 |
i++; |
|
102 |
} |
|
103 |
return i; |
|
104 |
} |
|
105 |
|
|
106 |
// Finds percent score for data given (using percent* properties) |
|
107 |
protected int percent(int PC, int points, int vulnerability) { |
|
108 |
int points_i = 0; |
|
109 |
int PC_i = 0; |
|
110 |
|
|
111 |
while (this.percent_points[vulnerability][points_i + 1] <= points) { |
|
112 |
points_i++; |
|
113 |
} |
|
114 |
|
|
115 |
while (this.percent_PC[PC_i + 1] <= PC) { |
|
116 |
PC_i++; |
|
117 |
} |
|
118 |
|
|
119 |
return this.percent[points_i][PC_i]; |
|
120 |
} |
|
121 |
|
|
122 |
public BridgeResult compute(int bid, int color, int dbl, int vulnerability, int PC, int tricks) { |
|
123 |
BridgeResult result = new BridgeResult(); |
|
124 |
int weVulnerable = vulnerability / 2; |
|
125 |
int theyVulnerable = vulnerability % 2; |
|
126 |
|
|
127 |
// 4 passes |
|
128 |
if (bid == 0) { |
|
129 |
result.pointsFor = BridgeResult.PointsFor.Them; |
|
130 |
result.points = this.PC[weVulnerable][theyVulnerable][PC]; |
|
131 |
result.IMPs = this.IMP(result.points); |
|
132 |
result.percent = 100 - this.percent(PC, 0, weVulnerable); |
|
133 |
|
|
134 |
return result; |
|
135 |
} |
|
136 |
|
|
137 |
|
|
138 |
// Result in points |
|
139 |
int resultPoints; |
|
140 |
// Points for tricks |
|
141 |
int pointsForTricks; |
|
142 |
// Result minus points required, according to PC property |
|
143 |
int pointsForScoring; |
|
144 |
|
|
145 |
// Number of overtricks (or undertricks if negative) |
|
146 |
int additionalTricks = tricks - 6 - bid; |
|
147 |
|
|
148 |
// Double/redouble multiplier |
|
149 |
int multiplier = dbl + 1; |
|
150 |
if (multiplier == 3) { |
|
151 |
multiplier = 4; |
|
152 |
} |
|
153 |
|
|
154 |
// Contract made |
|
155 |
if (additionalTricks >= 0) { |
|
156 |
// Ordinary contract |
|
157 |
if (multiplier == 1) { |
|
158 |
pointsForTricks = this.tricks[color][bid]; |
|
159 |
resultPoints = this.tricks[color][tricks - 6]; |
|
160 |
} |
|
161 |
// Contract doubled/redoubled |
|
162 |
else { |
|
163 |
pointsForTricks = this.tricks[color][bid] * multiplier; |
|
164 |
resultPoints = pointsForTricks; |
|
165 |
|
|
166 |
// Bonus for winning doubled/redoubled contract |
|
167 |
resultPoints += 25 * multiplier; |
|
168 |
// For overtricks |
|
169 |
resultPoints += additionalTricks * (weVulnerable == 0 ? 50 : 100) * multiplier; |
|
170 |
} |
|
171 |
|
|
172 |
if (bid == 6) { |
|
173 |
// Bonus for small slam |
|
174 |
resultPoints += (weVulnerable == 0 ? 500 : 750); |
|
175 |
} else if (bid == 7) { |
|
176 |
// Bonus for grand slam |
|
177 |
resultPoints += (weVulnerable == 0 ? 1000 : 1500); |
|
178 |
} |
|
179 |
|
|
180 |
if (pointsForTricks >= 100) { |
|
181 |
// Bonus for game |
|
182 |
resultPoints += (weVulnerable == 0 ? 300 : 500); |
|
183 |
} else { |
|
184 |
// Bonus for part-game |
|
185 |
resultPoints += 50; |
|
186 |
} |
|
187 |
} |
|
188 |
// Contract defeated |
|
189 |
else { |
|
190 |
// Points for undertricks (for defending side) |
|
191 |
resultPoints = -1 * this.undertricks[weVulnerable][dbl][-1 * additionalTricks]; |
|
192 |
} |
|
193 |
|
|
194 |
// Deduct points required |
|
195 |
pointsForScoring = resultPoints - this.PC[weVulnerable][theyVulnerable][PC]; |
|
196 |
|
|
197 |
// Score for defeaters |
|
198 |
if (pointsForScoring < 0) { |
|
199 |
result.pointsFor = BridgeResult.PointsFor.Them; |
|
200 |
result.pointsBefore = -1 * resultPoints; |
|
201 |
result.points = -1 * pointsForScoring; |
|
202 |
result.IMPs = IMP(result.points); |
|
203 |
if (resultPoints < 0) { |
|
204 |
result.percent = percent(40 - PC, -1 * resultPoints, theyVulnerable); |
|
205 |
} else { |
|
206 |
result.percent = 100 - percent(PC, resultPoints, weVulnerable); |
|
207 |
} |
|
208 |
} |
|
209 |
// Score for declarers |
|
210 |
else { |
|
211 |
result.pointsFor = BridgeResult.PointsFor.Us; |
|
212 |
result.pointsBefore = resultPoints; |
|
213 |
result.points = pointsForScoring; |
|
214 |
result.IMPs = IMP(result.points); |
|
215 |
if (resultPoints > 0) { |
|
216 |
result.percent = percent(PC, resultPoints, weVulnerable); |
|
217 |
} else { |
|
218 |
result.percent = 100 - percent(40 - PC, -1 * resultPoints, theyVulnerable); |
|
219 |
} |
|
220 |
} |
|
221 |
|
|
222 |
return result; |
|
223 |
} |
|
224 |
} |