-
Notifications
You must be signed in to change notification settings - Fork 1
/
shared.js
368 lines (338 loc) · 7.08 KB
/
shared.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
/*
The default starter deck is 1 of each card.
*/
defaultDeck = [
// Hearts
{
suit: 'hearts',
value: 2
},
{
suit: 'hearts',
value: 3
},
{
suit: 'hearts',
value: 4
},
{
suit: 'hearts',
value: 5
},
{
suit: 'hearts',
value: 6
},
{
suit: 'hearts',
value: 7
},
{
suit: 'hearts',
value: 8
},
{
suit: 'hearts',
value: 9
},
{
suit: 'hearts',
value: 10
},
{
suit: 'hearts',
value: 'jack'
},
{
suit: 'hearts',
value: 'queen'
},
{
suit: 'hearts',
value: 'king'
},
{
suit: 'hearts',
value: '1'
},
// Spades
{
suit: 'spades',
value: 2
},
{
suit: 'spades',
value: 3
},
{
suit: 'spades',
value: 4
},
{
suit: 'spades',
value: 5
},
{
suit: 'spades',
value: 6
},
{
suit: 'spades',
value: 7
},
{
suit: 'spades',
value: 8
},
{
suit: 'spades',
value: 9
},
{
suit: 'spades',
value: 10
},
{
suit: 'spades',
value: 'jack'
},
{
suit: 'spades',
value: 'queen'
},
{
suit: 'spades',
value: 'king'
},
{
suit: 'spades',
value: '1'
},
// Clubs
{
suit: 'clubs',
value: 2
},
{
suit: 'clubs',
value: 3
},
{
suit: 'clubs',
value: 4
},
{
suit: 'clubs',
value: 5
},
{
suit: 'clubs',
value: 6
},
{
suit: 'clubs',
value: 7
},
{
suit: 'clubs',
value: 8
},
{
suit: 'clubs',
value: 9
},
{
suit: 'clubs',
value: 10
},
{
suit: 'clubs',
value: 'jack'
},
{
suit: 'clubs',
value: 'queen'
},
{
suit: 'clubs',
value: 'king'
},
{
suit: 'clubs',
value: '1'
},
// Diamonds
{
suit: 'diamonds',
value: 2
},
{
suit: 'diamonds',
value: 3
},
{
suit: 'diamonds',
value: 4
},
{
suit: 'diamonds',
value: 5
},
{
suit: 'diamonds',
value: 6
},
{
suit: 'diamonds',
value: 7
},
{
suit: 'diamonds',
value: 8
},
{
suit: 'diamonds',
value: 9
},
{
suit: 'diamonds',
value: 10
},
{
suit: 'diamonds',
value: 'jack'
},
{
suit: 'diamonds',
value: 'queen'
},
{
suit: 'diamonds',
value: 'king'
},
{
suit: 'diamonds',
value: '1'
}
];
/*
How to randomize a javascript array?
http://stackoverflow.com/questions/2450954/how-to-randomize-a-javascript-array
*/
shuffle = function (array) {
var currentIndex = array.length
, temporaryValue
, randomIndex
;
// While there remain elements to shuffle...
while (0 !== currentIndex) {
// Pick a remaining element...
randomIndex = Math.floor(Math.random() * currentIndex);
currentIndex -= 1;
// And swap it with the current element.
temporaryValue = array[currentIndex];
array[currentIndex] = array[randomIndex];
array[randomIndex] = temporaryValue;
}
return array;
};
/**
* Determine if it's the specified players turn.
* @param user
* @param game
*/
isTurn = function (user, game) {
return (game.player1 == user.username && game.turn == 'player1')
|| (game.player2 == user.username && game.turn == 'player2');
};
/**
* Determine the specified player's seat in the game. (player1 or player2)
* @param user
* @param game
* @returns {string}
*/
getSeat = function (user, game) {
var player = null;
if (game.player1 == user.username) {
player = 'player1';
} else if (game.player2 == user.username) {
player = 'player2';
}
return player;
};
/**
* Finds the card in the deck.
* http://stackoverflow.com/questions/5579678/jquery-how-to-find-an-object-by-attribute-in-an-array
* @param deck
* @param cardId
* @returns {*}
*/
findCardInDeck = function (deck, cardId) {
var card = false;
for (var i = 0; i < deck.length; i++) {
if (deck[i].id == cardId) {
card = deck[i];
card.index = i;
}
}
return card;
};
/**
* Determine if the specified move is legal.
* @param stack
* @param card
* @param target
*/
isLegalMove = function (stack, card, target) {
// Are we trying to place a modifier?
if (typeof target != 'undefined' && target != null) {
// Only kings and jacks can be placed as modifiers
switch (card.value) {
case 'king':
case 'jack':
return true; // can go anywhere!
default:
return false;
}
}
// Are we placing a regular card?
if (target == null) {
// No kings or jacks
switch (card.value) {
case 'king':
case 'jack':
return false;
}
var lastCardInStack = stack.cards[stack.cards.length - 1];
if (typeof lastCardInStack !== 'undefined') {
// If the new card isn't the same suit as the last card, it must follow the direction of the stack
if (card.suit != lastCardInStack.suit) {
if (card.value == '1') card.value = 1;
// console.log('Direction analysis: ', stack.direction, card.value, lastCardInStack.value);
// Card can't be of equal value if is a different suit
if (card.value == lastCardInStack.value) return false;
// Going up or down?
if (stack.direction == 'asc') { // Up
// Card must be of greater value than the last one in the stack, queens allow you to change direction
if (card.value != 'queen' && card.value < lastCardInStack.value) return false;
} else if (stack.direction == 'desc') { // Down
// Card must be of lesser value than the last one in the stack, queens allow you to change direction
if (card.value != 'queen' && card.value > lastCardInStack.value) return false;
// Can't place a queen on a stack with no direction yet
}else if (card.value == 'queen' && stack.direction == null){
return false;
}
// console.log('Passed');
}
}
}
return true;
};
/**
* From http://css-tricks.com/snippets/javascript/htmlentities-for-javascript/
* @param str
* @returns {string}
*/
htmlEntities = function (str) {
return String(str).replace(/&/g, '&').replace(/</g, '<').replace(/>/g, '>').replace(/"/g, '"');
};