-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathskinnyRank.js
346 lines (346 loc) · 11.6 KB
/
skinnyRank.js
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
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
function initCards() {
const shorts = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz'.split('');
const ranks = '0123456789JQK'.split('');
const suits = 'cdhs'.split('');
let shortToRank = new Map([]);
let shortToSuit = new Map([]);
let i = 0;
for (let suit of suits) {
for (let rank of ranks) {
const short = shorts[i++];
shortToRank.set(short, rank);
shortToSuit.set(short, suit);
}
}
return { shorts, suits, ranks };
}
exports.initCards = initCards;
const { shorts, suits, ranks } = initCards();
const loACode = 'a'.charCodeAt(0);
/**
* Convert a short (character) to a number that could index into `short` to get a suit-mate.
* Old trick: convert to lowercase by `(x | 0b100000)`, i.e., turn on the 6th last significant bit.
* @param short string in `shorts`
* @returns number between 0 and 12 inclusive: 0 ace, 1 for deuce, 12 for king.
*/
function shortToNumber(short) { return ((short.charCodeAt(0) | 0b100000) - loACode) % 13; }
exports.shortToNumber = shortToNumber;
const ACEHIRANK = 13;
// (0 to 12) || 13 means aces get mapped to 13.
/**
* Maps a number between 0 and 12 (inclusive) to the natural rank.
* @param num number between 0 and 12 inclusive
* @returns number between 2 and 14 inclusive. 2 deuce, 3 three, 13 king, 14 ace.
*/
function numberToNumberAcesHigh(num) { return (num || ACEHIRANK) + 1; }
exports.numberToNumberAcesHigh = numberToNumberAcesHigh;
function shortToNumberAcesHigh(short) { return numberToNumberAcesHigh(shortToNumber(short)); }
exports.shortToNumberAcesHigh = shortToNumberAcesHigh;
function shortsToBestNumberAcesHighArr(shorts) {
return Math.max(...shorts.map(shortToNumberAcesHigh));
}
function shortsToBestNumberAcesHighStr(shorts) {
return shortsToBestNumberAcesHighArr(shorts.split(''));
}
function numberAcesHighToNumber(n) { return n === (ACEHIRANK + 1) ? 0 : n - 1; }
function shortToRank(short) { return ranks[shortToNumber(short)]; }
function shortToSuit(short) {
return ((short < 'N') && 'c') || ((short <= 'Z') && 'd') || ((short < 'n') && 'h') || 's';
}
function validateShort(short) { return /^[a-zA-Z]$/.test(short); }
exports.validateShort = validateShort;
var parseRank = (s) => {
let res = parseInt(s.replace('A', '1'));
return isNaN(res) ? s : '' + (res - 1);
};
// Not intended for speed!
function readableToShort(raw) {
const rank = parseRank(raw.slice(0, -1));
const suit = raw.slice(-1);
let rankIdx = ranks.indexOf(rank);
let suitIdx = suits.indexOf(suit);
if (rankIdx === -1 || suitIdx === -1) {
throw new Error('bad readable: ' + [[rank, rankIdx], [suit, suitIdx]]);
}
return shorts[suitIdx * 13 + rankIdx];
}
exports.readableToShort = readableToShort;
function shortToReadable(short) {
let suit = shortToSuit(short);
let num = shortToNumberAcesHigh(short);
let rank = ((num <= 10) && num.toString()) || (num === 11 && 'J') || (num === 12 && 'Q') || (num === 13 && 'K') || 'A';
return (rank.length === 1 ? ' ' : '') + rank + suit;
}
exports.shortToReadable = shortToReadable;
function sortString(s) { return s.split('').sort().join(''); }
exports.sortString = sortString;
function search(haystack, needle) {
const nhay = haystack.length;
const nnee = needle.length;
let targetIdx = 0;
if (nnee > nhay) {
return false;
}
for (let c of haystack) {
if (c > needle[targetIdx]) {
return false;
}
else if (c === needle[targetIdx]) {
if (++targetIdx === nnee) {
return true;
}
}
}
return false;
}
exports.search = search;
// 1: royal flushes
function isRoyalFlush(hand) {
if (hand.length < 5) {
return 0;
}
return ((hand.startsWith('A') && hand.includes('JKLM')) || (hand.includes('jklm') && hand.includes('a')) ||
(hand.includes('WXYZ') && hand.includes('N')) || (hand.endsWith('wxyz') && hand.includes('n')))
? 1
: 0;
}
exports.isRoyalFlush = isRoyalFlush;
// 2: straight flushes
function bestStraightFlush(hand) {
if (hand.length < 5) {
return 0;
}
let straightFlushesFound = [];
let nhits = 0;
let prevCharCode = hand.charCodeAt(0);
for (let i = 1; i < hand.length; i++) {
let newCharCode = hand.charCodeAt(i);
if (prevCharCode + 1 === newCharCode) {
if ((++nhits) >= 4 && (shortToSuit(hand[i]) === shortToSuit(hand[i - 4]))) {
straightFlushesFound.push(hand[i]);
}
}
else {
nhits = 0;
}
prevCharCode = newCharCode;
}
if (straightFlushesFound.length > 0) {
return Math.max(...straightFlushesFound.map(shortToNumberAcesHigh));
}
return 0;
}
exports.bestStraightFlush = bestStraightFlush;
function memoize(hand) {
let cardsPerRank = Array.from(Array(13), _ => 0);
for (let short of hand) {
cardsPerRank[shortToNumber(short)]++;
}
return { cardsPerRank };
}
function sweep(hand, memo) {
if (memo.cardsPerRank.length === 0) {
return sweep(hand, memoize(hand));
}
let quad = 0, trip = 0, trip2 = 0, pair = 0, pair2 = 0;
for (let i = 13; i > 0; i--) {
const hits = memo.cardsPerRank[i % 13];
quad = quad || (hits === 4 && numberToNumberAcesHigh(i % 13)) || 0;
if (trip) {
trip2 = trip2 || (hits === 3 && numberToNumberAcesHigh(i % 13)) || 0;
}
if (pair) {
pair2 = pair2 || (hits === 2 && numberToNumberAcesHigh(i % 13)) || 0;
}
trip = trip || (hits === 3 && numberToNumberAcesHigh(i % 13)) || 0;
pair = pair || (hits === 2 && numberToNumberAcesHigh(i % 13)) || 0;
}
return [quad, trip, trip2, pair, pair2];
}
function appendKickers(hand, memo, nCardsFound, acesLow) {
if (!memo.cardsPerRank.length) {
return appendKickers(hand, memoize(hand), nCardsFound, acesLow);
}
let kickersNeeded = 5 - nCardsFound;
for (let i = 13; i > 0; i--) {
const howMany = memo.cardsPerRank[i % 13];
if (howMany === 0 || acesLow.indexOf(i % 13) >= 0) {
continue;
}
const number = i % 13;
for (let j = 0; j < howMany && kickersNeeded > 0; j++, kickersNeeded--) {
acesLow.push(number);
}
if (kickersNeeded <= 0) {
return acesLow.map(numberToNumberAcesHigh);
}
}
return acesLow.map(numberToNumberAcesHigh).concat(Array.from(Array(kickersNeeded), _ => 0));
}
function fastScore(hand) {
// const rf = 1, sf = 2, quad = 3, fh = 4, fl = 5, str = 6, trip = 7, twop = 8, pair = 9, hic = 10;
if (isRoyalFlush(hand)) {
return 1;
}
if (bestStraightFlush(hand)) {
return 2;
}
let memo = memoize(hand);
let [quad, trip, trip2, pair, pair2] = sweep(hand, memo);
if (quad) {
return 3;
}
if (trip && (pair || trip2)) {
return 4;
}
if (bestFlushUnsafe(hand)[0]) {
return 5;
}
if (bestStraight(hand, memo)) {
return 6;
}
if (trip) {
return 7;
}
if (pair && pair2) {
return 8;
}
if (pair) {
return 9;
}
return 10;
}
exports.fastScore = fastScore;
function score(hand) {
if (isRoyalFlush(hand)) {
return { score: 1, output: [1] };
}
const sf = bestStraightFlush(hand);
if (sf) {
return { score: 2, output: [sf] };
}
let memo = memoize(hand);
let [quad, trip, trip2, pair, pair2] = sweep(hand, memo);
if (quad) {
return { score: 3, output: appendKickers(hand, memo, 4, [numberAcesHighToNumber(quad)]) };
}
if (trip && trip2) {
return { score: 4, output: [trip, trip2] };
}
else if (trip && pair) {
return { score: 4, output: [trip, pair] };
}
const flush = bestFlushUnsafe(hand);
if (flush[0] > 0) {
return { score: 5, output: flush };
}
const str = bestStraight(hand, memo);
if (str) {
return { score: 6, output: [str] };
}
if (trip) {
return { score: 7, output: appendKickers(hand, memo, 3, [numberAcesHighToNumber(trip)]) };
}
if (pair && pair2) {
return {
score: 8,
output: appendKickers(hand, memo, 4, [numberAcesHighToNumber(pair), numberAcesHighToNumber(pair2)])
};
}
if (pair) {
return { score: 9, output: appendKickers(hand, memo, 2, [numberAcesHighToNumber(pair)]) };
}
return { score: 10, output: appendKickers(hand, memo, 0, []) };
}
exports.score = score;
function compareHands(a, b) {
let { score: ascore, output: aout } = score(a);
let { score: bscore, output: bout } = score(b);
if (ascore !== bscore) {
return ascore - bscore;
}
// tie-breakers
if (aout.length !== bout.length) {
throw new Error('cannot compare hands of unequal size');
}
let ret = aout.findIndex((a, i) => bout[i] !== a);
if (ret === -1) {
return 0;
}
return bout[ret] - aout[ret];
}
exports.compareHands = compareHands;
function bestStraight(hand, memo) {
if (!memo.cardsPerRank.length) {
return bestStraight(hand, memoize(hand));
}
let stringSoFar = 0;
let prevHits = memo.cardsPerRank[0];
// start at king, prev = ace hits
for (let i = 12; i >= 0; i--) {
if (memo.cardsPerRank[i] > 0 && prevHits > 0) {
stringSoFar++;
if (stringSoFar >= 4) {
return numberToNumberAcesHigh(i + stringSoFar);
}
}
else {
stringSoFar = 0;
}
prevHits = memo.cardsPerRank[i];
}
return 0;
}
function handToSubSuits(hand) {
let ret = [];
let curr = 0;
let start = 0;
for (let max of 'MZmz') {
for (let i = start; i <= hand.length; i++) {
if (hand[i] > max || i === hand.length) {
if (curr - start >= 5) {
ret.push(hand.substring(start, curr));
}
start = curr;
break;
}
curr++;
}
}
return ret;
}
function bestFlushUnsafe(hand) {
let suits = handToSubSuits(hand);
if (suits.length === 0) {
return [0, 0, 0, 0, 0];
}
// if more than one flush is found, they have to be sorted element-wise, and I can't be having with that.
// FIXME
if (suits.length > 1) {
throw new Error('requires safe flush finder');
}
if (shortToNumber(suits[0][0]) === 0) {
return [14].concat(suits[0].slice(-4).split('').reverse().map(shortToNumberAcesHigh));
}
return suits[0].slice(-5).split('').reverse().map(shortToNumberAcesHigh);
}
const comb_1 = require("./comb");
if (require.main === module) {
const { writeFileSync } = require('fs');
const totalCards = 7;
for (const ncards of [2, 3]) {
const initialToHistogram = {};
for (const initial of comb_1.combinations(shorts, ncards)) {
const remainingCards = shorts.filter(c => !initial.includes(c));
const histogram = Array.from(Array(11), _ => 0); // why 11? Since fastScore returns 1 to 10, make this one longer
for (const rest of comb_1.combinations(remainingCards, totalCards - ncards)) {
histogram[fastScore(initial.concat(rest).sort().join(''))]++;
}
initialToHistogram[initial.join('')] = histogram.slice(1); // remove initial element because it'll be empty
console.log(`["${initial.join('')}",[${histogram.slice(1).join(',')}]]`);
}
writeFileSync(`map-r-${totalCards}-n-${ncards}.json`, JSON.stringify(initialToHistogram));
}
}