-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexample deck.js
398 lines (328 loc) · 11.2 KB
/
example deck.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
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
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
/******************************************************************************
* Playing Card Objects *
* *
* Do not remove this notice. *
* *
* Copyright 2001 by Mike Hall *
* Please see http://www.brainjar.com for terms of use. *
******************************************************************************/
//=============================================================================
// Card Object
//
// Note: Requires cards.css for display.
//=============================================================================
//-----------------------------------------------------------------------------
// Card constructor function.
//-----------------------------------------------------------------------------
function Card(rank, suit) {
this.rank = rank;
this.suit = suit;
this.toString = cardToString;
this.createNode = cardCreateNode;
}
//-----------------------------------------------------------------------------
// cardToString(): Returns the name of a card (including rank and suit) as a
// text string.
//-----------------------------------------------------------------------------
function cardToString() {
var rank, suit;
switch (this.rank) {
case "A" :
rank = "Ace";
break;
case "2" :
rank = "Two";
break;
case "3" :
rank = "Three";
break;
case "4" :
rank = "Four";
break;
case "5" :
rank = "Five";
break;
case "6" :
rank = "Six";
break;
case "7" :
rank = "Seven";
break;
case "8" :
rank = "Eight";
break;
case "9" :
rank = "Nine";
break;
case "10" :
rank = "Ten";
break;
case "J" :
rank = "Jack"
break;
case "Q" :
rank = "Queen"
break;
case "K" :
rank = "King"
break;
default :
rank = null;
break;
}
switch (this.suit) {
case "C" :
suit = "Clubs";
break;
case "D" :
suit = "Diamonds"
break;
case "H" :
suit = "Hearts"
break;
case "S" :
suit = "Spades"
break;
default :
suit = null;
break;
}
if (rank == null || suit == null)
return "";
return rank + " of " + suit;
}
//-----------------------------------------------------------------------------
// cardCreateNode(): Returns a DIV node which can be used to display the card
// on a page.
//-----------------------------------------------------------------------------
var cardImg0 = new Image(); cardImg0.src= "graphics/cardback.gif";
var cardImg1 = new Image(); cardImg1.src= "graphics/jack.gif";
var cardImg2 = new Image(); cardImg2.src= "graphics/queen.gif";
var cardImg3 = new Image(); cardImg3.src= "graphics/king.gif";
function cardCreateNode() {
var cardNode, frontNode, indexNode, spotNode, tempNode, textNode;
var indexStr, spotChar;
// This is the main node, a DIV tag.
cardNode = document.createElement("DIV");
cardNode.className = "card";
// Build the front of card.
frontNode = document.createElement("DIV");
frontNode.className = "front";
// Get proper character for card suit and change font color if necessary.
spotChar = "\u00a0";
switch (this.suit) {
case "C" :
spotChar = "\u2663";
break;
case "D" :
frontNode.className += " red";
spotChar = "\u2666";
break;
case "H" :
frontNode.className += " red";
spotChar = "\u2665";
break;
case "S" :
spotChar = "\u2660";
break;
}
// Create and add the index (rank) to the upper-left corner of the card.
indexStr = this.rank;
if (this.toString() == "")
indexStr = "\u00a0";
spotNode = document.createElement("DIV");
spotNode.className = "index";
textNode = document.createTextNode(indexStr);
spotNode.appendChild(textNode);
spotNode.appendChild(document.createElement("BR"));
textNode = document.createTextNode(spotChar);
spotNode.appendChild(textNode);
frontNode.appendChild(spotNode);
// Create and add spots based on card rank (Ace thru 10).
spotNode = document.createElement("DIV");
textNode = document.createTextNode(spotChar);
spotNode.appendChild(textNode);
if (this.rank == "A") {
spotNode.className = "ace";
tempNode = spotNode.cloneNode(true);
frontNode.appendChild(tempNode);
}
if (this.rank == "3" || this.rank == "5" || this.rank == "9") {
spotNode.className = "spotB3";
tempNode = spotNode.cloneNode(true);
frontNode.appendChild(tempNode);
}
if (this.rank == "2" || this.rank == "3") {
spotNode.className = "spotB1";
tempNode = spotNode.cloneNode(true);
frontNode.appendChild(tempNode);
}
if (this.rank == "2" || this.rank == "3") {
spotNode.className = "spotB5";
tempNode = spotNode.cloneNode(true);
frontNode.appendChild(tempNode);
}
if (this.rank == "4" || this.rank == "5" || this.rank == "6" ||
this.rank == "7" || this.rank == "8" || this.rank == "9" ||
this.rank == "10") {
spotNode.className = "spotA1";
tempNode = spotNode.cloneNode(true);
frontNode.appendChild(tempNode);
spotNode.className = "spotA5";
tempNode = spotNode.cloneNode(true);
frontNode.appendChild(tempNode);
spotNode.className = "spotC1";
tempNode = spotNode.cloneNode(true);
frontNode.appendChild(tempNode);
spotNode.className = "spotC5";
tempNode = spotNode.cloneNode(true);
frontNode.appendChild(tempNode);
}
if (this.rank == "6" || this.rank == "7" || this.rank == "8") {
spotNode.className = "spotA3";
tempNode = spotNode.cloneNode(true);
frontNode.appendChild(tempNode);
spotNode.className = "spotC3";
tempNode = spotNode.cloneNode(true);
frontNode.appendChild(tempNode);
}
if (this.rank == "7" || this.rank == "8" || this.rank == "10") {
spotNode.className = "spotB2";
tempNode = spotNode.cloneNode(true);
frontNode.appendChild(tempNode);
}
if (this.rank == "8" || this.rank == "10") {
spotNode.className = "spotB4";
tempNode = spotNode.cloneNode(true);
frontNode.appendChild(tempNode);
}
if (this.rank == "9" || this.rank == "10") {
spotNode.className = "spotA2";
tempNode = spotNode.cloneNode(true);
frontNode.appendChild(tempNode);
spotNode.className = "spotA4";
tempNode = spotNode.cloneNode(true);
frontNode.appendChild(tempNode);
spotNode.className = "spotC2";
tempNode = spotNode.cloneNode(true);
frontNode.appendChild(tempNode);
spotNode.className = "spotC4";
tempNode = spotNode.cloneNode(true);
frontNode.appendChild(tempNode);
}
// For face cards (Jack, Queen or King), create and add the proper image.
tempNode = document.createElement("IMG");
tempNode.className = "face";
if (this.rank == "J")
tempNode.src = "graphics/jack.gif";
if (this.rank == "Q")
tempNode.src = "graphics/queen.gif";
if (this.rank == "K")
tempNode.src = "graphics/king.gif";
// For face cards, add suit characters to the upper-left and lower-right
// corners.
if (this.rank == "J" || this.rank == "Q" || this.rank == "K") {
frontNode.appendChild(tempNode);
spotNode.className = "spotA1";
tempNode = spotNode.cloneNode(true);
frontNode.appendChild(tempNode);
spotNode.className = "spotC5";
tempNode = spotNode.cloneNode(true);
frontNode.appendChild(tempNode);
}
// Add front node to the card node.
cardNode.appendChild(frontNode);
// Return the card node.
return cardNode;
}
//=============================================================================
// Stack Object
//=============================================================================
//-----------------------------------------------------------------------------
// Stack constructor function.
//-----------------------------------------------------------------------------
function Stack() {
// Create an empty array of cards.
this.cards = new Array();
this.makeDeck = stackMakeDeck;
this.shuffle = stackShuffle;
this.deal = stackDeal;
this.draw = stackDraw;
this.addCard = stackAddCard;
this.combine = stackCombine;
this.cardCount = stackCardCount;
}
//-----------------------------------------------------------------------------
// stackMakeDeck(n): Initializes a stack using 'n' packs of cards.
//-----------------------------------------------------------------------------
function stackMakeDeck(n) {
var ranks = new Array("A", "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K");
var suits = new Array("C", "D", "H", "S");
var i, j, k;
var m;
m = ranks.length * suits.length;
// Set array of cards.
this.cards = new Array(n * m);
// Fill the array with 'n' packs of cards.
for (i = 0; i < n; i++)
for (j = 0; j < suits.length; j++)
for (k = 0; k < ranks.length; k++)
this.cards[i * m + j * ranks.length + k] = new Card(ranks[k], suits[j]);
}
//-----------------------------------------------------------------------------
// stackShuffle(n): Shuffles a stack of cards 'n' times.
//-----------------------------------------------------------------------------
function stackShuffle(n) {
var i, j, k;
var temp;
// Shuffle the stack 'n' times.
for (i = 0; i < n; i++)
for (j = 0; j < this.cards.length; j++) {
k = Math.floor(Math.random() * this.cards.length);
temp = this.cards[j];
this.cards[j] = this.cards[k];
this.cards[k] = temp;
}
}
//-----------------------------------------------------------------------------
// stackDeal(): Removes the first card in the stack and returns it.
//-----------------------------------------------------------------------------
function stackDeal() {
if (this.cards.length > 0)
return this.cards.shift();
else
return null;
}
//-----------------------------------------------------------------------------
// stackDraw(n): Removes the indicated card from the stack and returns it.
//-----------------------------------------------------------------------------
function stackDraw(n) {
var card;
if (n >= 0 && n < this.cards.length) {
card = this.cards[n];
this.cards.splice(n, 1);
}
else
card = null;
return card;
}
//-----------------------------------------------------------------------------
// stackAdd(card): Adds the given card to the stack.
//-----------------------------------------------------------------------------
function stackAddCard(card) {
this.cards.push(card);
}
//-----------------------------------------------------------------------------
// stackCombine(stack): Adds the cards in the given stack to the current one.
// The given stack is emptied.
//-----------------------------------------------------------------------------
function stackCombine(stack) {
this.cards = this.cards.concat(stack.cards);
stack.cards = new Array();
}
//-----------------------------------------------------------------------------
// stackCardCount(): Returns the number of cards currently in the stack.
//-----------------------------------------------------------------------------
function stackCardCount() {
return this.cards.length;
}