-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
376 lines (295 loc) · 10.2 KB
/
script.js
File metadata and controls
376 lines (295 loc) · 10.2 KB
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
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
// Initialize pot and player stacks
const SMALL_BLIND = 5;
const BIG_BLIND = 10;
const STARTING_STACK = 1000;
const FULL_RESET = 1;
const NEXT_CARD_RESET = 0;
let pot = 0;
let prevBet = 0;
let raiser = -1;
let dealer = 0;
let turn = 1;
let numPlayers = 1;
let playerStacks = {
player1: STARTING_STACK
};
let alreadyAdded = {
player1: 0
}
let folded = {
player1: false
}
// Function to add to the pot (deduct from the player's stack)
function raise(playerNum) {
const playerid = "player" + playerNum;
const amount = parseInt(prompt("Raise amount:"));
// support the cancel button, do not allow bad input
if (amount == null || amount < 0) return;
if (playerStacks[playerid] >= amount) {
if (amount >= prevBet * 2) {
// raise goes through
raiser = playerNum;
prevBet = amount + alreadyAdded[playerid];
put(playerNum, amount);
nextTurn();
} else {
alert("Minimum bet is " + prevBet * 2);
}
} else {
alert("Not enough chips in the player's stack!");
}
}
function put(playerNum, amount) {
const player = "player" + playerNum;
playerStacks[player] -= amount;
pot += amount;
alreadyAdded[player] += amount;
// Update player's stack
renderStackandPot(playerNum);
}
function callBet(playerNum) {
const playerid = getPlayerID(playerNum);
let amount = prevBet - alreadyAdded[playerid];
put(playerNum, amount);
checkForEndOfTurn();
}
function fold(playerNum) {
folded[getPlayerID(playerNum)] = true;
document.getElementById(`stack${playerNum}`).classList.add("folded");
document.getElementById(`pay${playerNum}`).classList.add("disabled");
checkForEndOfTurn();
}
// NEED TO ALLOW FOR THE LAST PERSON TO RAISE
function checkForEndOfTurn() {
let playersAlive = 0;
let alivePlayer = -1;
for (let i = 1; i <= numPlayers; i++) {
if (!folded[getPlayerID(i)]) {
playersAlive++;
alivePlayer = i;
}
}
if (playersAlive < 2) {
console.log("Alive player: ", alivePlayer);
pay(alivePlayer);
return;
}
let nextTurnNum = turn + 1 > numPlayers ? 1 : turn + 1;
console.log(`Turn: ${turn}, Raiser: ${raiser}`);
if (nextTurnNum == raiser) {
window.alert("Deal next card(s)!");
resetRound(NEXT_CARD_RESET);
} else {
nextTurn();
}
}
function pay(playerNum) {
const playerid = getPlayerID(playerNum);
if (folded[playerid]) return;
playerStacks[playerid] += pot;
pot = 0;
renderStackandPot(playerNum);
// disable buttons
removeButtons(turn);
// enable add/remove players
enableAddRemovePlayers();
resetRound(FULL_RESET);
}
function resetRound(full) {
prevBet = 0;
if (full) {
for (let playerNum = 1; playerNum <= numPlayers; playerNum++) {
alreadyAdded[getPlayerID(playerNum)] = 0;
folded[getPlayerID(playerNum)] = false;
document.getElementById(`stack${playerNum}`).classList.remove("folded");
document.getElementById(`pay${playerNum}`).classList.remove("disabled");
}
} else {
// set current turn to dealer + 1
removeButtons(turn);
turn = dealer + 1 > numPlayers ? 1 : dealer + 1;
raiser = dealer;
addButtons(turn);
// players have added 0 to the pot so far
for (let i = 1; i <= numPlayers; i++) {
alreadyAdded[getPlayerID(i)] = 0;
}
}
}
function blinds() {
// don't start a pot while one's already going
if (pot != 0) return;
// disable add/remove players
disableAddRemovePlayers();
nextDealer();
// set initial call value to blind value
prevBet = BIG_BLIND;
// if a player is too short stacked, problems will arise
nextTurn();
playerNumSmall = turn;
nextTurn();
playerNumBig = turn;
nextTurn();
small = getPlayerID(playerNumSmall);
big = getPlayerID(playerNumBig);
raiser = playerNumBig;
put(playerNumSmall, SMALL_BLIND);
put(playerNumBig, BIG_BLIND);
}
function renderStackandPot(playerNum) {
document.getElementById("stack" + playerNum).innerText = playerStacks[getPlayerID(playerNum)];
document.getElementById("pot").textContent = pot;
}
function nextTurn() {
removeButtons(turn);
turn = turn + 1 > numPlayers ? 1 : turn + 1;
addButtons(turn);
if (folded["player" + turn]) nextTurn();
}
function nextDealer() {
const dbutton = document.getElementById("dealer");
if (dbutton) {
// Remove the element from the DOM
dbutton.remove();
console.log('Dealer button removed successfully.');
} else {
console.log('No dealer button found to remove.');
}
dealer = dealer + 1 > numPlayers ? 1 : dealer + 1;
turn = dealer;
// Create the dealer button
let dealerButton = document.createElement("div");
dealerButton.textContent = "D";
dealerButton.classList.add("dealer-button");
dealerButton.title = "Dealer Button";
dealerButton.id = "dealer";
// Find the player and append the dealer button
let dealerContainer = document.getElementById("dealer-container" + dealer);
dealerContainer.appendChild(dealerButton);
}
function removeButtons(playerNum) {
// Find the container and append the buttons
let container = document.getElementById("button-container" + playerNum);
container.innerHTML = "";
}
// Function to dynamically add the buttons
function addButtons(playerNum) {
// Create the raise button
let raiseButton = document.createElement("button");
raiseButton.textContent = "+";
raiseButton.classList.add("optionButton");
raiseButton.onclick = function() {
raise(playerNum);
};
// Create the call/check button
let callButton = document.createElement("button");
callButton.textContent = "-";
callButton.classList.add("callButton");
callButton.classList.add("optionButton");
callButton.onclick = function() {
callBet(playerNum);
};
// Create the fold button
let foldButton = document.createElement("button");
foldButton.textContent = "x";
foldButton.classList.add("foldButton");
foldButton.classList.add("optionButton");
foldButton.onclick = function() {
fold(playerNum);
};
// Find the container and append the buttons
let container = document.getElementById("button-container" + playerNum);
container.appendChild(raiseButton);
container.appendChild(callButton);
container.appendChild(foldButton);
}
function addPlayer() {
if (pot != 0) return;
console.log("Player being added");
numPlayers++;
let playerNum = numPlayers;
let playerid = getPlayerID(playerNum);
let containerId = "player-container";
let payContainerId = "pay-container";
// The dynamic HTML string to be added
const playerHTML = `
<div class="player" id="${playerid}">
<div class="buttcont" id="dealer-container${playerNum}"></div>
<input type="text" id="l${playerNum}" value="Name">
<label id="stack${playerNum}">${STARTING_STACK}</label>
<div class="buttcont" id="button-container${playerNum}"></div>
</div>
`;
const payButtonHTML = `
<button id="pay${playerNum}" class="payButton" onclick="pay(${playerNum})">${playerNum}</button>
`
// Find the container element by ID
const playerContainer = document.getElementById(containerId);
const payContainer = document.getElementById(payContainerId);
if (playerContainer && payContainer) {
// Append the HTML to the container
playerContainer.insertAdjacentHTML('beforeend', playerHTML);
console.log(`Player ${playerNum} HTML added to container with ID: ${containerId}`);
playerStacks[playerid] = STARTING_STACK;
alreadyAdded[playerid] = 0;
folded[playerid] = false;
payContainer.insertAdjacentHTML('beforeend', payButtonHTML);
} else {
console.error(`Container with ID "${containerId}" or "${payContainerId}" not found.`);
}
}
function removePlayer() {
if (pot != 0) return;
console.log("Player being added");
let playerNum = numPlayers;
let playerid = getPlayerID(playerNum);
let payid = `pay${playerNum}`;
// Find the player element by ID
const playerHTML = document.getElementById(playerid);
const payButtonHTML = document.getElementById(payid);
if (playerHTML && payButtonHTML) {
// Append the HTML to the container
playerHTML.remove();
payButtonHTML.remove();
console.log(`Player ${playerNum} HTML removed`);
delete playerStacks[playerid];
delete alreadyAdded[playerid];
delete folded[playerid];
numPlayers--;
} else {
console.error(`Player with ID "${playerid}" or "${payid}" not found.`);
}
}
function enableAddRemovePlayers() {
const addButtonID = "add-button";
const removeButtonID = "remove-button";
const startButtonID = "start-button";
const addButton = document.getElementById(addButtonID);
const removeButton = document.getElementById(removeButtonID);
const startButton = document.getElementById(startButtonID);
if (addButton && removeButton && startButton) {
addButton.classList.remove("disabled");
removeButton.classList.remove("disabled");
startButton.classList.remove("disabled");
} else {
console.error(`Add and Remove container not found`);
}
}
function disableAddRemovePlayers() {
const addButtonID = "add-button";
const removeButtonID = "remove-button";
const startButtonID = "start-button";
const addButton = document.getElementById(addButtonID);
const removeButton = document.getElementById(removeButtonID);
const startButton = document.getElementById(startButtonID);
if (addButton && removeButton && startButton) {
addButton.classList.add("disabled");
removeButton.classList.add("disabled");
startButton.classList.add("disabled");
} else {
console.error(`Add and Remove container not found`);
}
}
function getPlayerID(playerNum) {
return `player${playerNum}`;
}