-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcardeck.js
275 lines (244 loc) · 6.91 KB
/
cardeck.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
(function(exports) {
/**
*
* Creates an instance of Card
*
* @constructor
* @this {Card}
* @param {number} numVal Numeric value of the card.
* @param {string} type Type of the card.
* @param {string} imgPath Path to the image which represents the card.
*/
function Card(numVal, type, imgPath) {
/** @private */ this.value = numVal || 0;
/** @private */ this.type = type || 'default';
/** @private */ this.imgPath = imgPath || './card.png';
}
/**
* Sets the value of a card.
*
* @this {Card}
* @param {number} v The numeric value of the card.
*/
Card.prototype.setValue = function (v) {
this.value = v;
};
/**
* Gets the value of the card.
*
* @this {Card}
* @return {number} The numeric value of the card.
*/
Card.prototype.getValue = function () {
return this.value;
};
/**
* Sets the type of a card.
*
* @this {Card}
* @param {string} t The type of the card.
*/
Card.prototype.setType = function (t) {
this.type = t;
};
/**
* Gets the type of the card.
*
* @this {Card}
* @return {string} The type of the card.
*/
Card.prototype.getType = function () {
return this.type;
};
/**
* Sets the path to the image which represents a card.
*
* @this {Card}
* @param {string} i The path to the image which represents the card.
*/
Card.prototype.setImgPath = function (i) {
this.imgPath = i;
};
/**
* Gets the path to the image which represents a card.
*
* @this {Card}
* @return {string} The path to the image which represents the card.
*/
Card.prototype.getImgPath = function () {
return this.imgPath;
};
/**
*
* Creates an instance of Deck
*
* @constructor
* @this {Deck}
* @param {string} name Name of the deck.
*/
function Deck(name) {
/** @private */ this.name = name || 'default';
/** @private */ this.cards = [];
/** @private */ this.size = 0;
}
/**
* Gets the name of the deck.
*
* @this {Deck}
* @return {string} The name of the deck.
*/
Deck.prototype.getName = function () {
return this.name;
};
/**
* Sets the name of the deck.
*
* @this {Deck}
* @param {string} n The name of the deck.
*/
Deck.prototype.setName = function (n) {
this.name = n;
};
/**
* Gets the size of the deck.
*
* @this {Deck}
* @return {number} The size of the deck.
*/
Deck.prototype.getSize = function () {
return this.size;
};
/**
* Adds a new card at the top of the deck.
*
* @this {Deck}
* @param {Object} c The object or card to insert on the deck.
*/
Deck.prototype.addCardToTop = function (c) {
if (c instanceof Card)
this.cards.unshift(c);
else
this.cards.unshift(new Card(c.value,c.type,c.imgPath));
this.size++;
};
/**
* Adds a new card at the end of the deck.
*
* @this {Deck}
* @param {Object} c The object or card to insert on the deck.
*/
Deck.prototype.addCardToBottom = function (c) {
if (c instanceof Card)
this.cards.push(c);
else
this.cards.push(new Card(c.value,c.type,c.imgPath));
this.size++;
};
/**
* Draws a card from the top of the deck.
*
* @this {Deck}
* @return {Card} The card at the top of the deck.
*/
Deck.prototype.drawCardFromTop = function () {
if (this.size > 0) {
var pickedCard = this.cards.shift();
this.size--;
return pickedCard;
}
};
/**
* Draws a card from the bottom of the deck.
*
* @this {Deck}
* @return {Card} The card at the bottom of the deck.
*/
Deck.prototype.drawCardFromBottom = function () {
if (this.size > 0) {
var pickedCard = this.cards.pop();
this.size--;
return pickedCard;
}
};
/**
* Adds a new card at a random position of the deck.
*
* @this {Deck}
* @return {Card} The card to add to the deck.
*/
Deck.prototype.addCardToRandom = function (c) {
var position = Math.floor(Math.random() * (this.size + 1));
if (c instanceof Card)
this.cards.splice(position, 0, c);
else
this.cards.splice(position, 0, new Card(c.value,c.type,c.imgPath));
this.size++;
};
/**
* Draws a card from a random position of the deck.
*
* @this {Deck}
* @return {Card} The card at a random position of the deck.
*/
Deck.prototype.drawCardFromRandom = function () {
if (this.size > 0) {
var position = Math.floor(Math.random() * this.size);
var pickedCard = this.cards.splice(position, 1);
this.size--;
return pickedCard[0];
}
};
/**
* Shuffles the order of the cards in a deck.
*
* @this {Deck}
*/
Deck.prototype.shuffle = function () {
var currentIndex = this.cards.length, temporaryValue, randomIndex;
// While there remain elements to shuffle...
while (0 !== currentIndex) {
// Pick a remaining element...
randomIndex = Math.floor(Math.random() * currentIndex);
currentIndex--;
// And swap it with the current element.
temporaryValue = this.cards[currentIndex];
this.cards[currentIndex] = this.cards[randomIndex];
this.cards[randomIndex] = temporaryValue;
}
};
/**
* Exchange the order of the first half of cards of the deck with the second one.
*
* @this {Deck}
*/
Deck.prototype.cut = function () {
var halfIndex = this.size / 2;
var firstHalf = this.cards.splice(0, halfIndex);
this.cards = this.cards.concat(firstHalf);
};
/**
* Deal cards by turns for each player, drawing from the top
*
* @this {Deck}
* @returns {Object<Array<Card>>} An object with as many array of Cards as players specified
*/
Deck.prototype.deal = function (c, p) {
var result = {};
for (var player = 0; player < p; player++) {
var cardsPerPlayer = [];
for (var card = 0; card < c; card++) {
var currentCard = this.drawCardFromTop();
if (currentCard) {
cardsPerPlayer.push(currentCard);
}
else {
break;
}
}
result[player] = cardsPerPlayer;
}
return result;
};
exports.Deck = Deck;
exports.Card = Card;
})(typeof exports === 'undefined' ? this['cardeck'] = {} : exports);