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
class Card {
constructor(rank, suit) {
this.rank = rank;
this.suit = suit;
}
toString() {
return this.rank + this.suit;
}
}
function generateDeck() {
const suits = ["h", "d", "c", "s"];
const ranks = ["2", "3", "4", "5", "6", "7", "8", "9", "T", "J", "Q", "K", "A"];
let deck = [];
for (let rank of ranks) {
for (let suit of suits) {
deck.push(new Card(rank, suit));
}
}
return deck;
}
function parseHand(handStr) {
const rank1 = handStr.slice(0, 1);
const rank2 = handStr.slice(1, 2);
const suited = handStr[2] === "s"; // Check if the hand is suited or offsuit
return [
new Card(rank1, suited ? "h" : "h"), // Assign the same suit for suited hands
new Card(rank2, suited ? "h" : "d"), // Assign different suits for offsuit hands
];
}
function shuffleDeck(deck) {
for (let i = deck.length - 1; i > 0; i--) {
const j = Math.floor(Math.random() * (i + 1));
[deck[i], deck[j]] = [deck[j], deck[i]];
}
return deck;
}
function generateAllHands(deck) {
let hands = [];
for (let i = 0; i < deck.length; i++) {
for (let j = i + 1; j < deck.length; j++) {
hands.push([deck[i], deck[j]]);
}
}
return hands;
}
function getHandRanks(cards) {
const rankOrder = "23456789TJQKA";
return cards.map(card => rankOrder.indexOf(card.rank)).sort((a, b) => b - a);
}
function countOccurrences(arr) {
return arr.reduce((acc, val) => {
acc[val] = (acc[val] || 0) + 1;
return acc;
}, {});
}
function isFlush(cards) {
return cards.every(card => card.suit === cards[0].suit);
}
function isStraight(ranks) {
const sortedRanks = ranks.slice().sort((a, b) => a - b);
if (sortedRanks[0] === 12 && sortedRanks.slice(1).join("") === "01234") return true; // Special case for A-2-3-4-5
for (let i = 0; i < sortedRanks.length - 1; i++) {
if (sortedRanks[i] - sortedRanks[i + 1] !== 1) return false;
}
return true;
}
function evaluateHand(cards) {
const ranks = getHandRanks(cards);
const occurrences = countOccurrences(ranks);
const isFlushHand = isFlush(cards);
const isStraightHand = isStraight(ranks);
const rankCount = Object.values(occurrences).sort((a, b) => b - a);
if (isStraightHand && isFlushHand && ranks[0] === 12) return 10; // Royal Flush
if (isStraightHand && isFlushHand) return 9; // Straight Flush
if (rankCount[0] === 4) return 8; // Four of a Kind
if (rankCount[0] === 3 && rankCount[1] === 2) return 7; // Full House
if (isFlushHand) return 6; // Flush
if (isStraightHand) return 5; // Straight
if (rankCount[0] === 3) return 4; // Three of a Kind
if (rankCount[0] === 2 && rankCount[1] === 2) return 3; // Two Pair
if (rankCount[0] === 2) return 2; // One Pair
return 1; // High Card
}
function precomputeOpponentHands(vpipArray, deck, heroHand) {
let availableCards = deck.filter(card => !heroHand.includes(card));
Val Town is a social website to write and deploy JavaScript.
Build APIs and schedule functions from your browser.
Comments
Nobody has commented on this val yet: be the first!
September 10, 2024