-
Notifications
You must be signed in to change notification settings - Fork 0
/
jquery.CountingCardGame.js
814 lines (675 loc) · 29.1 KB
/
jquery.CountingCardGame.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
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
//Created by Robert J. Kruhlak
//License: CC-BY
//Inspiration: Elated.com
//Dedicated to: Justine Kruhlak
//Modification of Drag and Drop and Card Style from Elated.com
//See http://www.elated.com/res/File/articles/development/javascript/jquery/
// drag-and-drop-with-jquery-your-essential-guide/card-game.html
;(function($){
// ∇ =∇ ="∇"(do not seem to work in FireFox
var symbols=['♣', '♥', '♠', '♦', '◊', 'Δ', '∞'];
var myNumbers = [ 1, 2, 3, 4, 5, 6,7,8,9,10,11,12,13,14,15, 16,17,18,19,20];
$.fn.countingCardGame = function(options) {
var defaults = {
numbers: myNumbers, // Numbers used for matching and counting
__correctCards: 0, // Initialize to zero, increment when cards are placed correctly
__incorrectCards: 0,
cardColor: 'purple',
cardPileLabel: 'Take a card',
slotsTableColor: 'blue',
lowCard: 1,
highCard: 10,
minCard: 1,
maxCardFaceCols: 4,
maxCardFaceRows: 5,
maxCard: myNumbers.length,
minColumns: 1,
maxColumns: 7,
cardColumns: 4,
symbolIndex: 1,
symbols: symbols,
shufflePictures: true,
shufflePicturesLabel: 'Shuffle Pictures',
shuffleNumbers: true,
shuffleNumbersLabel: 'Shuffle Numbers',
successMessage: 'You did it!',
randIds: 1,
animate: true,
};
if (options){
checkIntOptions(defaults, options);
options.minColumns = checkMinMax(options.minColumns,options.maxColumns);
options.lowCard = checkMinMax(options.lowCard,options.highCard);
}
var options = $.extend(defaults, options);
// support mutltiple elements
if (this.length > 1){
this.each(function() {
$(this).countingCardGame(options)
});
return this;
}
var ids = {
debug: 0,
obj: this.attr('id'),
license: 'CC-BY',
functionPanel: 'functionPanel',
slot: 'slot',
slotsTable: 'slotsTable',
cardTable: 'cardTable',
cardTableRow: 'cardTableRow',
cell: 'cell',
card: 'card',
cardFace: 'cardFace',
cardPile: 'cardPile',
panelList: 'panelList',
shufflePictures: 'shufflePicts',
shuffleNumbers: 'shuffleNums',
shuffleFaces: 'shuffleFaces',
resetButton: 'resetButton',
rangeSlider: 'slider-range',
lowCard: 'low',
highCard: 'high',
columnsSlider: 'columnsSlider',
columnsSliderLabel: 'cardTableColumns',
symbolsSlider: 'symbolsSlider',
symbolsSliderLabel: 'symbol',
successMessage: 'successMessage',
};
//Prepend a random string to ids so more than one game
// can be put on a page. Needs to be done before
// other objects are populated with ids.
if (options.randIds){ ids=prependRandomString(ids);}
var labels = {
cardPile: options.cardPileLabel,
shufflePictures: options.shufflePicturesLabel,
shuffleNumbers: options.shuffleNumbersLabel,
lowCard: 'Low Card',
highCard: 'High Card',
resetButton: 'Reset',
shuffleFaces: 'Suffle Faces',
columnsSlider: 'Columns: ',
symbolsSlider: 'Symbol: ',
};
var symbolsSlider = {
min: 0,
max: (symbols.length-1),
value: options.symbolIndex,
id: ids.symbolsSlider,
labelId: ids.symbolsSliderLabel,
label: labels.symbolsSlider,
symbols: symbols,
};
var columnsSlider = {
min: options.minColumns,
max: options.maxColumns,
value: options.cardColumns,
id: ids.columnsSlider,
labelId: ids.columnsSliderLabel,
label: labels.columnsSlider,
};
var sliders = {'columns': columnsSlider, 'symbols': symbolsSlider};
this.getOptions = function() {
return options;
};
this.printOptions = function(){
alert(dump(options));
};
this.getIds = function() {
return ids;
};
this.getLabels = function() {
return labels;
};
this.getSliders = function() {
return sliders;
};
//Private functions
//UI
// Card faces are added to cardSlots
function addCardFace(id, symbol, opts, ids){
var visArray=[];
for (var i in opts.numbers){
visArray.push(0);
}
// set visible symbols
for (i=0; i<id; i++){
visArray[i]=1;
}
// Randomize the placement of the visible symbols
visArray=fisherYates(visArray);
// need to send number as data for droppable code
$('<table><tbody></tbody></table>').data( 'number', id )
.attr( 'id', ids.cardFace + id )
.appendTo('#' + ids.slot+ id);
// A card face can hold 20 symbols five rows by 4 columns
// is the max that will fit in a slot
for (j=0; j<opts.maxCardFaceRows; j++){
$('<tr></tr>').attr('id', ids.cardFace+ id +'r' + j)
.appendTo('#' + ids.cardFace + id);
for (i = j*opts.maxCardFaceCols; i < ((j+1)*opts.maxCardFaceCols); i++){
$('<td><div>'+symbol+'</div></td>').attr('id', ids.cardFace + id + 'c' + i)
.appendTo('#' + ids.cardFace + id +'r' + j);
// <div> above is necessary because drag and drop doesn't play well with tables.
$('#' + ids.cardFace +id+'c'+i).addClass("hidden");
if (symbol == "♥" || symbol == "♦" || symbol == "◊" || symbol == "Δ" ){
$('#' + ids.cardFace + id + 'c' + i).addClass("red");
}
if (visArray[i]){
$('#' + ids. cardFace + id + 'c' + i).toggleClass("hidden");
}
}
}
};
function addCardSymbol(ind,symbols){
if (parseInt(ind)>=0 && parseInt(ind)< symbols.length ){
return symbols[ind];
}else{
return symbols[0];
}
};
function addCardSlots( picts, ids, opts ){
var myMin = $('#' + ids.lowCard).val();
var myMax = $('#' + ids.highCard).val();
picts = picts.slice((myMin-1),myMax);
var myColumns = $( '#' + ids.columnsSlider ).slider( 'value' );
var myRows = Math.ceil(picts.length/myColumns);
if ($('#' + ids.shufflePictures).prop('checked')){
picts=fisherYates(picts);
}
removeHTML(ids.cardTable);
createCardSlots(picts, myRows, myColumns, ids, opts);
if (ids.debug){ alert($('#' + ids.obj).css('height'))}
};
// Resize the cardTable and functionPanel
function resizeUI(){
//Used when card columns, number of cards change.
var contentPaddingHeight = 50;
var contentPaddingWidth = 80;
var $cardTable = $('table[id$="cardTable"]');
var $functionPanel = $('div[id*="functionPanel"]');
var fpWidth = $functionPanel.outerWidth(true);
var sTableWidth = $cardTable.outerWidth(true);
var contentWidth = fpWidth + sTableWidth +contentPaddingWidth;
var slotWidth = $('.slot').first().width();
var cardFaceWidth = $('table[id*="cardFace"]').first().outerWidth(true);
var cardFaceHeight =$('table[id*="cardFace"]').first().outerHeight(true);
if (cardFaceWidth > slotWidth) {$('.slot').width(cardFaceWidth).height(cardFaceHeight)};
sizeCardPile();
$cardTable.parent().width(sTableWidth);
var maxHeight = matchHeight($cardTable,$functionPanel);
var contentHeight = (maxHeight + contentPaddingHeight);
$cardTable.parent().parent().width(contentWidth).height(contentHeight);
return contentHeight;
};
function createCardSlots( picts, myRows, myColumns, ids, opts){
var pictIndex=0;
var correctCards = opts.__correctCards;
for ( ii = 0; ii < myRows; ii++) {
$('<tr></tr>').attr('id', ids.cardTableRow + ii).appendTo('#' + ids.cardTable);
for ( jj=(ii*(myRows-1)); jj < ((ii*(myRows-1)) + myColumns); jj++ ) {
if (pictIndex < picts.length) {
$('<td></td>').attr('id', ids.cell + pictIndex).appendTo('#' + ids.cardTableRow + ii);
$('<div class="slot"></div>').data( 'number', picts[pictIndex])
.data( 'ids', ids )
.data( 'opts', opts)
.attr( 'id', ids.slot + picts[pictIndex] )
.appendTo( '#' + ids.cell + pictIndex )
.droppable( {
accept: '#' + ids.cardPile + ' div',
hoverClass: 'hovered',
drop: handleCardDrop
} );
var myPict = picts[pictIndex];
addCardFace(myPict, addCardSymbol($( '#' + ids.symbolsSlider ).slider( 'value' ), opts.symbols),
opts,ids);
//$('.slot').width($('#'+ids.cardFace+myPict).outerWidth(true) ); //Width of card face
//$('.slot').height($('#'+ids.cardFace+myPict).outerHeight(true) ); //Height of card face
}
pictIndex=pictIndex+1;
}
}
};
function addCardPile(numbers, ids, opts){
removeHTML(ids.cardPile);
numbers=numbers.slice(($('#' + ids.lowCard).val()-1), $('#' + ids.highCard).val());
numbers=numbers.reverse(); // reverse so 1 starts when unshuffled
if ($('#' + ids.shuffleNumbers).is(':checked')){
numbers=fisherYates(numbers);
}
return createCardPile(numbers, ids, opts);
};
function animateTopCard(myShift,myDuration){
$('.animate').animate({'marginLeft': myShift, 'marginTop': -myShift},myDuration, 'swing')
.animate({'marginLeft':'10px', 'marginTop': '10px'}, myDuration, 'swing', function() {
animateTopCard(myShift,myDuration);});
};
function stopAnimation(){
$('.animate').stop(true,true)
.animate({'marginLeft':'10', 'marginTop' : '10'},1000)
.stop(true,true).removeClass('animate');
};
function addLicense(id){
$('#' + id).html('<a rel="license" href="http://creativecommons.org/licenses/by/3.0/"><img alt="Creative Commons Attribution 3.0 Unported License" style="border-width:0" src="http://upload.wikimedia.org/wikipedia/commons/1/16/CC-BY_icon.svg"></a>').css({'height': '10px', 'padding': '10px'})
};
function createCardPile( numbers, ids, opts ){
var cPId = '#' + ids.cardPile;
var cardId = ids.card;
var topCardId = '#'+cardId+numbers[numbers.length-1];
if (ids.debug){alert($('#' + ids.obj).attr('id'));}
for ( var i in numbers) {
$('<div>' + numbers[i] + '</div>').data( 'number', numbers[i] )
.attr( 'id', cardId + numbers[i] ,'z-index',i,'position','absolute')
.css( {'background': opts.cardColor})
.appendTo( cPId).draggable( {
containment: $('#' + ids.obj),
stack: cPId + ' div',
cursor: 'move',
revert: true
} );
}
//add mousedown so that we can stop the animation on touch screen
//because it does not seen to respond to hover
$(topCardId).addClass('animate').hover(function(){stopAnimation();})
.mousedown(function(){stopAnimation();});
return '#'+cardId+numbers[numbers.length-1];
};
function createFunctionPanel(ids, opts, labels){ // will be the right panel in UI
var cPId=ids.cardPile;
return $('<div></div>').addClass( 'functionPanel')
.attr('id', ids.functionPanel)
.append(myLabel(cPId, labels.cardPile))
.append(myDiv(cPId).addClass('cardPile'))
.append(createPanelList(ids,opts,labels));
};
function createPanelList(ids,opts,labels){
return myList(ids.panelList)
.append(addCheckboxToList(ids.shufflePictures, labels.shufflePictures,opts.shufflePictures))
.append(addCheckboxToList(ids.shuffleNumbers, labels.shuffleNumbers,opts.shuffleNumbers))
.append(addButtonToList(ids.resetButton,labels.resetButton,'resetButton'))
.append(addInputTextboxToList(ids.lowCard, labels.lowCard))
.append(addInputTextboxToList(ids.highCard, labels.highCard))
.append(addDiv(ids.rangeSlider))
.append(addP(ids.symbolsSliderLabel))
.append(addDiv(ids.symbolsSlider))
.append(addP(ids.columnsSliderLabel))
.append(addDiv(ids.columnsSlider))
.append(addDiv(ids.license));
};
function createSuccessMessage(ids, opts){
//need to use mouseup so works on mobile browsers
//that don't seem to handle the click event
$('<div></div>').appendTo($('#' + ids.obj))
.attr('id', ids.successMessage)
.css('position', 'absolute')
.addClass('successMessage')
.mouseup({'ids': ids, 'opts': opts}, handleClick)
.append($('<h2>'+opts.successMessage +'</h2>'))
.append($('<div></div>').attr('id','stats'))
.append( $('<button></button>')
.html('Play Again')
);
hideSuccessMessage(ids);
};
function hideSuccessMessage(ids){ // Hide the success message
var $sMId = $('#' + ids.successMessage);
var $objId = $('#' + ids.obj);
$sMId.hide();
$sMId.css( {
'top': $objId.position().top,
'left': $objId.position().left,
'width': '0',
'height': '0',
} );
};
// Event Handlers
function handleCardDrop( event, ui ) {
var slotNumber = $(this).data( 'number' );
var cardNumber = ui.draggable.data( 'number' );
var ids= $(this).data( 'ids');
var opts = $(this).data('opts');
// currently sent as data
// because handleCardDrop can't take any other parameters
var $sMId =$('#' + ids.successMessage);
// If the card was dropped to the correct slot,
// change the card colour, position it directly
// on top of the slot, and prevent it being dragged
// again
if ( slotNumber == cardNumber ) {
ui.draggable.addClass( 'correct' ).css('border','0');
ui.draggable.draggable( 'disable' );
$(this).droppable( 'disable' );
ui.draggable.position( { of: $(this), my: 'left top', at: 'left top' } );
ui.draggable.draggable( 'option', 'revert', false );
opts.__correctCards++;
} else {
opts.__incorrectCards++;
}
// If all the cards have been placed correctly then display a message
// and reset the cards for another go
if ( opts.__correctCards == ($('#' + ids.highCard).val()-$('#' + ids.lowCard).val()+1)) {
$('#stats').html('Correct: ' + opts.__correctCards + ' Incorrect: ' + opts.__incorrectCards);
$sMId.show();
var $objId = $('#'+ ids.obj);
//animate the success message
$sMId.animate( {
left: $objId.position().left + $objId.width()/4,
top: $objId.position().top + $objId.height()/4,
width: '200px',
height: '120px',
opacity: 1
} );
}
};
var delay = (function(){
var timer = 0;
return function(callback, ms){
clearTimeout (timer);
timer = setTimeout(callback, ms);
};
})();
function matchHeight($class1,$class2){
// match the height of the slotsTable with the functionPanel
// depending on which is longest
var stHeight = $class1.outerHeight(true);
var fpHeight = $class2.outerHeight(true);
if (stHeight >= fpHeight) {
$class2.outerHeight(stHeight);
$class1.parent().outerHeight(stHeight);
return stHeight;
} else {
$class1.parent().outerHeight(fpHeight);
return fpHeight;
}
};
function sizeCardPile(){
// set the size of the card pile to match the slot class size
// set the line height to height so the text is vertically aligned
var $cP = $('.cardPile')
var $slot= $('.slot')
$cP.outerWidth($slot.outerWidth())
$cP.outerHeight($slot.outerHeight())//set to size of slot
$cP.css('line-height',$cP.outerHeight()+'px')
};
function setHandleClick(id, ids, opts){
// ids, and opts needed as parameters for handleClick
// so that the game can be updated properly.
//.on('click', ...) does not work on mobile touch screens
$('#' + id).on('mouseup',{'ids': ids, 'opts': opts}, handleClick);
};
function setHandleClick2(id, ids, opts){
// ids, and opts needed as parameters for handleClick
// so that the game can be updated properly.
//.on('click', ...) does not work on mobile touch screens
$('#' + id).change({'ids': ids, 'opts': opts}, handleClick);
};
function updateSlider() {
var ids = $(this).data('ids');
var opts = $(this).data('opts');
if (ids.debug) {alert( 'Check a value of ids array ' +ids.cardPile);}
if (ids.debug) {alert('Check a value of opts array ' + opts.numbers);}
resetGame(opts,ids);
};
function handleClick(event){
ids=event.data.ids;
opts = event.data.opts;
if (ids.debug){ alert('HandleClick event.data.ids.cardPile=' + ids.cardPile);}
if (ids.debug) {alert('HandleClick event.data.opts.numbers' + opts.numbers);}
resetGame(opts,ids);
};
function resetGame(opts,ids){ // Reset the game
stopAnimation();
hideSuccessMessage(ids,opts);
opts.__correctCards = 0;
opts.__incorrectCards = 0;
//Add card slots before pile so that we can
// use the size for the card pile
addCardSlots(opts.numbers,ids, opts);
//sizeCardPile()
resizeUI();
addCardPile(opts.numbers,ids, opts);
if (opts.animate) {
jQuery.fx.interval = 50;
$('.animate').delay(1000).animate({'marginLeft':'10px'},50);
animateTopCard(-40,2000);
}
};
//Sliders
function createSlider( o, ids, opts){
var $id = $('#'+ o.id).data('ids', ids)
.data('opts', opts);
var $labelId = $('#'+ o.labelId);
$id.slider({
min: o.min,
max: o.max,
value: o.value,
slide: function(event, ui) {
$labelId.html(o.label + ui.value);
},
stop: updateSlider,
});
//Render initial value
$labelId.html( o.label + $id.slider( 'value' ) ); // In this instance it is a <p>
$id.find('a').css({'background':'transparent','border':'none','text-align':'center','text-decoration':'none'}).html('▼');
};
function createSymbolSlider( o, ids, opts){
var $id = $('#'+ o.id).data('ids', ids)
.data('opts', opts);
var $labelId = $('#'+ o.labelId);
$id.slider({
min: o.min,
max: o.max,
value: o.value,
slide: function(event, ui) {
$labelId.html(o.label + addCardSymbol(ui.value, opts.symbols));
},
stop: updateSlider,
});
//Render initial value
$labelId.html( o.label + addCardSymbol( $id.slider( 'value' ), opts.symbols )); // In this instance it is<p>
$id.find('a').css({'background':'transparent','border':'none','text-align':'center','text-decoration':'none'}).html('▲');
};
function createRangeSlider(o,ids, opts){
var $sliderId = $('#' + ids.rangeSlider ).data('ids', ids)
.data('opts', opts);
var $lowId = $( '#' + ids.lowCard );
var $highId = $( '#' + ids.highCard );
$sliderId.slider({
range: true,
min: o.minCard,
values: [ o.lowCard, o.highCard ],
max: o.maxCard,
slide: function( event, ui ) {
$lowId.val( ui.values[ 0 ] );
$highId.val( ui.values[ 1 ] );
},
stop: updateSlider
});
// Render initial values
$lowId.val( $sliderId.slider( "values", 0 )).attr('disabled',true);
$highId.val( $sliderId.slider( "values", 1 )).attr('disabled',true);
$sliderId.find($('a')[0]).css({'background':'transparent no-repeat','border':'none','text-align':'left','text-decoration':'none'}).html('▶');
$sliderId.find($('a')[1]).css({'background':'transparent','border':'none','text-align':'right','text-decoration':'none'}).html('◀');
};
//Utilities
$(function() {
$( "#sortable" ).sortable();
$( "#sortable" ).disableSelection();
});
function dump(obj) {
var out = '';
for (var key in obj) {
out += key + ": " + obj[key] + "\n";
}
return out;
};
function checkMinMax(low,high){
if (low > high){
return high;
}
};
function checkIntOptions(defaults, options){
for (var key in defaults){
switch(key){
case 'minCard':
case 'maxCard':
case 'lowCard':
case 'highCard':
case '__correctCards':
case '__incorrectCards':
case 'minColumns':
case 'maxColumns':
case 'cardColumns':
case 'symbolIndex':
case 'randIds':
if (key in options) {
if (!isInt(options[key])){
options[key]=defaults[key];
console.log(key + ' is not an integer. Reverting to default.');
alert(key + 'is not an integer. Reverting to default.');
} else {
options[key]=Math.abs(options[key]);
}
}
break;
default:
if (key in options){
console.log(key + ' is ' + options[key]);
}
}
}
};
function isInt(val){
//regex options /^[0-9]+$/.test(val);
if((parseFloat(val) == parseInt(val)) && !isNaN(val)){
return true;
} else {
return false;
}
};
function prependRandomString(ids){
var randomString = 'R'+Math.floor(10000*Math.random()).toString();
for (var key in ids){
if (key != 'debug' && key !='obj' && key != 'license'){
ids[key] = randomString + ids[key];
}
}
return ids;
};
function removeHTML(id){
$('#' + id).html( '' ).removeData();
};
function myTable(id){
return $('<table></table>').attr('id', id);
};
function myLabel(id,text){
return $('<label for=' + id + '></label>').html(text);
};
function myDiv(id){
return $('<div></div>').attr( 'id', id);
};
function myP(id){
return $('<p></p>').attr( 'id', id);
};
function myList(id){
return $('<ul></ul>').attr( 'id', id).css({'list-style-type':'none', 'padding': '10px'})
};
function addDiv (id){
return $('<li></li>').append(myDiv(id));
};
function addP(id){
return $('<li></li>').append(myDiv(id));
};
function addCheckboxToList(id,text,checked){
var $in = $('<input type="checkbox" />').prop('checked', checked)
.attr('id',id);
return $('<li></li>').append($in)
.append($('<label for='+id+'>'+text+'</label>'));
};
function addButtonToList(id, text, myClass){
return $('<li></li>').append($('<button></button>').attr('id', id)
.addClass(myClass)
.html(text)
);
};
function addInputTextboxToList( id, text){
return $('<li></li>').append(
$('<label for=id>' + text + '</label>')
).append(
$('<input type="text" id=' + id + ' maxlength="4" size="4"/>')
);
};
function fisherYates(myArray){
//Randomizes the order of the array.
var i = myArray.length;
while(i--){
var j=Math.floor(Math.random()*(i+1));
var tempi=myArray[i];
myArray[i]=myArray[j];
myArray[j]=tempi;
}
return myArray;
};
//Initialization
function initialize(obj){
var opts=obj.getOptions();
var ids = obj.getIds();
var labels = obj.getLabels();
var sliders= obj.getSliders();
obj.addClass('gameContainer');
obj.append(myDiv(ids.slotsTable)
.append(myTable(ids.cardTable)));
$('#'+ids.slotsTable).addClass('slotsTable')
.css({'background': opts.slotsTableColor});
$('#'+ids.cardTable).addClass('cardTable')
.css({'background': 'transparent'});
obj.append(createFunctionPanel(ids, opts, labels));
createSlider( sliders.columns, ids, opts);
createSymbolSlider( sliders.symbols,ids, opts);
createRangeSlider(opts,ids, opts);
createSuccessMessage(ids, opts);
addLicense(ids.license);
setHandleClick(ids.resetButton, ids, opts);
setHandleClick2(ids.shufflePictures, ids, opts);
setHandleClick2(ids.shuffleNumbers, ids, opts);
// Add cards to Table
addCardSlots(opts.numbers, ids, opts);
// Create the pile of shuffled cards
sizeCardPile()
addCardPile(opts.numbers, ids, opts);
// flesh out the game area
// in the event that the game is attached
// to a zero height div.
resizeUI();
var cssObj = {
'text-align': 'center',
'user-select' : 'none',
'width' : $(window).width,
'height' : $(window).height,
};
obj.css(cssObj);
if (opts.animate) {
jQuery.fx.interval = 50;
$('.animate').delay(1000);
animateTopCard(-40,2000);
};
var delay = (function(){
var timer = 0;
return function(callback, ms){
clearTimeout (timer);
timer = setTimeout(callback, ms);
};
}());
$(window).resize(function() {
delay(function(){
//alert('window width = ' +$(this).width() + '\n card slot width = ' + //$('.slot').first().width());
resizeUI();
//alert($('.slot').first().width());
//...
}, 200); //500 is time in miliseconds
}); //Not working properly. Widths returning NaN on resize.
return obj;
};
return initialize(this);
}
})(jQuery);