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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
|
<?php
require_once('OsikaParser.php');
class OsikaEvaluator {
private $_hand;
public function __construct($hand = NULL) {
if ($hand) {
$this->setHand($hand);
}
}
public function setHand($hand) {
$parser = new OsikaParser($hand);
$this->_hand = $parser->parse();
}
private static $_honorCounts = array();
private function _countHonors($suit, $major = FALSE) {
if (!isset(self::$_honorCounts[$suit])) {
self::$_honorCounts[$suit] = array(NULL, NULL);
}
if (self::$_honorCounts[$suit][$major] === NULL) {
self::$_honorCounts[$suit][$major] = preg_match_all($major ? '/a|k|q/' : '/a|k|q|j/', $suit);
}
return self::$_honorCounts[$suit][$major];
}
private static $_cardHonorTricks = array(
'a' => 1.125,
'k' => 0.8125,
'q' => 0.4375,
'j' => 0.125
);
private function _honorTricks($suit) {
// only 3 highest cards in the suit holding count towards honor tricks
$suit = substr($suit, 0, 3);
$cards = self::$_cardHonorTricks;
// only count Jack if it's the highest card in suit
if (strpos($suit, 'j') !== 0) {
$cards = array_slice($cards, 0, 3);
}
$ret = 0;
foreach ($cards as $card => $value) {
$ret += substr_count($suit, $card) * $value;
}
return $ret;
}
private function _honorTrickCorrections($suit) {
// only 3 highest cards in the suit holding count towards honor tricks
$suit = substr($suit, 0, 3);
$count = $this->_countHonors($suit);
// HH = 1/4 trick; HHH = 1/2 trick
return ($count < 2) ? 0 : $count * 0.25;
}
private function _honorTrickSupportCorrections($suit) {
$ret = 0;
// every 10 within 4+ suit with any of the AKQ = 1/8 of a trick
if (strlen($suit) > 3 && strpos($suit, 't') !== FALSE && $this->_countHonors($suit, TRUE)) {
$ret += 0.125;
}
// every 109 = 1/16 of a trick
if (strpos($suit, 't9') !== FALSE) {
$ret += 0.0625;
}
return $ret;
}
private function _honorTrickShortCorrections($suit) {
$length = strlen($suit);
// either nothing to subtract from or no need to
if (!$length || $length > 2) {
return 0;
}
$count = $this->_countHonors($suit);
if (!$count) {
return 0;
}
// H sec = -1/8 of a trick; Hx = -1/16 of a trick; HH sec = -1/8 of a trick
return $count * (($length === 1) ? -0.125 : -0.0625);
}
private static $_lengthDistributionTricks = array(
4 => 0.4375,
5 => 1.5,
6 => 2.75,
7 => 3.9375
);
private function _distributionTricks($suit) {
$length = strlen($suit);
if ($length < 4) {
return 0;
}
if ($length >= 8) {
return $length - 3;
}
return self::$_lengthDistributionTricks[$length];
}
private function _quickTricks($hand) {
$hand = '|'.implode('|', $hand);
// aces and kings contribute towards quick tricks
$highCards = substr_count($hand, 'a') + substr_count($hand, 'k');
// queens and jacks contribute against, but we don't count unsupported jacks, because of how they count towards honor tricks in the first place
$lowCards = substr_count($hand, 'q') + substr_count($hand, 'j') - substr_count($hand, '|j');
$difference = $highCards - $lowCards;
// difference of:
// +3 or more = 1/8
// +2 = 1/16
// +1 to -1 = 0
// -2 = -1/16
// -3 or less = -1/8
if (abs($difference) <= 1) {
return 0;
}
if ($difference > 2) {
return 0.125;
}
if ($difference > 1) {
return 0.0625;
}
if ($difference < -2) {
return -0.125;
}
if ($difference < -1) {
return -0.0625;
}
}
private function _middleCardCorrections($hand) {
// count only 10's and 9's in 3+ card suits
$nonshort = '';
foreach ($hand as $suit) {
if (strlen($suit) >= 3) {
$nonshort .= $suit;
}
}
// statistically, we should have 2 of those
// but we're not counting short suits and some long suit 10 configurations
// so the par for the course is 1 middle card
$count = preg_match_all('/t|9/', $nonshort) - 1;
// if we're better than that single middle card -> 1/16 of a trick
// if we're worse -> -1/16
return max(-0.0625, min(0.0625, $count * 0.0625));
}
private function _shortSuitCorrections($distribution) {
$shortSuits = array();
foreach ($distribution as $suit) {
// short suit is a 3- card suit here
if ($suit <= 3) {
$shortSuits[] = $suit;
}
}
// the correction only applies if we're having 2 or more short suits
if (count($shortSuits) < 2) {
return 0;
}
sort($shortSuits);
// if the shortest short suits are 3-0, 3-1 or 2-0, we add 1/16 of a trick
$diff = $shortSuits[1] - $shortSuits[0];
return ($diff > 1) ? 0.0625 : 0;
}
private function _majorSuitCorrections($distribution) {
// at least 8 cards in majors...
if ($distribution['s'] + $distribution['h'] >= 8) {
// ...and at least 3 cards in each major...
if ($distribution['h'] > 2 && $distribution['h'] > 2) {
// ...constitute a 1/16 of a trick correction
return 0.0625;
}
}
return 0;
}
// I honestly have no idea what the hell's going on below.
private function _localizationCorrections($result, $distribution) {
$strength = array();
$length = array();
foreach ($result['lh'] as $index => $value) {
if (strlen($index) === 1) {
if ($distribution[$index] >= 3) {
if (isset($strength[$distribution[$index]])) {
$strength[$distribution[$index]] += ($result['lh'][$index]+$result['lh_plus'][$index]+$result['lh_10'][$index]+$result['lh_short'][$index]);
}
else {
$strength[$distribution[$index]] = ($result['lh'][$index]+$result['lh_plus'][$index]+$result['lh_10'][$index]+$result['lh_short'][$index]);
}
if (isset($length[$distribution[$index]])) {
$length[$distribution[$index]] += $distribution[$index];
}
else {
$length[$distribution[$index]] = $distribution[$index];
}
}
}
}
ksort($strength);
ksort($length);
$sumLength = array_sum($length);
$sumStrength = array_sum($strength);
$longestDiff = end($strength)-$sumStrength*end($length)/$sumLength;
$shortestDiff = reset($strength)-$sumStrength*reset($length)/$sumLength;
if (abs($longestDiff) > 0.5) {
if (abs($longestDiff) > 1) {
return 0.25*(abs($longestDiff)/$longestDiff);
}
else {
return 0.125*(abs($longestDiff)/$longestDiff);
}
}
if (abs($shortestDiff) > 0.5) {
if (abs($shortestDiff) > 1) {
return -0.125*(abs($shortestDiff)/$shortestDiff);
}
else {
return -0.0625*(abs($shortestDiff)/$shortestDiff);
}
}
return 0;
}
private static $_suits = ['s','h','d','c'];
public function evaluate() {
$result = array();
$result['lh'] = array();
$result['lh_plus'] = array();
$result['lh_10'] = array();
$result['lh_short'] = array();
$result['lu'] = array();
foreach ($this->_hand as $ind => $suit) {
$suitChar = self::$_suits[$ind];
$result['lh'][$suitChar] = $this->_honorTricks($suit);
$result['lh_plus'][$suitChar] = $this->_honorTrickCorrections($suit);
$result['lh_10'][$suitChar] = $this->_honorTrickSupportCorrections($suit);
$result['lh_short'][$suitChar] = $this->_honorTrickShortCorrections($suit);
$result['lu'][$suitChar] = $this->_distributionTricks($suit);
}
$result['lh']['total'] = array_sum($result['lh']);
$result['lh_plus']['total'] = array_sum($result['lh_plus']);
$result['lh_10']['total'] = array_sum($result['lh_10']);
$result['lh_short']['total'] = array_sum($result['lh_short']);
$result['lu']['total'] = array_sum($result['lu']);
$result['lsz'] = array('total' => $this->_quickTricks($this->_hand));
$result['lu_plus'] = array('total' => $this->_middleCardCorrections($this->_hand));
$distribution = array_combine(self::$_suits, array_map('strlen', $this->_hand));
$result['short_suit'] = array('total' => $this->_shortSuitCorrections($distribution));
$result['major_suit'] = array('total' => $this->_majorSuitCorrections($distribution));
$result['l10n'] = array('total' => $this->_localizationCorrections($result, $distribution));
$subtotal = array();
$lhSubtotal = array();
$total = 0;
foreach ($result as $category => $factor) {
if (count($factor) === 5) {
foreach ($factor as $index => $subt) {
if (!isset($subtotal[$index])) {
$subtotal[$index] = 0;
$lhSubtotal[$index] = 0;
}
$subtotal[$index] += $subt;
if ($category !== 'lu') {
$lhSubtotal[$index] += $subt;
}
}
}
$total += $factor['total'];
}
$result['lh_subtotal'] = $lhSubtotal;
$result['subtotal'] = $subtotal;
$result['total'] = array('total' => $total);
return $result;
}
}
?>
|