-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathp2.js
136 lines (115 loc) · 3.82 KB
/
p2.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
// by GPT4.0
const pokersolver = require('pokersolver');
const Hand = pokersolver.Hand;
// const suits = ['c', 'd', 'h', 's'];
const suits = ['♠', '♥', '♦', '♣'];
const ranks = '23456789TJQKA';
function calculateWinRate(playerHand, communityCards) {
let wins = 0;
let ties = 0;
let total = 0;
const unexposedCards = [];
for (const suit of suits) {
for (const rank of ranks) {
const card = rank + suit;
if (!playerHand.includes(card) && !communityCards.includes(card)) {
unexposedCards.push(card);
}
}
}
const communityCombinations = unexposedCards.length >= 5 - communityCards.length
? k_combinations(unexposedCards, 5 - communityCards.length)
: [communityCards];
for (const community of communityCombinations) {
const fullCommunity = community.concat(communityCards);
const playerBestHand = Hand.solve(playerHand.concat(fullCommunity));
for (const card1 of unexposedCards) {
if (community.includes(card1)) continue;
for (const card2 of unexposedCards) {
if (community.includes(card2) || card1 === card2) continue;
const opponentHand = [card1, card2];
const opponentBestHand = Hand.solve(opponentHand.concat(fullCommunity));
const winner = Hand.winners([playerBestHand, opponentBestHand]);
if (winner.length === 1 && winner[0] === playerBestHand) {
wins++;
} else if (winner.length === 2) {
ties++;
}
total++;
}
}
}
return {
winRate: wins / total,
tieRate: ties / total
};
}
function k_combinations(set, k) {
const combinations = [];
const n = set.length;
function helper(start, comb) {
if (comb.length === k) {
combinations.push(comb.slice());
return;
}
for (let i = start; i < n; i++) {
comb.push(set[i]);
helper(i + 1, comb);
comb.pop();
}
}
helper(0, []);
return combinations;
}
function monteCarloSimulation(playerHand, communityCards, numSimulations = 10000) {
let wins = 0;
let ties = 0;
let total = 0;
for (let i = 0; i < numSimulations; i++) {
const [opponentHand, fullCommunity] = generateRandomHandAndCommunity(playerHand, communityCards);
const playerBestHand = Hand.solve(playerHand.concat(fullCommunity));
const opponentBestHand = Hand.solve(opponentHand.concat(fullCommunity));
const winner = Hand.winners([playerBestHand, opponentBestHand]);
if (winner.length === 1 && winner[0] === playerBestHand) {
wins++;
} else if (winner.length === 2) {
ties++;
}
total++;
}
return {
winRate: wins / total,
tieRate: ties / total
};
}
function generateRandomHandAndCommunity(playerHand, communityCards) {
const unexposedCards = [];
for (const suit of suits) {
for (const rank of ranks) {
const card = rank + suit;
if (!playerHand.includes(card) && !communityCards.includes(card)) {
unexposedCards.push(card);
}
}
}
const opponentHand = [];
for (let i = 0; i < 2; i++) {
const randomIndex = Math.floor(Math.random() * unexposedCards.length);
opponentHand.push(unexposedCards.splice(randomIndex, 1)[0]);
}
const numCommunityCardsNeeded = 5 - communityCards.length;
const fullCommunity = communityCards.slice();
for (let i = 0; i < numCommunityCardsNeeded; i++) {
const randomIndex = Math.floor(Math.random() * unexposedCards.length);
fullCommunity.push(unexposedCards.splice(randomIndex, 1)[0]);
}
return [opponentHand, fullCommunity];
}
// const playerHand = ['Ah', 'Ks'];
// const communityCards = ['Qd', 'Js', 'Th'];
// // const result = calculateWinRate(playerHand, communityCards);
// const result = monteCarloSimulation(playerHand, communityCards);
// console.log(`胜率:${result.winRate.toFixed(4)},平局率:${result.tieRate.toFixed(4)}`);
module.exports = {
monteCarloSimulation
}