-
Notifications
You must be signed in to change notification settings - Fork 0
/
set-old.html
908 lines (843 loc) · 35.2 KB
/
set-old.html
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
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
<!DOCTYPE html>
<html>
<head>
<title>The Game of Set</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<meta charset="utf-8" />
<link
href="https://fonts.googleapis.com/css?family=Roboto+Condensed:400,400i,700,700i"
rel="stylesheet"
/>
<style>
* {
--card-width: 150px;
}
@media (orientation: landscape) {
* {
--card-width: 11vw;
}
}
@media (orientation: portrait) {
* {
--card-width: 22vw;
}
}
* {
--card-height: calc(var(--card-width) / 0.6);
--card-radius: calc(var(--card-width) / 7.5);
--card-outline: calc(var(--card-width) / 15);
font-family: "Roboto Condensed", sans-serif;
font-size: 18px;
margin: 0;
padding: 0;
}
h1 {
font-size: 30px;
font-weight: 700;
font-style: italic;
}
h4 {
font-weight: 700;
}
section {
margin: 0;
padding: 0;
padding: 10px 0;
width: 100%;
text-align: center;
}
.info_container {
display: inline-block;
width: 20%;
}
.button {
background: lightgray;
display: inline-flex;
justify-content: center;
align-items: center;
border-radius: 5px;
width: 80px;
height: 40px;
font-weight: 700;
text-decoration: none;
border: none;
vertical-align: bottom;
}
.button:hover {
cursor: pointer;
opacity: 0.75;
}
#undealt_section .card_spot {
position: fixed;
left: 0;
top: 100vh;
}
#discarded_section .card_spot {
position: fixed;
left: 100vw;
top: 100vh;
}
#dealt_section {
text-align: center;
}
#game_over {
display: none;
justify-content: center;
align-items: center;
position: fixed;
left: 0;
top: 0;
right: 0;
bottom: 0;
z-index: 10;
}
.card_spot {
display: inline-block;
position: static;
width: var(--card-width);
height: var(--card-height);
margin: calc(var(--card-width) / 25);
border-radius: var(--card-radius);
box-shadow: none;
transition: box-shadow 1s;
}
.card_button {
display: inline-block;
width: var(--card-width);
height: var(--card-height);
border-radius: var(--card-radius);
position: fixed;
padding: 0;
background: none;
border: none;
outline: none;
}
.card_button[data-selected="false"]:before {
border: none;
}
.card_button[data-selected="true"]:before {
content: "";
display: block;
position: absolute;
top: 0;
bottom: 0;
left: 0;
right: 0;
border-radius: var(--card-radius);
border: var(--card-outline) solid black;
}
.card_button:hover {
transform: scale(1.025);
cursor: pointer;
}
.card_button:focus {
box-shadow: 0 0 var(--card-radius) black;
}
</style>
<script>
window.onload = function() {
game = new Game();
};
// game controller object
function Game() {
// card property values
this.numbers = ["one", "two", "three"];
this.fills = ["hollow", "striped", "solid"];
this.colors = ["red", "green", "blue"];
this.shapes = ["rectangle", "oval", "diamond"];
// decks
this.undealt = [];
this.dealt = [];
this.discarded = [];
// clock
this.time = 0;
// show hint if haven't made a set in X seconds
this.hintTime = 60;
this.lastSet = 0;
// transition time in ms
this.transition = 300;
// start game
this.initDeck();
this.attachHandlers();
this.dealNew(); // deal first 12 cards
}
// CHEAT - automatically find and submit set every 100 ms
Game.prototype.cheat = function() {
var _this = this;
new Timer(
function() {
var set = _this.findSets(_this.dealt);
if (set.length > 0)
_this.submitSet(set[0][0], set[0][1], set[0][2]);
},
100,
-1
);
};
// make 81 card deck and shuffle
Game.prototype.initDeck = function() {
for (var number of this.numbers)
for (var fill of this.fills)
for (var color of this.colors)
for (var shape of this.shapes)
this.undealt.push(
new Card(number, fill, color, shape, this)
);
this.undealt.shuffle();
// move all cards to #undealt section
for (var card of this.undealt) card.undeal();
this.moveCards(this.undealt);
};
// attach actions to window events
Game.prototype.attachHandlers = function() {
var _this = this;
window.onscroll = function() {
_this.moveCards(_this.undealt);
_this.moveCards(_this.dealt);
_this.moveCards(_this.discarded);
};
window.onresize = function() {
_this.moveCards(_this.undealt);
_this.moveCards(_this.dealt);
_this.moveCards(_this.discarded);
};
document.body.onclick = function(event) {
_this.clickPage(event);
};
document.getElementById("sort_button").onclick = function() {
_this.sort();
};
this.timer = new Timer(
function() {
_this.tickTimer();
},
1000,
-1
); // start clock
};
// deselect all cards
Game.prototype.deselectCards = function() {
for (var card of this.undealt) card.deselect();
for (var card of this.dealt) card.deselect();
for (var card of this.discarded) card.deselect();
};
// deal new cards. run on game start and on set submit
Game.prototype.dealNew = function() {
var _this = this;
var dealCount = 0;
if (
this.findSets(this.dealt.concat(this.undealt)).length === 0
) {
// if there are no sets left
// deal any remaining cards and trigger game over
dealCount = this.undealt.length;
new Timer(function() {
_this.gameOver();
}, this.transition * dealCount);
} else {
// continue dealing 3 cards at a time until there is a set and at least 12 cards on table
while (
this.dealt.length + dealCount < 12 ||
this.findSets(
this.dealt.concat(
this.undealt.slice(
this.undealt.length - dealCount
)
)
).length === 0
)
dealCount += 3;
}
new Timer(
function() {
_this.dealCard();
},
this.transition,
dealCount
);
};
// take card off top of undealt and move to dealt
Game.prototype.dealCard = function() {
var card = this.undealt.pop();
if (card === undefined) return;
this.dealt.push(card);
card.deal();
this.updateInfo();
this.moveCards(this.dealt, this.transition);
};
// take card off dealt and move to discarded
Game.prototype.discardCards = function(cards) {
for (var card of cards) {
this.dealt.remove(card);
this.discarded.push(card);
card.discard();
}
this.updateInfo();
this.moveCards(
this.dealt.concat(this.discarded),
this.transition * 2
);
};
// run move() on given pile of cards
Game.prototype.moveCards = function(pile, duration) {
for (var card of pile) card.move(duration);
};
// check if three cards are a set
Game.prototype.checkSet = function(cardA, cardB, cardC) {
var allSameOrAllDifferent = function(a, b, c) {
if (
(a !== b && b !== c && c !== a) ||
(a === b && b === c && c === a)
)
return true;
else return false;
};
if (
allSameOrAllDifferent(
cardA.number,
cardB.number,
cardC.number
) &&
allSameOrAllDifferent(cardA.fill, cardB.fill, cardC.fill) &&
allSameOrAllDifferent(
cardA.color,
cardB.color,
cardC.color
) &&
allSameOrAllDifferent(cardA.shape, cardB.shape, cardC.shape)
)
return true;
else return false;
};
// find all sets in given pile of cards
Game.prototype.findSets = function(pile) {
var sets = [];
for (var a = 0; a < pile.length; a++)
for (var b = 0; b < pile.length; b++)
for (var c = 0; c < pile.length; c++)
if (c > b && b > a)
if (this.checkSet(pile[a], pile[b], pile[c]))
sets.push([pile[a], pile[b], pile[c]]);
return sets;
};
// if the three selected cards are a set, remove them and deal new cards. otherwise deselect them
Game.prototype.submitSet = function(cardA, cardB, cardC) {
var _this = this;
if (this.checkSet(cardA, cardB, cardC)) {
// reset hint timer
this.lastSet = this.time;
this.hideHint();
new Timer(function() {
_this.discardCards([cardA, cardB, cardC]);
}, this.transition / 2);
new Timer(function() {
_this.dealNew();
}, this.transition);
} else {
new Timer(function() {
_this.deselectCards();
}, this.transition / 2);
}
};
// stop timer and show game over message
Game.prototype.gameOver = function() {
this.timer.stop();
document.getElementById("game_over").style.display = "flex";
};
// increment clock
Game.prototype.tickTimer = function() {
this.time++;
this.updateInfo();
// trigger hint if it has been too long since last set made
if (this.time - this.lastSet >= this.hintTime) this.showHint();
};
// seconds to 00:00 format
Game.prototype.timeToString = function(time) {
if (typeof time !== "number") return;
var padZeros = function(string, digits) {
while (string.length < digits) string = "0" + string;
return string;
};
var minutes = padZeros(String(Math.floor(time / 60)), 2);
var seconds = padZeros(String(Math.floor(time) % 60), 2);
return minutes + ":" + seconds;
};
// update header info
Game.prototype.updateInfo = function() {
document.getElementById(
"time_display"
).innerHTML = this.timeToString(this.time);
document.getElementById("set_made").innerHTML = Math.floor(
this.discarded.length / 3
);
document.getElementById("cards_left").innerHTML = String(
this.undealt.length + this.dealt.length
);
};
// if player clicks off cards, deselect all
Game.prototype.clickPage = function(event) {
if (!event.path.contains("className", "card_button"))
this.deselectCards();
};
// each time a card is clicked, if three cards selected, submit them as a set
Game.prototype.cardClicked = function() {
var selectedCards = [];
for (var card of this.dealt)
if (card.selected) selectedCards.push(card);
if (selectedCards.length >= 3)
this.submitSet(
selectedCards[0],
selectedCards[1],
selectedCards[2]
);
};
// sort dealt cards by properties
Game.prototype.sort = function() {
var _this = this;
var cardComparison = function(cardA, cardB) {
var numberDiff =
_this.numbers.indexOf(cardA.number) -
_this.numbers.indexOf(cardB.number);
var fillDiff =
_this.fills.indexOf(cardA.fill) -
_this.fills.indexOf(cardB.fill);
var colorDiff =
_this.colors.indexOf(cardA.color) -
_this.colors.indexOf(cardB.color);
var shapeDiff =
_this.shapes.indexOf(cardA.shape) -
_this.shapes.indexOf(cardB.shape);
// sort by number, then fill, then color, then shape
if (numberDiff !== 0) return numberDiff;
else if (fillDiff !== 0) return fillDiff;
else if (colorDiff !== 0) return colorDiff;
else if (shapeDiff !== 0) return shapeDiff;
else return 0;
};
this.dealt.sort(cardComparison);
// remove cards from dealt, then re-add them in new order
for (var card of this.dealt) card.undeal();
for (var card of this.dealt) card.deal();
// reset hint spot glow
this.hideHint();
this.moveCards(this.dealt, this.transition);
};
// find a set and start making it glow
Game.prototype.showHint = function() {
var set = this.findSets(this.dealt)[0];
for (var card of set)
card.spot.style.boxShadow =
"0 0 calc(var(--card-radius) * 3) #9c27b0";
};
// stop any glowing cards
Game.prototype.hideHint = function() {
for (var card of this.dealt) card.spot.style.boxShadow = "";
for (var card of this.discarded) card.spot.style.boxShadow = "";
};
// card template object
function Card(number, fill, color, shape, game) {
this.number = number;
this.fill = fill;
this.color = color;
this.shape = shape;
this.game = game;
this.spot;
this.button;
this.svg;
this.make();
}
// generate svg and other containing DOM elements for card
Card.prototype.make = function() {
// card appearance
var cardWidth = 150;
var cardHeight = 250;
var cardColor = "white";
var cardStrokeColor = "black";
var cardStrokeWidth = 4;
var cardRadius = 20;
var glyphWidth = 110;
var glyphHeight = 60;
var glyphSpacing = 70;
var glyphStrokeWidth = 6;
var glyphRed = "#f43636";
var glyphGreen = "#4caf50";
var glyphBlue = "#3074D4";
var stripeWidth = 4;
var stripeSpacing = 10;
// generic make html or svg element
var makeElement = function(type, svg) {
return document.createElementNS(
svg
? "http://www.w3.org/2000/svg"
: "http://www.w3.org/1999/xhtml",
type
);
};
// make svg of glyph with origin (0,0) at center of shape
var makeGlyph = function(fill, color, shape) {
// compute some geometry
var ovalRadius = Math.min(
glyphWidth / 2 - glyphStrokeWidth / 2,
glyphHeight / 2 - glyphStrokeWidth / 2
);
var diamondMiterX =
glyphStrokeWidth /
2 /
Math.sin(Math.atan(glyphHeight / glyphWidth));
var diamondMiterY =
glyphStrokeWidth /
2 /
Math.sin(Math.atan(glyphWidth / glyphHeight));
// convert enumerated color (eg 'red') to hex color
switch (color) {
case "red":
color = glyphRed;
break;
case "green":
color = glyphGreen;
break;
case "blue":
color = glyphBlue;
break;
}
// glyph svg group
var glyph = makeElement("g", true);
// set glyph color
glyph.setAttribute("stroke", color);
glyph.setAttribute("fill", color);
// make glyph outlines
if (shape === "rectangle") {
var path = makeElement("rect", true);
path.setAttribute(
"x",
-glyphWidth / 2 + glyphStrokeWidth / 2
);
path.setAttribute(
"y",
-glyphHeight / 2 + glyphStrokeWidth / 2
);
path.setAttribute(
"width",
glyphWidth - glyphStrokeWidth
);
path.setAttribute(
"height",
glyphHeight - glyphStrokeWidth
);
} else if (shape === "oval") {
var path = makeElement("rect", true);
path.setAttribute(
"x",
-glyphWidth / 2 + glyphStrokeWidth / 2
);
path.setAttribute(
"y",
-glyphHeight / 2 + glyphStrokeWidth / 2
);
path.setAttribute(
"width",
glyphWidth - glyphStrokeWidth
);
path.setAttribute(
"height",
glyphHeight - glyphStrokeWidth
);
path.setAttribute("rx", ovalRadius);
path.setAttribute("ry", ovalRadius);
} else if (shape === "diamond") {
var path = makeElement("polygon", true);
var diamondPoints = [];
diamondPoints.push(
-glyphWidth / 2 + diamondMiterX + ",0"
);
diamondPoints.push(
"0," + (-glyphHeight / 2 + diamondMiterY)
);
diamondPoints.push(
glyphWidth / 2 - diamondMiterX + ",0"
);
diamondPoints.push(
"0," + (glyphHeight / 2 - diamondMiterY)
);
diamondPoints = diamondPoints.join(" ");
path.setAttribute("points", diamondPoints);
}
// set glyph outline thickness
path.setAttribute("stroke-width", glyphStrokeWidth);
// set glyph fill to none. otherwise, fill defaults to parent group fill
if (fill === "hollow" || fill === "striped")
path.setAttribute("fill", "none");
// create stripe lines
if (fill === "striped") {
var stripes = [];
// start in middle of glyph (0,0) and go to right edge
for (
var x = 0;
x < glyphWidth / 2 - glyphStrokeWidth;
x += stripeSpacing
) {
var y;
// calculate y height at x position depending on shape
if (shape === "rectangle")
y = glyphHeight / 2 - glyphStrokeWidth / 2;
else if (shape === "oval") {
if (x < glyphWidth / 2 - ovalRadius)
y = glyphHeight / 2 - glyphStrokeWidth / 2;
else {
var w =
ovalRadius -
(glyphWidth / 2 -
glyphStrokeWidth / 2 -
x);
var angle = Math.acos(w / ovalRadius);
y = ovalRadius * Math.sin(angle);
}
} else if (shape === "diamond")
y =
glyphHeight / 2 -
diamondMiterY -
(x * glyphHeight) / glyphWidth;
// add stripe
stripes.push({ x1: x, y1: -y, x2: x, y2: y });
// mirror stripe to other side as well. avoid duplicate in middle
if (x > 0)
stripes.push({ x1: -x, y1: -y, x2: -x, y2: y });
}
// add stripes to glyph group
for (var stripe of stripes) {
var line = makeElement("line", true);
line.setAttribute("x1", stripe.x1);
line.setAttribute("x2", stripe.x2);
line.setAttribute("y1", stripe.y1);
line.setAttribute("y2", stripe.y2);
line.setAttribute("stroke-linecap", "round");
line.setAttribute("stroke-width", stripeWidth);
glyph.appendChild(line);
}
}
// add outline to glyph
glyph.appendChild(path);
return glyph;
};
// make svg of card with origin (0,0) at center of card
var makeCard = function(number, fill, color, shape) {
var svg = makeElement("svg", true);
svg.setAttribute(
"viewBox",
[
-cardWidth / 2,
-cardHeight / 2,
cardWidth,
cardHeight
].join(" ")
);
// make card background and outline
var path = makeElement("rect", true);
path.setAttribute(
"x",
-cardWidth / 2 + cardStrokeWidth / 2
);
path.setAttribute(
"y",
-cardHeight / 2 + cardStrokeWidth / 2
);
path.setAttribute("width", cardWidth - cardStrokeWidth);
path.setAttribute("height", cardHeight - cardStrokeWidth);
path.setAttribute("rx", cardRadius - cardStrokeWidth / 2);
path.setAttribute("ry", cardRadius - cardStrokeWidth / 2);
path.setAttribute("fill", cardColor);
path.setAttribute("stroke", cardStrokeColor);
path.setAttribute("stroke-width", cardStrokeWidth);
svg.appendChild(path);
// add glyphs to card
switch (number) {
case "one":
var glyph1 = makeGlyph(fill, color, shape);
svg.appendChild(glyph1);
break;
case "two":
var glyph1 = makeGlyph(fill, color, shape);
var glyph2 = makeGlyph(fill, color, shape);
glyph1.setAttribute(
"transform",
"translate(0 -" + glyphSpacing / 2 + ")"
);
glyph2.setAttribute(
"transform",
"translate(0 " + glyphSpacing / 2 + ")"
);
svg.appendChild(glyph1);
svg.appendChild(glyph2);
break;
case "three":
var glyph1 = makeGlyph(fill, color, shape);
var glyph2 = makeGlyph(fill, color, shape);
var glyph3 = makeGlyph(fill, color, shape);
glyph2.setAttribute(
"transform",
"translate(0 -" + glyphSpacing + ")"
);
glyph3.setAttribute(
"transform",
"translate(0 " + glyphSpacing + ")"
);
svg.appendChild(glyph1);
svg.appendChild(glyph2);
svg.appendChild(glyph3);
break;
}
return svg;
};
// create container elements and attach together
this.spot = document.createElement("div");
this.button = document.createElement("button"); // make button for tabbing control accesibility
this.svg = makeCard(
this.number,
this.fill,
this.color,
this.shape
);
this.spot.className = "card_spot";
this.button.className = "card_button";
this.button.appendChild(this.svg);
this.spot.appendChild(this.button);
// attach card click handler
var _this = this;
this.button.onclick = function() {
_this.click();
};
};
// move card to #undealt section
Card.prototype.undeal = function() {
this.spot.remove();
document
.getElementById("undealt_section")
.appendChild(this.spot);
this.button.tabIndex = -1; // make sure user can't tab to cards that are off screen
this.deselect();
};
// move card to #dealt section
Card.prototype.deal = function() {
this.spot.remove();
document.getElementById("dealt_section").appendChild(this.spot);
this.button.tabIndex = 1; // allow user tabbing control
};
// move card to #discarded section
Card.prototype.discard = function() {
this.spot.remove();
document
.getElementById("discarded_section")
.appendChild(this.spot);
this.button.tabIndex = -1; // make sure user can't tab to cards that are off screen
this.deselect();
};
// move card button element (fluid) to card spot element (static) over transition duration
Card.prototype.move = function(duration) {
var duration = duration || 0;
duration /= 1000;
this.button.style.transition =
"left " + duration + "s, top " + duration + "s";
this.button.style.left =
String(this.spot.getBoundingClientRect().left) + "px";
this.button.style.top =
String(this.spot.getBoundingClientRect().top) + "px";
};
// toggle card selected and send game controller object notification that card has been clicked
Card.prototype.click = function() {
if (this.selected) this.deselect();
else this.select();
this.game.cardClicked();
};
// select card, adding data attribute for css styling
Card.prototype.select = function() {
this.selected = true;
this.button.dataset.selected = "true";
};
// deselect card
Card.prototype.deselect = function() {
this.selected = false;
this.button.dataset.selected = "false";
};
// self correcting timer
function Timer(func, delay, repeat) {
this.func = func;
this.delay = delay;
this.repeat = repeat;
if (!this.repeat || typeof this.repeat !== "number")
this.repeat = 0;
this.count = 0;
this.startTime = performance.now();
if (repeat === 0) return;
var _this = this;
this.timeout = window.setTimeout(function() {
_this.tick();
}, this.delay);
}
Timer.prototype.tick = function() {
this.func();
this.count++;
if (
this.repeat === -1 ||
(this.repeat > 0 && this.count < this.repeat)
) {
var adjustedDelay = Math.max(
1,
this.startTime +
(this.count + 1) * this.delay -
performance.now()
);
var _this = this;
this.timeout = window.setTimeout(function() {
_this.tick();
}, adjustedDelay);
}
};
Timer.prototype.stop = function() {
window.clearTimeout(this.timeout);
};
// array helpers
Array.prototype.shuffle = function() {
for (var index = this.length - 1; index >= 1; index--) {
var randomIndex = Math.floor(Math.random() * (index + 1));
var temp = this[randomIndex];
this[randomIndex] = this[index];
this[index] = temp;
}
return this;
};
Array.prototype.contains = function(property, value) {
for (var step of this)
if (step[property] === value) return true;
return false;
};
Array.prototype.remove = function(item) {
var index = this.indexOf(item);
if (index > -1) this.splice(index, 1);
return index;
};
</script>
</head>
<body>
<section>
<div class="info_container">
<h4>Time</h4>
<span id="time_display">00:00</span>
</div>
<div class="info_container">
<h4>Sets Made</h4>
<span id="set_made">0</span>
</div>
<div class="info_container">
<h4>Cards Left</h4>
<span id="cards_left">81</span>
</div>
<div class="info_container">
<button class="button" id="sort_button">Sort</button>
</div>
</section>
<section id="dealt_section"></section>
<section id="undealt_section"></section>
<section id="discarded_section"></section>
<section id="game_over"><h1>Game Over!</h1></section>
</body>
</html>