-
Notifications
You must be signed in to change notification settings - Fork 3
/
holdem.js
181 lines (165 loc) · 4.38 KB
/
holdem.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
"use strict";
var poker = require("./poker");
var utils = require("./poker-utils");
function pickWinner (err, board, hands, callback) {
// Validate Cards
if (!poker.validateCards(board)) {
callback("Invalid Card in Board");
return "Invalid Card in Board";
}
for (var hand in hands) {
if (!poker.validateCards(hands[hand])) {
callback("Invalid Card in " + hand);
return "Invalid Card in " + hand;
}
}
// Convert Cards
board = poker.convertCards(board);
var pockets = [];
for (var hand in hands) {
pockets.push([hand, poker.convertCards(hands[hand])]);
}
// Check for duplicates
var cards = board;
for (var i = 0; i < pockets.length; i++) {
cards = cards.concat(pockets[i][1]);
}
for (var i = 0; i < cards.length; i++) {
for (var j = i + 1; j < cards.length; j++) {
if (cards[i].name == cards[j].name && cards[i].suit == cards[j].suit) {
callback("Invalid Deck -- Duplicate cards");
return "Invalid Deck -- Duplicate cards";
}
}
}
// Determine all hands
var allHands = [];
for (var i = 0; i < pockets.length; i++) {
var allCards = board.concat(pockets[i][1]);
allHands.push(poker.determineHighHand(allCards));
}
// Compare hands, find a winner
var highHandNum = 0;
var highHand = allHands[0];
var highHandVal = poker.evalHand(allHands[0]);
var highPockets = pockets[0][0];
for (var i = 1; i < allHands.length; i++) {
var ret = poker.compareHands(allHands[highHandNum], allHands[i]);
if (ret.err) {
console.log(i);
console.log(ret.err);
console.log(highHandNum);
callback(ret.err);
return ret.err;
}
if (ret.winner == 2) {
highHandNum = i;
highHandVal = ret.handVal;
highHand = ret.hand;
highPockets = pockets[i][1];
}
}
callback(null, pockets[highHandNum][0], highHandVal, highHand);
return {
error: null,
highHandCards: highHand,
highHandString: pockets[highHandNum][0],
handValue: highHandVal,
};
}
function dealHand(players, deck) {
for (var i in players) {
players[i].hand = [];
}
for (i = 0; i < 2; i++) {
for (var j in players) {
players[j].hand.push(deck.shift());
}
}
return {
players: players,
deck: deck,
};
}
function dealStreet(board, deck) {
if (board.length == 0) {
deck.shift();
for (var i = 0; i < 3; i++) {
board.push(deck.shift());
}
} else if (board.length == 3 || board.length == 4) {
deck.shift();
board.push(deck.shift());
}
return {
board: board,
deck: deck
};
}
/**
* Object Definitions:
* GameState
* {players, playing, board, deck, dealer, action, blinds, minBet, minRaise, lastRaiser, currentBets, pot,}
*
* Players
* {id, name, seat, hand, stack, inNextHand, canBeDealer, bank,}
*
* {
* players: {
* '0': {
* id: 0,
* name: 'RiverBitch'.
* seat: 0,
* hand: ['As', 'Qs',],
* stack: 1500,
* inNextHand: true,
* canBeDealer: true,
* bank: 1000000,
* },
* '1': {
* id: 1,
* name: 'SirFoldsAlot',
* seat: 1,
* hand: null,
* stack: 1500,
* inNextHand: true,
* canBeDealer: true,
* bank: 1000000,
* },
* '5': {
* id: 2,
* name: 'KoJack',
* seat: 5,
* hand: ['Kc', 'Jd',],
* stack: 1500,
* inNextHand: true,
* canBeDealer: true,
* bank: 1000000,
* },
* },
* playing: [0, 5,],
* deck: [ ... ]
* board: ['Ks', 'Js', 'Jc', '5d', 'Ts',],
* dealer: 0,
* action: 1,
* blinds: {
* small: 2,
* big: 5,
* },
* minBet: 0,
* minRaise: 5,
* lastRaiser: 1,
* currentBets: {
* '0': 0,
* '5': 0,
* },
* pot: 20,
* }
*/
function makeDecision(gameState) {
}
module.exports = {
pickWinner: pickWinner,
dealHand: dealHand,
dealStreet: dealStreet,
}