holdem-core 0.0.0-stage → 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +13 -0
- package/LICENSE +21 -0
- package/README.md +179 -2
- package/README.zh-CN.md +75 -0
- package/dist/index.cjs +679 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +275 -0
- package/dist/index.d.ts +275 -0
- package/dist/index.js +646 -0
- package/dist/index.js.map +1 -0
- package/package.json +73 -4
package/CHANGELOG.md
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
# Changelog
|
|
2
|
+
|
|
3
|
+
All notable changes to this project are documented here. The project follows [Semantic Versioning](https://semver.org/).
|
|
4
|
+
|
|
5
|
+
## 0.1.0 — first release
|
|
6
|
+
|
|
7
|
+
- Cards: parsing (`"As"`, `"10h"`, `"K♠"`), formatting, rank/suit helpers, duplicate checks.
|
|
8
|
+
- Randomness: secure source over `crypto.getRandomValues` with rejection sampling; seeded xoshiro128** source for tests and replays.
|
|
9
|
+
- Deck: unbiased Fisher–Yates `shuffle` / `shuffled`, `Deck` with exclusions.
|
|
10
|
+
- Evaluation: allocation-free `scoreHand` for 5–7 cards, `evaluateHand` with best five cards and descriptions, comparison helpers.
|
|
11
|
+
- Equity: exact enumeration up to 2,000,000 runouts, Monte Carlo beyond, board and dead cards, 2–10 players.
|
|
12
|
+
- Pots: main and side pots, folded chips, uncalled bets, odd-chip order; zero-sum settlement.
|
|
13
|
+
- Verification: all 133,784,560 seven-card hands; 1,000,000-hand cross-check against pokersolver; property tests for settlement.
|
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 HoldemForge
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,3 +1,180 @@
|
|
|
1
|
-
#
|
|
1
|
+
# holdem-core
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
**Fast, zero-dependency Texas Hold'em toolkit for TypeScript and JavaScript.**
|
|
4
|
+
Card parsing, unbiased shuffling, 5–7 card hand evaluation, exact equity and side-pot settlement — verified exhaustively.
|
|
5
|
+
|
|
6
|
+
[](https://github.com/holdemforge/holdem-core/actions/workflows/ci.yml)
|
|
7
|
+
[](https://www.npmjs.com/package/holdem-core)
|
|
8
|
+
[](LICENSE)
|
|
9
|
+

|
|
10
|
+

|
|
11
|
+
|
|
12
|
+
[中文说明](README.zh-CN.md)
|
|
13
|
+
|
|
14
|
+
```ts
|
|
15
|
+
import { parseCards, evaluateHand, calculateEquity, settlePots } from 'holdem-core';
|
|
16
|
+
|
|
17
|
+
evaluateHand(parseCards('Kh Kd Ks Tc Td 2s 3h')).description;
|
|
18
|
+
// → "Full House, Kings full of Tens"
|
|
19
|
+
|
|
20
|
+
calculateEquity([parseCards('As Ah'), parseCards('Kd Kc')]).players[0].equity;
|
|
21
|
+
// → 0.812555… (exact, all 1,712,304 runouts in ~75 ms)
|
|
22
|
+
|
|
23
|
+
settlePots(
|
|
24
|
+
[{ id: 'alice', amount: 50 }, { id: 'bob', amount: 100 }, { id: 'carol', amount: 100 }],
|
|
25
|
+
{ scores: { alice: 900, bob: 500, carol: 700 } },
|
|
26
|
+
).payouts;
|
|
27
|
+
// → { alice: 150, bob: 0, carol: 100 } (main pot to Alice, side pot to Carol)
|
|
28
|
+
```
|
|
29
|
+
|
|
30
|
+
## Why holdem-core
|
|
31
|
+
|
|
32
|
+
- **Correct, and proven.** Every one of the **133,784,560** seven-card hands is evaluated in CI and matches the published category totals. 1,000,000 random hands and 1,000,000 showdowns agree with [pokersolver](https://www.npmjs.com/package/pokersolver) with zero mismatches.
|
|
33
|
+
- **Fast.** About **20 million** 7-card evaluations per second on one core — no 21-combination loop, no allocations, no giant lookup files (≈ 24 KB of tables built at load).
|
|
34
|
+
- **Complete for real games.** Side pots, folded chips, uncalled bets and odd-chip rules are handled; every settlement is checked to be zero-sum.
|
|
35
|
+
- **Safe defaults.** Shuffles use `crypto.getRandomValues` with rejection sampling — no modulo bias. A seeded generator is available for tests and replays.
|
|
36
|
+
- **Small and typed.** Zero runtime dependencies, ESM + CommonJS, full `.d.ts`. Tested on Node 20, 22 and 24; uses only standard Web APIs, so it also runs in modern browsers.
|
|
37
|
+
|
|
38
|
+
## Install
|
|
39
|
+
|
|
40
|
+
```sh
|
|
41
|
+
npm install holdem-core
|
|
42
|
+
```
|
|
43
|
+
|
|
44
|
+
## Usage
|
|
45
|
+
|
|
46
|
+
### Cards
|
|
47
|
+
|
|
48
|
+
Cards are plain integers `0..51` (`rank * 4 + suit`) so hot loops stay allocation-free. Parse and format at the edges:
|
|
49
|
+
|
|
50
|
+
```ts
|
|
51
|
+
import { parseCard, parseCards, formatCards, rankOf, suitOf } from 'holdem-core';
|
|
52
|
+
|
|
53
|
+
parseCards('As Kd 10h Q♠'); // accepts "T" or "10", any case, suit letters or symbols
|
|
54
|
+
formatCards(parseCards('askd')); // "As Kd"
|
|
55
|
+
rankOf(parseCard('Td')); // 8 (0 = deuce … 12 = ace)
|
|
56
|
+
suitOf(parseCard('Td')); // 1 (clubs, diamonds, hearts, spades)
|
|
57
|
+
```
|
|
58
|
+
|
|
59
|
+
### Shuffling and dealing
|
|
60
|
+
|
|
61
|
+
```ts
|
|
62
|
+
import { Deck, createSeededRandom } from 'holdem-core';
|
|
63
|
+
|
|
64
|
+
const deck = new Deck(); // cryptographically secure shuffle
|
|
65
|
+
const [a, b] = [deck.deal(2), deck.deal(2)];
|
|
66
|
+
const flop = deck.deal(3);
|
|
67
|
+
|
|
68
|
+
const replay = new Deck(createSeededRandom('hand #1042')); // reproducible order — tests and replays only
|
|
69
|
+
```
|
|
70
|
+
|
|
71
|
+
`shuffle(array, random?)` and `shuffled(array, random?)` work on any array.
|
|
72
|
+
|
|
73
|
+
### Evaluating hands
|
|
74
|
+
|
|
75
|
+
```ts
|
|
76
|
+
import { scoreHand, evaluateHand, compareHands, winners, formatCards } from 'holdem-core';
|
|
77
|
+
|
|
78
|
+
const board = parseCards('Ah Kh 7c 2d 9s');
|
|
79
|
+
const alice = scoreHand([...parseCards('Qh Jh'), ...board]); // fast path: a comparable integer
|
|
80
|
+
const bob = scoreHand([...parseCards('As 9c'), ...board]);
|
|
81
|
+
winners([alice, bob]); // [1] — Bob's two pair wins
|
|
82
|
+
|
|
83
|
+
const r = evaluateHand(parseCards('As 2s 3s 4s 5s Kd Qc'));
|
|
84
|
+
r.name; // "Straight Flush"
|
|
85
|
+
r.description; // "Straight Flush, Five-high"
|
|
86
|
+
formatCards(r.cards); // "5s 4s 3s 2s As"
|
|
87
|
+
```
|
|
88
|
+
|
|
89
|
+
A `HandScore` is `category << 20 | five 4-bit ranks`: higher wins, equal splits, and there are exactly 7,462 distinct values.
|
|
90
|
+
|
|
91
|
+
### Equity
|
|
92
|
+
|
|
93
|
+
```ts
|
|
94
|
+
import { calculateEquity, parseCards, createSeededRandom } from 'holdem-core';
|
|
95
|
+
|
|
96
|
+
calculateEquity([parseCards('Ah Kh'), parseCards('Qs Qd'), parseCards('7c 8c')], {
|
|
97
|
+
board: parseCards('Qh 9h 6c'),
|
|
98
|
+
});
|
|
99
|
+
// → { exact: true, runouts: 903, players: [{ equity, win, tie }, …] }
|
|
100
|
+
|
|
101
|
+
// Too many runouts to enumerate? It switches to Monte Carlo automatically:
|
|
102
|
+
calculateEquity(hands, { iterations: 500_000, random: createSeededRandom(1) });
|
|
103
|
+
```
|
|
104
|
+
|
|
105
|
+
Options: `board`, `dead`, `exactLimit` (default 2,000,000 runouts — heads-up and 3-way preflop are exact), `iterations`, `random`.
|
|
106
|
+
|
|
107
|
+
### Side pots and settlement
|
|
108
|
+
|
|
109
|
+
```ts
|
|
110
|
+
import { buildPots, settlePots } from 'holdem-core';
|
|
111
|
+
|
|
112
|
+
buildPots([
|
|
113
|
+
{ id: 'short', amount: 30 },
|
|
114
|
+
{ id: 'mid', amount: 80 },
|
|
115
|
+
{ id: 'big', amount: 200 },
|
|
116
|
+
{ id: 'folded', amount: 20, folded: true },
|
|
117
|
+
]);
|
|
118
|
+
// → [ { amount: 110, eligible: ['short','mid','big'] },
|
|
119
|
+
// { amount: 100, eligible: ['mid','big'] },
|
|
120
|
+
// { amount: 120, eligible: ['big'] } ] ← uncalled chips go back to "big"
|
|
121
|
+
|
|
122
|
+
settlePots(contributions, {
|
|
123
|
+
scores: { short: s1, mid: s2, big: s3 }, // e.g. scoreHand(...) of each live hand
|
|
124
|
+
oddChipOrder: ['mid', 'big', 'short'], // seats starting left of the button
|
|
125
|
+
});
|
|
126
|
+
// → { pots: [...winners, shares], payouts, net } — net always sums to 0
|
|
127
|
+
```
|
|
128
|
+
|
|
129
|
+
Amounts are integers in your smallest unit. Players alone in a pot need no score.
|
|
130
|
+
|
|
131
|
+
## Verification and benchmarks
|
|
132
|
+
|
|
133
|
+
```sh
|
|
134
|
+
npm test # unit + property tests, includes all 2,598,960 five-card hands
|
|
135
|
+
npm run verify:exhaustive # all 133,784,560 seven-card hands
|
|
136
|
+
npm run verify:crosscheck # 1,000,000 hands + 1,000,000 showdowns vs pokersolver
|
|
137
|
+
npm run bench
|
|
138
|
+
```
|
|
139
|
+
|
|
140
|
+
Results on Node 24, Apple M5:
|
|
141
|
+
|
|
142
|
+
| | |
|
|
143
|
+
|---|---|
|
|
144
|
+
| Seven-card hands evaluated exhaustively | 133,784,560 in 2.8 s — all category totals and the 4,824 distinct values match |
|
|
145
|
+
| Cross-check vs pokersolver | 0 mismatches in 1,000,000 categories and 1,000,000 showdowns |
|
|
146
|
+
| `scoreHand`, 7 cards | ~20,000,000 hands/s (pokersolver: ~120,000 hands/s, ~166× slower) |
|
|
147
|
+
| AA vs KK preflop, exact | 1,712,304 runouts in 74 ms — equity 81.2555%, identical to a pokersolver brute force |
|
|
148
|
+
| Test coverage | 100% lines and functions |
|
|
149
|
+
|
|
150
|
+
## API
|
|
151
|
+
|
|
152
|
+
| Area | Exports |
|
|
153
|
+
|---|---|
|
|
154
|
+
| Cards | `Card`, `parseCard`, `parseCards`, `formatCard`, `formatCards`, `makeCard`, `rankOf`, `suitOf`, `rankName`, `isCard`, `assertDistinctCards`, `RANKS`, `SUITS` |
|
|
155
|
+
| Randomness | `RandomSource`, `secureRandom`, `createSecureRandom`, `createSeededRandom` |
|
|
156
|
+
| Deck | `Deck`, `createDeck`, `shuffle`, `shuffled` |
|
|
157
|
+
| Evaluation | `scoreHand`, `evaluateHand`, `compareHands`, `compareScores`, `winners`, `categoryOf`, `handName`, `describeScore`, `HandCategory`, `HandScore`, `HandResult` |
|
|
158
|
+
| Equity | `calculateEquity`, `EquityOptions`, `EquityResult`, `PlayerEquity` |
|
|
159
|
+
| Pots | `buildPots`, `settlePots`, `Contribution`, `Pot`, `SettledPot`, `Settlement`, `SettleOptions` |
|
|
160
|
+
| Errors | `HoldemError` with a `code` (`INVALID_CARD`, `DUPLICATE_CARD`, `INVALID_HAND_SIZE`, …) |
|
|
161
|
+
|
|
162
|
+
Exports carry TSDoc comments, so your editor shows the details inline.
|
|
163
|
+
|
|
164
|
+
## Roadmap
|
|
165
|
+
|
|
166
|
+
- Hand-range notation (`QQ+`, `AKs`, `T9s-65s`) for range-vs-range equity
|
|
167
|
+
- Omaha (4 and 5 cards) evaluation
|
|
168
|
+
- Run-it-twice settlement helpers
|
|
169
|
+
|
|
170
|
+
Ideas and pull requests are welcome — see [CONTRIBUTING.md](CONTRIBUTING.md).
|
|
171
|
+
|
|
172
|
+
## Need a complete poker platform?
|
|
173
|
+
|
|
174
|
+
holdem-core is maintained by **HoldemForge**. We also build a complete, self-hosted Texas Hold'em club platform — multiplayer tables, clubs, admin panels and one-command Docker deployment — for entertainment play only, with no real-money features. See [holdemforge.com](https://holdemforge.com).
|
|
175
|
+
|
|
176
|
+
Contact: [hello@holdemforge.com](mailto:hello@holdemforge.com) · Telegram [@holdemforge](https://t.me/holdemforge)
|
|
177
|
+
|
|
178
|
+
## License
|
|
179
|
+
|
|
180
|
+
[MIT](LICENSE) © HoldemForge
|
package/README.zh-CN.md
ADDED
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
# holdem-core
|
|
2
|
+
|
|
3
|
+
**快速、零依赖的德州扑克工具库(TypeScript / JavaScript)。**
|
|
4
|
+
提供牌面解析、无偏洗牌、5–7 张牌型判断、精确胜率和边池结算,全部经过穷举验证。
|
|
5
|
+
|
|
6
|
+
[English](README.md)
|
|
7
|
+
|
|
8
|
+
```ts
|
|
9
|
+
import { parseCards, evaluateHand, calculateEquity, settlePots } from 'holdem-core';
|
|
10
|
+
|
|
11
|
+
evaluateHand(parseCards('Kh Kd Ks Tc Td 2s 3h')).description;
|
|
12
|
+
// → "Full House, Kings full of Tens"(葫芦,三条 K 带一对 10)
|
|
13
|
+
|
|
14
|
+
calculateEquity([parseCards('As Ah'), parseCards('Kd Kc')]).players[0].equity;
|
|
15
|
+
// → 0.812555…(精确值,穷举全部 1,712,304 种公共牌,约 75 毫秒)
|
|
16
|
+
|
|
17
|
+
settlePots(
|
|
18
|
+
[{ id: 'alice', amount: 50 }, { id: 'bob', amount: 100 }, { id: 'carol', amount: 100 }],
|
|
19
|
+
{ scores: { alice: 900, bob: 500, carol: 700 } },
|
|
20
|
+
).payouts;
|
|
21
|
+
// → { alice: 150, bob: 0, carol: 100 }(主池归 Alice,边池归 Carol)
|
|
22
|
+
```
|
|
23
|
+
|
|
24
|
+
## 特点
|
|
25
|
+
|
|
26
|
+
- **结果可验证。** 持续集成中穷举全部 **133,784,560** 种七张牌组合,各牌型数量与理论值完全一致;随机抽取 100 万手牌和 100 万次摊牌,与 [pokersolver](https://www.npmjs.com/package/pokersolver) 对比,结果零差异。
|
|
27
|
+
- **速度快。** 单核每秒约 **2000 万** 次七张牌判断:无需枚举 21 种组合,计算过程零内存分配,也不依赖几十 MB 的查找表(启动时仅生成约 24 KB)。
|
|
28
|
+
- **覆盖真实牌局。** 边池、弃牌玩家已投入的筹码、无人跟注时的退还、平分时的零头分配都已处理;每次结算都会校验所有人输赢合计为零。
|
|
29
|
+
- **默认安全。** 洗牌使用 `crypto.getRandomValues` 加拒绝采样,没有取模偏差;另提供可固定种子的随机源,供测试和回放使用。
|
|
30
|
+
- **轻量,自带类型。** 零运行时依赖,同时提供 ESM 和 CommonJS 两种格式,附完整类型声明;已在 Node 20 / 22 / 24 上测试,只用标准 Web API,在现代浏览器中也能直接运行。
|
|
31
|
+
|
|
32
|
+
## 安装
|
|
33
|
+
|
|
34
|
+
```sh
|
|
35
|
+
npm install holdem-core
|
|
36
|
+
```
|
|
37
|
+
|
|
38
|
+
## 用法
|
|
39
|
+
|
|
40
|
+
完整示例见 [英文说明](README.md#usage),要点:
|
|
41
|
+
|
|
42
|
+
- **牌**:`0..51` 的整数(`点数 * 4 + 花色`)。`parseCards('As Kd 10h Q♠')` 解析,`formatCards` 显示。
|
|
43
|
+
- **洗牌发牌**:`new Deck()` 用安全随机数洗牌,`deck.deal(n)` 发牌;`new Deck(createSeededRandom('种子'))` 每次顺序相同,只用于测试和回放。
|
|
44
|
+
- **比牌**:`scoreHand(cards)` 返回可直接比较的整数(越大越强,相等即平分);`evaluateHand` 额外给出牌型名称、描述和组成的五张牌。
|
|
45
|
+
- **胜率**:`calculateEquity(hands, { board, dead })`,能穷举时给精确值,否则自动蒙特卡洛。
|
|
46
|
+
- **结算**:`settlePots(contributions, { scores, oddChipOrder })` 算出主池、边池、每池赢家和每人净输赢。
|
|
47
|
+
|
|
48
|
+
## 验证与性能
|
|
49
|
+
|
|
50
|
+
```sh
|
|
51
|
+
npm test # 单元测试 + 性质测试,含全部 2,598,960 种五张牌
|
|
52
|
+
npm run verify:exhaustive # 全部 133,784,560 种七张牌
|
|
53
|
+
npm run verify:crosscheck # 与 pokersolver 对比 100 万手 + 100 万次摊牌
|
|
54
|
+
npm run bench
|
|
55
|
+
```
|
|
56
|
+
|
|
57
|
+
Node 24、Apple M5 上的结果:
|
|
58
|
+
|
|
59
|
+
| | |
|
|
60
|
+
|---|---|
|
|
61
|
+
| 七张牌穷举 | 133,784,560 种,2.8 秒;各牌型数量和 4,824 种不同牌力全部吻合 |
|
|
62
|
+
| 与 pokersolver 对比 | 100 万手牌型、100 万次摊牌,0 处不一致 |
|
|
63
|
+
| `scoreHand`(七张) | 约每秒 2000 万手(pokersolver 约每秒 12 万手,慢约 166 倍) |
|
|
64
|
+
| AA 对 KK 翻前精确胜率 | 1,712,304 种公共牌,74 毫秒;81.2555%,与 pokersolver 穷举结果完全相同 |
|
|
65
|
+
| 测试覆盖率 | 代码行、函数 100% |
|
|
66
|
+
|
|
67
|
+
## 需要完整的德州扑克平台?
|
|
68
|
+
|
|
69
|
+
holdem-core 由 **HoldemForge** 维护。我们还提供可自主部署的德州扑克俱乐部平台,包含多人牌桌、俱乐部、管理后台和 Docker 一键部署,仅限娱乐玩法,不含任何真钱功能。详见 [holdemforge.com](https://holdemforge.com)。
|
|
70
|
+
|
|
71
|
+
联系:[hello@holdemforge.com](mailto:hello@holdemforge.com) · Telegram [@holdemforge](https://t.me/holdemforge)
|
|
72
|
+
|
|
73
|
+
## 许可
|
|
74
|
+
|
|
75
|
+
[MIT](LICENSE) © HoldemForge
|