-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinitDeck.js
More file actions
171 lines (141 loc) · 5.58 KB
/
initDeck.js
File metadata and controls
171 lines (141 loc) · 5.58 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
// initDeck.js
// Handles deck creation and dealing logic for Rummy-style games
// Works standalone or integrated with HTML UI
class initDeck {
constructor() {
this.drawPile = [];
this.discardPile = [];
// Default card definitions
this.suits = ["♥", "♦", "♣", "♠"];
this.ranks = ["2","3","4","5","6","7","8","9","10","J","Q","K","A"];
this.extraSuitSymbol = "★";
}
// Get rule settings — from HTML if available, otherwise defaults
getRuleSettings() {
if (typeof window !== "undefined" && window.gameRules) {
// Use values from the HTML or global gameRules object
return {
extraDeck: window.gameRules.extraDeck ?? 0,
extraSuit: window.gameRules.extraSuit ?? false,
wildType: window.gameRules.wildType ?? "classic",
wildsEnabled: window.gameRules.wildsEnabled ?? true,
};
}
// Default fallback if not connected to HTML
return {
extraDeck: 0,
extraSuit: true,
wildType: "classic",
wildsEnabled: true,
};
}
// Create and shuffle the deck
createDeck(numPlayers = 3) {
const { extraDeck, extraSuit, wildType, wildsEnabled } = this.getRuleSettings();
// Determine number of decks
let decksNeeded = 2;
if (numPlayers > 3) decksNeeded += Math.floor((numPlayers - 3) / 2);
decksNeeded += extraDeck;
// Determine suits
const suitsToUse = [...this.suits];
if (extraSuit) suitsToUse.push(this.extraSuitSymbol);
let allCards = [];
// Build decks
for (let i = 0; i < decksNeeded; i++) {
for (let suit of suitsToUse) {
for (let rank of this.ranks) {
allCards.push({ rank, suit });
}
}
// Add Jokers if applicable
if (wildsEnabled && wildType === "joker") {
allCards.push({ rank: "W", suit: "♦" });
allCards.push({ rank: "W", suit: "♥" });
allCards.push({ rank: "W", suit: "♣" });
allCards.push({ rank: "W", suit: "♠" });
if (extraSuit) allCards.push({ rank: "W", suit: "★" });
}
}
this.drawPile = this.shuffle(allCards);
this.discardPile = [];
return this.drawPile;
}
// Fisher–Yates shuffle
shuffle(array) {
for (let i = array.length - 1; i > 0; i--) {
const j = Math.floor(Math.random() * (i + 1));
[array[i], array[j]] = [array[j], array[i]];
}
return array;
}
reshuffle(drawPile, discardPile) {
if (drawPile.length > 1) {
console.warn('More than 1 card in drawpile');
return;
}
// Nothing to reshuffle if there are 0 or 1 cards in the discard pile.
if (!Array.isArray(discardPile) || discardPile.length <= 1) {
console.warn('Reshuffle called with insufficient discard cards.');
return { drawPile, discardPile };
}
// Keep the top (most recent) discard card.
const topDiscard = discardPile[discardPile.length - 1];
// All older discards become the pool to shuffle.
const toShuffle = discardPile.slice(0, -1);
// Empty the discard pile and put the top card back.
discardPile.length = 0;
discardPile.push(topDiscard);
// Shuffle the pool using the class's own Fisher–Yates implementation.
const shuffled = this.shuffle(toShuffle);
// --------------------------------------------------------------
// Insert the shuffled cards **under** the existing draw‑pile.
// drawPile is drawn with `pop()`, so we prepend the shuffled cards.
// --------------------------------------------------------------
if (!Array.isArray(drawPile)) drawPile = [];
// Prepend – the existing draw‑card(s) stay on top.
drawPile.unshift(...shuffled);
console.info(`Reshuffled ${shuffled.length} cards back into draw pile.`);
return { drawPile, discardPile };
}
// Deal 10 cards to each player (one at a time)
dealCards(playerNames = []) {
// Fallback to default player set if none provided
if (!playerNames || playerNames.length === 0) {
const numPlayers = (typeof window !== "undefined" && window.numPlayers) ? window.numPlayers : 3;
playerNames = Array.from({ length: numPlayers }, (_, i) => `Player ${i + 1}`);
}
const hands = {};
playerNames.forEach(name => hands[name] = []);
for (let i = 0; i < 10; i++) {
for (let name of playerNames) {
const card = this.drawPile.shift();
if (card) hands[name].push(card);
}
}
return hands;
}
}
// Export for browser and Node compatibility
if (typeof window !== "undefined") {
window.initDeck = initDeck;
} else if (typeof module !== "undefined") {
module.exports = initDeck;
}
// --- Quick test block (only runs when not in browser) ---
if (typeof window === "undefined") {
const initDeck = module.exports;
const cm = new initDeck();
// Create deck for 4 players
const drawPile = cm.createDeck(4);
console.log(`\nDraw pile created with ${drawPile.length} cards.`);
// Deal to 4 players
const hands = cm.dealCards(["Alice", "Bob", "Charlie", "David"]);
// Print each player's hand
for (const [player, cards] of Object.entries(hands)) {
console.log(`\n${player}'s hand (${cards.length} cards):`);
console.log(cards.map(c => `${c.rank}${c.suit}`).join(" "));
}
// Print remaining draw pile size and top 5 cards for reference
console.log(`\nRemaining draw pile: ${cm.drawPile.length} cards`);
console.log("Top 5 cards of remaining draw pile:", cm.drawPile.slice(0, 5).map(c => `${c.rank}${c.suit}`).join(" "));
}