-
Notifications
You must be signed in to change notification settings - Fork 0
/
game-of-sums.html
1001 lines (899 loc) · 48.5 KB
/
game-of-sums.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
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<!DOCTYPE html>
<html>
<head>
<title>Game of Sums</title>
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<style>
html, body {
background-color: #eef;
width: 100vw;
height: 100vh;
margin: 0;
padding: 0;
border: 0;
font-family: Sans-Serif;
overflow: hidden;
}
button {
display: inline;
margin: 1vw;
padding: 1vw;
border: 1px solid #CCCCCC;
-webkit-box-shadow: #FEFFFF 0px 2px 2px ;
-moz-box-shadow: #FEFFFF 0px 2px 2px ;
box-shadow: #FEFFFF 0px 2px 2px ;
-webkit-border-radius: 10px;
-moz-border-radius: 10px;
border-radius: 10px;
padding: 10px 10px 10px 10px;
text-shadow: 0px 2px 2px rgba(255,255,255,1);
color: #4A4A4A;
background-color: #F7F5F6;
background-image: -webkit-gradient(linear, left top, left bottom, from(#F7F5F6), to(#DDDDDD));
background-image: -webkit-linear-gradient(top, #F7F5F6, #DDDDDD);
background-image: -moz-linear-gradient(top, #F7F5F6, #DDDDDD);
background-image: -ms-linear-gradient(top, #F7F5F6, #DDDDDD);
background-image: -o-linear-gradient(top, #F7F5F6, #DDDDDD);
background-image: linear-gradient(to bottom, #F7F5F6, #DDDDDD);
filter:progid:DXImageTransform.Microsoft.gradient(GradientType=0,startColorstr=#F7F5F6, endColorstr=#DDDDDD);
white-space: normal;
font-size: 3vmin;
}
button:hover{
border:1px solid #ADADAD;
background-color: #E0E0E0;
background-image: -webkit-gradient(linear, left top, left bottom, from(#E0E0E0), to(#BDBBBC));
background-image: -webkit-linear-gradient(top, #E0E0E0, #BDBBBC);
background-image: -moz-linear-gradient(top, #E0E0E0, #BDBBBC);
background-image: -ms-linear-gradient(top, #E0E0E0, #BDBBBC);
background-image: -o-linear-gradient(top, #E0E0E0, #BDBBBC);
background-image: linear-gradient(to bottom, #E0E0E0, #BDBBBC);
filter:progid:DXImageTransform.Microsoft.gradient(GradientType=0,startColorstr=#E0E0E0, endColorstr=#BDBBBC);
}
button:active{
border:1px solid #ADADAD;
background-color: #E0E0E0;
background-image: -webkit-gradient(linear, left top, left bottom, from(#BDBBBC), to(#E0E0E0));
background-image: -webkit-linear-gradient(top, #BDBBBC, #E0E0E0);
background-image: -moz-linear-gradient(top, #BDBBBC, #E0E0E0);
background-image: -ms-linear-gradient(top, #BDBBBC, #E0E0E0);
background-image: -o-linear-gradient(top, #BDBBBC, #E0E0E0);
background-image: linear-gradient(to bottom, #BDBBBC, #E0E0E0);
filter:progid:DXImageTransform.Microsoft.gradient(GradientType=0,startColorstr=#BDBBBC, endColorstr=#E0E0E0);
}
button:disabled{
color: #E0E0E0;
border:1px solid #ADADAD;
background-color: #E0E0E0;
background-image: -webkit-gradient(linear, left top, left bottom, from(#E0E0E0), to(#E0E0E0));
background-image: -webkit-linear-gradient(top, #E0E0E0, #E0E0E0);
background-image: -moz-linear-gradient(top, #E0E0E0, #E0E0E0);
background-image: -ms-linear-gradient(top, #E0E0E0, #E0E0E0);
background-image: -o-linear-gradient(top, #E0E0E0, #E0E0E0);
background-image: linear-gradient(to bottom, #E0E0E0, #E0E0E0);
filter:progid:DXImageTransform.Microsoft.gradient(GradientType=0,startColorstr=#E0E0E0, endColorstr=#E0E0E0);
}
div.dots {
font-weight: bold;
background-color: #eaeaea;
width: 100%;
height: 100%;
-webkit-border-radius: 15px;
-moz-border-radius: 15px;
border-radius: 15px;
text-align: center;
vertical-align: middle;
color: #a00;
display: none;
}
div.english {
background-color: #eaeaea;
width: 100%;
height: 100%;
-webkit-border-radius: 15px;
-moz-border-radius: 15px;
border-radius: 15px;
text-align: center;
vertical-align: middle;
color: #060;
display: none;
}
div.german {
background-color: #eaeaea;
width: 100%;
height: 100%;
-webkit-border-radius: 15px;
-moz-border-radius: 15px;
border-radius: 15px;
text-align: center;
vertical-align: middle;
color: #060;
display: none;
}
div.digits {
background-color: #eaeaea;
width: 100%;
height: 100%;
-webkit-border-radius: 15px;
-moz-border-radius: 15px;
border-radius: 15px;
text-align: center;
vertical-align: middle;
color: #00a;
}
div.card {
position: relative;
float: left;
border: 0.1vw solid #CCCCCC;
margin: 0.3vw;
-webkit-box-shadow: #FEFFFF 0px 2px 2px ;
-moz-box-shadow: #FEFFFF 0px 2px 2px ;
box-shadow: #FEFFFF 0px 2px 2px ;
-webkit-border-radius: 20px;
-moz-border-radius: 20px;
border-radius: 20px;
padding: 0.3vw;
text-shadow: 0px 2px 2px rgba(255,255,255,1);
color: #4A4A4A;
background-color: #F7F5F6;
background-image: -webkit-gradient(linear, left top, left bottom, from(#F7F5F6), to(#DDDDDD));
background-image: -webkit-linear-gradient(top, #F7F5F6, #DDDDDD);
background-image: -moz-linear-gradient(top, #F7F5F6, #DDDDDD);
background-image: -ms-linear-gradient(top, #F7F5F6, #DDDDDD);
background-image: -o-linear-gradient(top, #F7F5F6, #DDDDDD);
background-image: linear-gradient(to bottom, #F7F5F6, #DDDDDD);
filter:progid:DXImageTransform.Microsoft.gradient(GradientType=0,startColorstr=#F7F5F6, endColorstr=#DDDDDD);
white-space: normal;
vertical-align: top;
}
div.card:hover{
border: 0.1vw solid #ADADAD;
background-color: #E0E0E0;
background-image: -webkit-gradient(linear, left top, left bottom, from(#E0E0E0), to(#BDBBBC));
background-image: -webkit-linear-gradient(top, #E0E0E0, #BDBBBC);
background-image: -moz-linear-gradient(top, #E0E0E0, #BDBBBC);
background-image: -ms-linear-gradient(top, #E0E0E0, #BDBBBC);
background-image: -o-linear-gradient(top, #E0E0E0, #BDBBBC);
background-image: linear-gradient(to bottom, #E0E0E0, #BDBBBC);
filter:progid:DXImageTransform.Microsoft.gradient(GradientType=0,startColorstr=#E0E0E0, endColorstr=#BDBBBC);
}
div.card:active{
border: 0.1vw solid #ADADAD;
background-color: #E0E0E0;
background-image: -webkit-gradient(linear, left top, left bottom, from(#BDBBBC), to(#E0E0E0));
background-image: -webkit-linear-gradient(top, #BDBBBC, #E0E0E0);
background-image: -moz-linear-gradient(top, #BDBBBC, #E0E0E0);
background-image: -ms-linear-gradient(top, #BDBBBC, #E0E0E0);
background-image: -o-linear-gradient(top, #BDBBBC, #E0E0E0);
background-image: linear-gradient(to bottom, #BDBBBC, #E0E0E0);
filter:progid:DXImageTransform.Microsoft.gradient(GradientType=0,startColorstr=#BDBBBC, endColorstr=#E0E0E0);
}
div {
text-shadow: 0px 2px 2px rgba(255,255,255,1);
color: #202020;
}
div.table {
position: absolute; left:0; right:0;padding:0; width:100vw; height:100vh; text-align: justify;
}
div.message {
position: relative;
width:78vw;
height:77vh;
left:1%;
top:1%;
padding: 10vh 10vw 10vh 10vw;
font-size: 3vmin;
display: none;
border: 1px solid #CCCCCC;
-webkit-box-shadow: #FEFFFF 0px 2px 2px ;
-moz-box-shadow: #FEFFFF 0px 2px 2px ;
box-shadow: #FEFFFF 0px 2px 2px ;
-webkit-border-radius: 20px;
-moz-border-radius: 20px;
border-radius: 20px;
text-shadow: 0px 2px 2px rgba(255,255,255,1);
color: #4A4A4A;
background-color: #F7F5F6;
background-image: -webkit-gradient(linear, left top, left bottom, from(#F7F5F6), to(#DDDDDD));
background-image: -webkit-linear-gradient(top, #F7F5F6, #DDDDDD);
background-image: -moz-linear-gradient(top, #F7F5F6, #DDDDDD);
background-image: -ms-linear-gradient(top, #F7F5F6, #DDDDDD);
background-image: -o-linear-gradient(top, #F7F5F6, #DDDDDD);
background-image: linear-gradient(to bottom, #F7F5F6, #DDDDDD);
filter:progid:DXImageTransform.Microsoft.gradient(GradientType=0,startColorstr=#F7F5F6, endColorstr=#DDDDDD);
white-space: normal;
vertical-align: top;
}
div.result {
position: relative; float: left; padding: 0.5em; margin: 0.1em;
display: none;
border: 1px solid #CCCCCC;
-webkit-box-shadow: #FEFFFF 0px 2px 2px ;
-moz-box-shadow: #FEFFFF 0px 2px 2px ;
box-shadow: #FEFFFF 0px 2px 2px ;
-webkit-border-radius: 10px;
-moz-border-radius: 10px;
border-radius: 10px;
}
input.text {
font-family: verdana, arial, sansserif;
font-size: 3vmin;
position: relative;
float: left;
border: 0.1px solid #CCCCCC;
margin: 1vw;
padding: 1vw;
-webkit-box-shadow: #FEFFFF 0px 2px 2px ;
-moz-box-shadow: #FEFFFF 0px 2px 2px ;
box-shadow: #FEFFFF 0px 2px 2px ;
-webkit-border-radius: 20px;
-moz-border-radius: 20px;
border-radius: 20px;
text-shadow: 0px 2px 2px rgba(255,255,255,1);
color: #000000;
background-color: #F7F5F6;
}
div.player {
font-size: 3vmin;
padding: 5vw;
position: absolute;
left:33vw;
width:20vw;
top:13vmin;
height: 20vh;
display: none;
-webkit-box-shadow: #FEFFFF 0px 2px 2px ;
-moz-box-shadow: #FEFFFF 0px 2px 2px ;
box-shadow: 0px 0px 50px #505050;
-webkit-border-radius: 30px;
-moz-border-radius: 30px;
border-radius: 30px;
text-shadow: 0px 2px 2px rgba(255,255,255,1);
}
span.stars {
text-shadow: 0px 0px 1px #400;
color: #fb0;
font-size: 5vmin;
}
span.blue {
color: #00c;
}
span.red {
color: #c00;
}
span.green {
color: #0c0;
}
</style>
<script type="text/javascript">
<!--
// debug mode
var _debugMode = false;
// global gameHistory object
var _gameHistory = null;
var _equation = null;
var _globalLock = false;
var _quitFullScreen = false;
var _language = "en";
//______________________________
// Class representing a single player.
// Used to count points, colours and names.
function Player(name, colour, id, max, amount)
{
if (_debugMode)
alert("Player( name=" + name.toString() + ", colour=" + colour.toString() + ", id=" + id.toString(), + ", max=" + max.toString() + ")");
this.name = name;
this.colour = colour;
this.id = id;
this.points = 0;
this.pointString = "";
this.nextPlayer = 0;
this.cards = [];
cardString = "";
cols = Math.round(Math.sqrt(amount+1));
rows = Math.ceil((amount+1) / cols);
var i = 0; // counter for cards
var n = 0; // counter for numbers on cards
finished = false;
while (!finished) {
for (k=0; k<2 && !finished; k++) {
if (Math.random() > 0.5) {
this.cards.push(new Card(n, i, rows, cols, this.id));
cardString += this.cards[i].html;
i++;
if (i>amount) finished = true;
}
}
n = Math.round(i*max/(amount-1));
}
this.openCards = amount;
this.hasPassed = false;
document.getElementById("table_" + this.id).innerHTML = cardString;
document.getElementById("table_" + this.id).style.display = "block";
document.getElementById(this.id).style.backgroundColor = this.colour;
this.hint("");
}
//______________________________
// Adds a point to the player's score, displays a message with the new score and releases the lock
Player.prototype.addPoint = function()
{
// show message
this.points += 1;
this.pointString += " <span class='stars'>★</span>";
message = this.name + ": " + this.points.toString();
if (_language == "en")
message += " points.<br/><br/><br/>" + this.pointString;
if (_language == "de")
message += " Punkte.<br/><br/><br/>" + this.pointString;
document.getElementById(this.id).innerHTML = message;
document.getElementById(this.id).style.display = "block";
// start time out for hiding message
setTimeout( function(id) { document.getElementById(id).style.display = "none"; _globalLock = false;}.bind(0, this.id), 1200 );
};
//______________________________
// Remove all cards that are covered
Player.prototype.clearUp = function()
{
for (i=0; i<this.cards.length; i++) {
if (this.cards[i].state === 'suggested') {
this.cards[i].remove();
this.openCards--;
}
}
};
//______________________________
// Player confirms his choice of number
Player.prototype.confirm = function(what)
{
message = "";
switch (what) {
case 'part1':
case 'part2':
if (_language==="en") this.hint("Choose two numbers that add up to the number on the right and press <i>Ready</i>, or press <i>Pass</i>.");
if (_language==="de") this.hint("Wähle zwei Zahlen sodass die Zahl rechts stimmt und drücke <i>Fertig</i>, oder drücke <i>Passe</i>.");
if (this !== this.nextPlayer)
this.nextPlayer.hint("");
for (var i=0; i<this.cards.length; i++) {
this.cards[i].enable();
if (this.nextPlayer !== this)
this.nextPlayer.cards[i].disable();
}
// enable the pass button and disable the confirm button
document.getElementById("button_confirm").disabled = true;
document.getElementById("button_pass").disabled = false;
break;
case 'solution':
this.hasPassed = false;
if (_language==="en") this.hint("Choose a number for " + this.nextPlayer.name + ".");
if (_language==="de") this.hint("Wähle eine Zahl für " + this.nextPlayer.name + ".");
var open = 0;
var max = Math.round(this.cards.length / 4);
for (var i=this.cards.length-1; i>-1; i--) {
if (this.cards[i].state === "open" && open < max) {
this.cards[i].enable();
open++;
} else {
this.cards[i].disable();
}
if (this.nextPlayer !== this) {
this.nextPlayer.cards[i].disable();
}
}
// disable the pass and confirm buttons
document.getElementById("button_confirm").disabled = true;
document.getElementById("button_pass").disabled = true;
}
}
//______________________________
// Displays help for the player (what to do next)
Player.prototype.hint = function(text)
{
document.getElementById("hint_" + this.id).innerHTML = this.name + "<br/>" + this.pointString + "<br/>" + text;
};
//______________________________
// Query whether player has any open cards left
Player.prototype.isFinished = function()
{
return this.openCards == 0;
}
//______________________________
// Pass - remove all highlights and confirm
Player.prototype.pass = function()
{
// update all cards
this.hasPassed = true;
for (var i=0; i<this.cards.length; i++) {
this.cards[i].suggest(false);
}
// disable the pass and confirm buttons
document.getElementById("button_confirm").disabled = true;
document.getElementById("button_pass").disabled = true;
}
//______________________________
// Player writes a number into the next part of the equation
Player.prototype.suggest = function(what, cardId1, cardId2, cardId3)
{
if (cardId2!==false)
this.cards[cardId2].suggest(false);
if (cardId3!==false)
this.cards[cardId3].suggest(false);
var card = this.cards[cardId1].suggest(this);
if (card !== false) {
switch (what) {
case "part1":
// update texts
if (_language === "en") this.hint("Choose another number and press <i>Ready</i>, or press <i>Pass</i>.");
if (_language === "de") this.hint("Wähle eine weiter Zahl und drücke <i>Fertig</i>, oder drücke <i>Passe</i>.");
// disable the confirm and enable the pass button
document.getElementById("button_confirm").disabled = true;
document.getElementById("button_pass").disabled = false;
break;
case "part2":
// update texts
if (_language === "en") this.hint("If you think your answer is correct, press <i>Ready</i>.");
if (_language === "de") this.hint("Wenn Du meinst, dass die Zahlen richtig sind, drücke <i>Fertig</i>.");
// enable the pass and confirm buttons
document.getElementById("button_confirm").disabled = false;
document.getElementById("button_pass").disabled = false;
break;
case "solution":
// update texts
if (_language === "en") this.hint("Press <i>Ready</i> or choose a different number.");
if (_language === "de") this.hint("Drücke <i>Fertig</i> or wähle eine andere Zahl.");
// enable the confirm and disable the pass buttons
document.getElementById("button_confirm").disabled = false;
document.getElementById("button_pass").disabled = true;
}
}
return card;
};
//______________________________
// class for a single number card
// A memory card is represented by two divs with the outside one representing the card
// and the inside one representing the face. The face div has a class according to the
// type: <div class="card"><div class="digits">23</div></div>
// both divs have distinct IDs
function Card(meaning, cardIndex, rows, cols, playerId)
{
this.state = "open";
this.meaning = meaning; // the actual number that's on the card
this.faceId = cardIndex.toString() + "_" + playerId + "_face"; // the respective div id, e.g. "3_face"
this.index = cardIndex;
this.cardId = cardIndex.toString() + "_" + playerId + "_card"; // the respective div id for the card, e.g. "3_card"
this.string = meaning.toString();
// determine sizes for width, left, top and height
width = Math.floor(38.0 / cols);
left = (50.0 - cols*(width+0.7)) / 2.0;
height = Math.floor(47.0 / rows);
fontSize = 12 / cols;
lineHeight = 30 / cols;
// alert("width: " + width + ", left: " + left);
// hack for bug in Chrome on Android
if (navigator.userAgent.indexOf("Chrome") > -1 && navigator.userAgent.indexOf("Android") > -1) {
factor = 0.86;
height *= factor;
fontSize *= factor;
lineHeight *= factor;
}
this.html = "<div class=\"card\" id=\"" + this.cardId + "\" style=\"width:" + width.toString() + "vw; height:" + height.toString() + "vh; left:" + left.toString() + "vw;\" onClick=\"_gameHistory.suggest('" + playerId + "', " + this.index.toString() + ");\">"
+ "<div class=\"digits\" id=\"" + this.faceId + "\" style=\"font-size:" + fontSize.toString() + "vw; line-height:" + lineHeight.toString() + "vh;\">"
+ this.string
+ "</div>"
+ "</div>";
}
//______________________________
// remove the card from the table
Card.prototype.remove = function()
{
this.state = "removed";
document.getElementById(this.cardId).style.visibility = "hidden";
};
//______________________________
// disable a card
Card.prototype.disable = function()
{
if (this.state === "open") {
this.state = "disabled";
document.getElementById(this.faceId).style.color = "#bbb";
}
};
//______________________________
// disable a card
Card.prototype.enable = function()
{
if (this.state === "disabled") { // don't set suggested cards back to open
this.state = "open";
document.getElementById(this.faceId).style.color = "#00a";
}
};
//______________________________
// highlight a card
Card.prototype.suggest = function(player)
{
index = false;
colour = false;
if (player===false && this.state==="suggested") {
this.state = "open";
colour = "#eaeaea";
}
if (player!==false && this.state==="open") {
index = this.index;
this.state = "suggested";
colour = player.colour;
}
document.getElementById(this.faceId).style.backgroundColor = colour;
return index;
};
//______________________________
// gameHistory object, tracks open cards, removes cards and assigns points
// player1,2: players
function GameHistory(player1, player2)
{
if (_debugMode) alert("GameHistory( player1=" + player1.toString() + ", player2=" + player2.toString() + ")");
this.player1 = player1;
this.player2 = player2;
if (player2) {
this.player1.nextPlayer = this.player2;
this.player2.nextPlayer = this.player1;
this.currentPlayer = this.player1;
this.nextPlayer = this.player2;
} else {
this.player1.nextPlayer = this.player1;
this.currentPlayer = this.player1;
this.nextPlayer = this.player1;
}
// the equation: part1 + part2 = solution
this.part1 = false;
this.part2 = false;
this.solution = false;
this.nextMove = 'solution';
this.currentPlayer.confirm("solution");
}
//______________________________
// a confirm button has been clicked
GameHistory.prototype.confirm = function()
{
switch (this.nextMove) {
case 'solution':
this.nextPlayer = this.currentPlayer;
this.currentPlayer = this.currentPlayer.nextPlayer;
this.nextMove = 'part1';
break;
case 'part1':
case 'part2':
// calculating points for current player
if (this.currentPlayer.cards[this.part1].meaning + this.currentPlayer.cards[this.part2].meaning == this.nextPlayer.cards[this.solution].meaning)
this.currentPlayer.addPoint();
this.nextMove = 'solution';
}
this.currentPlayer.confirm(this.nextMove);
this.player1.clearUp();
if (this.player2)
this.player2.clearUp();
if (this.player1.isFinished() || (!this.player2 && this.player2.isFinished()))
this.finishGame();
};
//______________________________
// a pass button has been clicked
GameHistory.prototype.pass = function()
{
this.part1 = false;
this.part2 = false;
this.solution = false;
this.nextMove = "solution";
this.currentPlayer.pass();
this.currentPlayer.confirm(this.nextMove);
this.currentPlayer.hasPassed = true;
this.updateGui();
if (this.player1.isFinished() || (!this.player2 && this.player2.isFinished()))
this.finishGame();
}
//______________________________
// a card has been clicked -- make a move
GameHistory.prototype.suggest = function(player, cardIndex)
{
if (this.currentPlayer.id == player) {
if (!_globalLock) {
_globalLock = true;
switch (this.nextMove) {
case 'part1':
this.part1 = this.currentPlayer.suggest(this.nextMove, cardIndex, this.part1, this.part2);
this.part2 = false;
this.updateGui();
this.nextMove = 'part2';
break;
case 'part2':
this.part2 = this.currentPlayer.suggest(this.nextMove, cardIndex, false, false);
this.updateGui();
this.nextMove = 'part1';
break;
case 'solution':
this.part1 = false;
this.part2 = false;
this.solution = this.currentPlayer.suggest(this.nextMove, cardIndex, this.solution, false);
this.updateGui();
}
_globalLock = false;
}
}
};
//______________________________
// a card has been clicked -- make a move
GameHistory.prototype.updateGui = function()
{
// find parts
var part1 = false;
var part2 = false;
var solution = false;
if (this.part1===false && this.part2===false) {
part1 = this.part1!==false ? this.nextPlayer.cards[this.part1].meaning : false;
part2 = this.part2!==false ? this.nextPlayer.cards[this.part2].meaning : false;
solution = this.solution!==false ? this.currentPlayer.cards[this.solution].meaning : false;
}
else {
part1 = this.part1!==false ? this.currentPlayer.cards[this.part1].meaning : false;
part2 = this.part2!==false ? this.currentPlayer.cards[this.part2].meaning : false;
solution = this.solution!==false ? this.nextPlayer.cards[this.solution].meaning : false;
}
// assemble and write equation
document.getElementById("equation").innerHTML = "<span class='red'>" + (part1!==false ? part1 : " ") + "</span>" + " + " + "<span class='green'>" + (part2!==false ? part2 : " ") + "</span>" + " = " + "<span class='blue'>"+ (solution!==false ? solution : " ") + "</span>";
// assemble part1 and part2 dots
part1 = this.part1!==false ? part1 : 0;
part2 = this.part2!==false ? part2 : 0;
partString = "<span class='red'>";
for (var i=0; i < part1+part2; i++) {
if (i === part1) {
partString += "</span><span class='green'>";
}
if (i % 5 === 0)
partString += " ";
if (i % 10 === 0)
partString += " ";
partString += "•";
}
partString += "</span><br/>";
// assemble solution dots
solution = solution!==false ? solution : 0;
solutionString = "<span class='blue'>";
for (var i=0; i<solution; i++) {
if (i % 5 === 0)
solutionString += " ";
if (i % 10 === 0)
solutionString += " ";
solutionString += "•";
}
solutionString += "</span>";
document.getElementById("helper").innerHTML = partString + solutionString;
};
//______________________________
// start a new game --
// create a new gameHistory, create new cards, shuffle them
// and put them face down on the table
function newGame(max)
{
if (_debugMode) alert("newGame(max=" + max.toString() + ")");
// read player 2 (if missing, we're in single player mode)
name = document.getElementById("name2_" + _language).value.trim();
colour = document.getElementById("name2_" + _language).style.backgroundColor;
amount = max;
if (name=="") {
player2 = false;
single = true;
document.getElementById("table_player1").style.width = "70vw";
document.getElementById("table_player1").style.left = "10vw";
amount = Math.floor(2*max/3)*3; // the largest multiple of three that is smaller than twice max
} else
player2 = new Player(name, colour, "player2", max, amount);
// read player 1
name = document.getElementById("name1_" + _language).value.trim();
colour = document.getElementById("name1_" + _language).style.backgroundColor;
player1 = new Player(name, colour, "player1", max, amount);
// create history
helperSize = 90 / max;
document.getElementById("helper").style.fontSize = helperSize.toString()+"vw";
_gameHistory = null;
_gameHistory = new GameHistory(player1, player2);
}
//______________________________
// start a new game --
// create a new gameHistory, create new cards, shuffle them
// and put them face down on the table
GameHistory.prototype.finishGame = function()
{
resultsTable = "<table>"
resultsTable += "<tr><td> " + this.player1.name
+ " </td><td> " + this.player1.points
+ " </td><td> " + this.player1.pointString
+ " </td></tr>";
if (this.player2) {
resultsTable += "<tr><td> " + this.player2.name
+ " </td><td> " + this.player2.points
+ " </td><td> " + this.player2.pointString
+ " </td></tr>";
}
resultsTable += "</table>";
document.getElementById('results_' + _language).innerHTML = resultsTable;
proceed("table", "finish_" + _language);
}
//______________________________
// go to full screen
function goFullScreen()
{
if (_quitFullScreen) {
clearTimeout(_quitFullScreen);
_quitFullScreen = false;
} else {
if (document.documentElement.requestFullScreen) {
document.documentElement.requestFullScreen();
} else if (document.documentElement.mozRequestFullScreen) {
document.documentElement.mozRequestFullScreen();
} else if (document.documentElement.webkitRequestFullScreen) {
document.documentElement.webkitRequestFullScreen(Element.ALLOW_KEYBOARD_INPUT);
} else if (document.documentElement.msRequestFullScreen) {
document.documentElement.msRequestFullScreen();
}
}
}
//______________________________
// stop full screen
function quitFullScreen()
{
if (document.cancelFullScreen) {
document.cancelFullScreen();
} else if (document.mozCancelFullScreen) {
document.mozCancelFullScreen();
} else if (document.webkitCancelFullScreen) {
document.webkitCancelFullScreen();
} else if (document.msCancelFullScreen) {
document.msCancelFullScreen();
}
_quitFullScreen = false;
}
//______________________________
// start the session
function startSession(max, language)
{
_language = language;
if (_language==="en") document.getElementById("button_confirm").innerHTML = "Ready";
if (_language==="de") document.getElementById("button_confirm").innerHTML = "Fertig";
if (_language==="en") document.getElementById("button_pass").innerHTML = "Pass";
if (_language==="de") document.getElementById("button_pass").innerHTML = "Passe";
goFullScreen();
newGame(max);
}
//______________________________
// hides the first id and shows the second
function proceed(hide, show)
{
document.getElementById(hide).style.display = "none";
document.getElementById(show).style.display = "block";
}
//-->
</script>
</head>
<body>
<!-- Card table -->
<div id="table" class="table">
<div id="hint_player1" style="position: relative; float: left; width:20vw; padding: 2vh 2vw 0 2vw; height:36vh; font-size:2.5vh;"></div>
<div style="position: relative; float: left; width:50vw; height:38vh;">
<div id="equation" style="position: relative; float: left; width:50vw; height:14vh;font-size:10vh; text-align: center;"></div>
<div style="position: relative; float: left; width:50vw; height:10vh; line-height:4vh; text-align: center;">
<div id="helper" style="display: inline-block; text-align: left; font-size: 8vh;"></div>
</div>
<div style="position: relative; float: left; width:50vw; height: 14vh; text-align: center;">
<button id="button_confirm" onClick="_gameHistory.confirm()" disabled>Ready</button>
<button id="button_pass" onClick="_gameHistory.pass()" disabled>Pass</button>
</div>
</div>
<div id="hint_player2" style="position: relative; float: left; width:20vw; padding: 2vh 2vw 0 2vw; height:36vh;font-size:2.5vh;"></div>
<div id="table_player1" style="position: relative; float: left; width:48vw; height: 60vh; display: none;"></div>
<div id="table_player2" style="position: relative; float: left; width:48vw; height: 60vh; display: none;"></div>
</div>
<!-- Language chose -->
<div id="language" class="message" style="display:block;">
<button onClick="proceed('language', 'intro_en');" style="clear:left;">English</button>
<button onClick="proceed('language', 'intro_de');" style="clear:left;">Deutsch</button>
</div>
<!-- English workflow -->
<div id="intro_en" class="message">
<b>Game of Sums</b> <br>
Challenge your opponent to a game of sums! Players play in turns.
<ul>
<li>The first player chooses a number and presses <i>Ready</i>.</li>
<li>The second player chooses two numbers that add up to the number from the first player and presses <i>Ready</i>. If the equation is correct, the second player gets a point.</li>
<li>The second player chooses a number for the first player and presses <i>Ready</i>. Then the first player finds two numbers to complete the equation.</li>
<li>If there are no numbers available that would complete a sum, the player presses <i>Pass</i>.</li>
</ul>
If one player has no numbers left or if both players pass, the game is finished.
The winner is the player with most points.<br/>
<br/><br/>
If you enter only one name you'll be playing on your own. Try to choose your numbers in a clever way and use up all your cards without having to pass once.
<div>
<button onClick="proceed('intro_en', 'names_en');">Next</button> or <button onClick="proceed('intro_en', 'language');">Go Back</button>
</div>
</div>
<div id="names_en" class="message">
Enter two players
<br/>
<input id="name1_en" type="text" class="text" style="clear:left; background-color: #fff090"></input>
<input id="name2_en" type="text" class="text" style="clear:left; background-color: #b0fff0"></input>
<br/><br/>
<div>
<button onClick="proceed('names_en', 'start_en');">Next</button> or <button onClick="proceed('names_en', 'intro_en');">Go Back</button>
</div>
</div>
<div id="start_en" class="message">
Choose your game size and start
<br/>
<button onClick="startSession(10,'en'); proceed('start_en', 'table');" style="clear:left;">All numbers up to 10</button><br/>
<button onClick="startSession(15,'en'); proceed('start_en', 'table');" style="clear:left;">All numbers up to 15</button><br/>
<button onClick="startSession(20,'en'); proceed('start_en', 'table');" style="clear:left;">All numbers up to 20</button><br/>
<button onClick="startSession(30,'en'); proceed('start_en', 'table');" style="clear:left;">All numbers up to 30</button><br/>
<button onClick="startSession(50,'en'); proceed('start_en', 'table');" style="clear:left;">All numbers up to 50</button>
<br/><br/>
<div>
Or <button onClick="proceed('start_en', 'names_en');">Go Back</button>
</div>
</div>
<div id="finish_en" class="message">
Final Results
<br/><br/>
<span id="results_en"></span>
<br/><br/>
<button onClick="proceed('finish_en', 'names_en');" style="clear:left;">New Game</button>
<br/>
</div>
<!-- German workflow -->
<div id="intro_de" class="message">
<b>Additions-Wettkampf</b> <br>
Fordere Deinen Gegner zu einem Summenwettkampf heraus! Die Spieler spielen abwechselnd.
<ul>
<li>Der erste Spieler wählt eine Zahl und drückt <i>Fertig</i>.</li>
<li>Der zweite Spiele wählt zwei Zahlen, deren Summe die Zahl des ersten Spielers ergibt und drückt <i>Fertig</i>. Wenn die Gleichung korrekt ist, bekommt der zweite Spieler einen Punkt.</li>
<li>Der zweite Spieler wählt eine Zahl und drückt <i>Fertig</i>. Dann wählt der erste Spieler zwei Zahlen, die die Gleichung vervollständigen.</li>
<li>Wenn es keine passenden Zahlen mehr gibt, drückt der Spieler <i>Passe</i>.</li>
</ul>
Wenn ein Spieler keine Karten mehr hat, oder wenn beide Spieler hintereinander passen, ist das Spiel beendet. Sieger ist der Spieler mit den meisten Punkten.<br/>
<br/><br/>
Wenn Du nur einen Namen einträgst, spielst Du alleine. Versuche so geschickt zu spielen, daß Du alle Karten aufbrauchst ohne zu passen.
<div>
<button onClick="proceed('intro_de', 'names_de');">Weiter</button> oder <button onClick="proceed('intro_de', 'language');">Zurück</button>
</div>
</div>
<div id="names_de" class="message">
Tragt ein oder zwei Spieler ein
<br/>
<input id="name1_de" type="text" class="text" style="clear:left; background-color: #fff090"></input>
<input id="name2_de" type="text" class="text" style="clear:left; background-color: #b0fff0"></input>
<br/><br/>
<div>
<button onClick="proceed('names_de', 'start_de');">Weiter</button> oder <button onClick="proceed('names_de', 'intro_de');">Zurück</button>
</div>
</div>
<div id="start_de" class="message">
Wählt eine Spielgröße
<br/>
<button onClick="startSession(10,'de'); proceed('start_de', 'table');" style="clear:left;">Alle Zahlen bis 10</button><br/>
<button onClick="startSession(15,'de'); proceed('start_de', 'table');" style="clear:left;">Alle Zahlen bis 15</button><br/>
<button onClick="startSession(20,'de'); proceed('start_de', 'table');" style="clear:left;">Alle Zahlen bis 20</button><br/>
<button onClick="startSession(30,'de'); proceed('start_de', 'table');" style="clear:left;">Alle Zahlen bis 30</button><br/>
<button onClick="startSession(50,'de'); proceed('start_de', 'table');" style="clear:left;">Alle Zahlen bis 50</button>
<br/><br/>
<div>
Oder <button onClick="proceed('start_de', 'names_de');">Zurück</button>
</div>
</div>
<div id="finish_de" class="message">
Ergebnis
<br/><br/>
<span id="results_de"></span>
<br/><br/>
<button onClick="proceed('finish_de', 'names_de');" style="clear:left;">Neues Spiel</button>
<br/>
</div>
<!-- Player message boxes -->
<div id="player1" class="player"></div>
<div id="player2" class="player"></div>
</body>