-
Notifications
You must be signed in to change notification settings - Fork 0
/
code.js
1209 lines (1106 loc) · 740 KB
/
code.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
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
var XURDLE = {};
(function() {
"use strict";
var header, mainCanvas, help, statsDiv, statsCanvas, currentTab;
var problemNumber;
const pi = Math.PI;
var mode;
// "main" grid
var selectedArrow = -1;
var grid;
var reveal;
var sp = 4; // spacing between rows and columns
// row height, column width, line width
var rh, cw;
// "guess" grid
var correct; // word to find during this round of guesses
var guessNumber = 0;
var charIndex = 0;
var totalGuesses = 0;
var guesses = [ ["","","","",""],["","","","",""],["","","","",""],
["","","","",""],["","","","",""],["","","","",""]];
var gStyles = [ ["","","","",""],["","","","",""],["","","","",""],
["","","","",""],["","","","",""],["","","","",""]];
var keyboardGeometry;
var keys = [ [ "Q", "W", "E", "R", "T", "Y", "U", "I", "O", "P" ],
[ "A", "S", "D", "F", "G", "H", "J", "K", "L", "u"],
[ "ENTER", "Z", "X", "C", "V", "B", "N", "M", "b","d"] ];
var wWidth = window.innerWidth // browser window's width
|| document.documentElement.clientWidth
|| document.body.clientWidth;
var wHeight = window.innerHeight // browser window's height
|| document.documentElement.clientHeight
|| document.body.clientHeight;
var width = wWidth - 10;
var height = wHeight;
var cWidth = width;
var cHeight = height - 30;;
var portrait = height > width;
var gw = (5*rh - 6*sp)/6;
/*
while (width > 0.95*wWidth) // adjust to fit window's width
{
rh -= 1;
cw = rh;
gw = (5*rh - 6*sp)/6;
width = sp+cw+sp+5*cw+2*sp+5*(gw+sp);
}
*/
if (portrait)
gw = rh;
rh /= 1.5;
cw /= 1.5;
gw /= 2;
var top=rh, left=sp+cw+sp;
var lw = Math.max(3,0.1 * rh);
//var height = top + 7*rh; // canvas height
//if (portrait)
// height = top + 12*rh;
var wordFound = { "-1":0, 0:0, 1:0, 2:0, 3:0, 4:0, 5:0 };
// colors
var bkgColor = 'white';
var diag2WordColor = "rgba(250,100,100,1)";
var horizWordColor = "rgba(50,120,200,1)";
var diagWordColor = diag2WordColor;
var centerColor = diag2WordColor;
function getFromLS(key)
{
return JSON.parse(window.localStorage.getItem(key));
}
function saveToLS(key,value)
{
return window.localStorage.setItem(key,JSON.stringify(value));
}
function resizeCanvas()
{
}
/******************************************************************
This code, from: https://stackoverflow.com/a/15666143
increases the resolution of canvas drawing by passing
a integer larger than one for ratio in createHiDPICanvas
(see init() and drawStats() for the two canvases in this app)
*******************************************************************/
var PIXEL_RATIO = (function () {
var ctx = document.createElement("canvas").getContext("2d"),
dpr = window.devicePixelRatio || 1,
bsr = ctx.webkitBackingStorePixelRatio ||
ctx.mozBackingStorePixelRatio ||
ctx.msBackingStorePixelRatio ||
ctx.oBackingStorePixelRatio ||
ctx.backingStorePixelRatio || 1;
return dpr / bsr;
})();
var createHiDPICanvas = function(w, h, ratio) {
if (!ratio) { ratio = PIXEL_RATIO; }
var can = document.createElement("canvas");
can.width = w * ratio;
can.height = h * ratio;
can.style.width = w + "px";
can.style.height = h + "px";
can.getContext("2d").setTransform(ratio, 0, 0, ratio, 0, 0);
return can;
}
/*******************************************************************/
function redraw()
{
const ctx = mainCanvas.getContext("2d");
/*
ctx.beginPath();
ctx.fillStyle = "red";
ctx.strokeStyle = "black";
ctx.lineWidth =2;
var width = Math.floor(mainCanvas.style.width.slice(0,-2));
var height = mainCanvas.style.height.slice(0,-2);
console.log(width);
ctx.rect(0, 0,cWidth,cHeight-5);
ctx.fill();
ctx.stroke();
ctx.closePath();
*/
keyboardGeometry = drawKeyboard();
drawGrid();
drawScore();
/*
drawGuesses();
*/
drawHeaderAndFooter();
}
function getKey(canvas, event)
{
const rect = canvas.getBoundingClientRect()
const x = event.clientX - rect.left
const y = event.clientY - rect.top
//console.log("x = " + x + " y = " + y);
const kb = keyboardGeometry;
//console.log("top = " + kb["top"] + " h = " + kb["h"]);
const row = Math.floor((y - kb["top"]) / kb["h"]);
const col = Math.floor((x - kb["left"]) / (kb["w"]+kb["sp"]));
//console.log("row = " + row + " col = " + col);
if (row >= 0 && row <= 2 && col >= 0 && col <= 9)
return keys[row][col];
}
function handleClick(e)
{
const key = getKey(mainCanvas,e);
console.log("key = " + key);
switch(key)
{
case "u":
e.keyCode = 38;
e.key = "ArrowUp";
handleKeyDown( e );
break;
case "d":
e.keyCode = 40;
e.key = "ArrowDown";
break;
case "ENTER":
e.keyCode = 13;
e.key = "Enter";
break;
case "b":
e.keyCode = 8;
e.key = "Backspace";
break;
default:
e.keyCode = key.charCodeAt(0);
e.key = key;
break;
}
handleKeyDown( e );
}
function init()
{
//console.log("avail: " + width + " by " + height);
//console.log("window: " + wWidth + " by " + wHeight);
//document.getElementById("myHeader").height = 100;
//console.log ("hello " + document.getElementById("myHeader"));
document.getElementById("myHeader").style.width = width + "px";
//document.getElementById("myHeader").style.height = 30 + "px";
// game is NOT already loaded: create the game gadgets
if (! mainCanvas)
{
document.addEventListener('keydown', handleKeyDown);
//window.addEventListener('resize', resizeCanvas);
// game header
document.getElementById("titleDiv").style.width = "100%"; //wWidth + "px";
// main game canvas
mainCanvas = createHiDPICanvas(cWidth, cHeight, 4); // higher resolution
mainCanvas.setAttribute("id","canvas");
document.getElementById("canvasDiv").appendChild( mainCanvas );
mainCanvas.addEventListener('click', handleClick);
// stats tab
statsDiv = document.getElementById("stats");
statsCanvas = createHiDPICanvas(width,height,4); // hi resolution!
statsCanvas.setAttribute("id","statsCanvas");
statsDiv.appendChild( statsCanvas );
statsDiv.setAttribute("style","width: " + width + "px");
statsDiv.style.width = width +"px";
// help tab
help = document.getElementById("help");
help.setAttribute("style","width: " + width + "px");
help.style.width = width +"px";
// web counter
var counter = document.getElementById("counter");
//counter.setAttribute("style","width: " + width + "px");
counter.style.width = width +"px";
}// game widgets creation
width = mainCanvas.style.width.slice(0,-2);
height = mainCanvas.style.height.slice(0,-2);
//console.log(width + " by " + height);
currentTab = mainCanvas;
// is local storage empty?
if (! window.localStorage.getItem("numPlayed") )
{ // if so, initialize it
saveToLS("numPlayed",0);
saveToLS("max",0);
saveToLS("min",Number.MAX_SAFE_INTEGER);
saveToLS("sum",0);
for(var s = 5; s <= 49; s++)
saveToLS(s+"",0);
saveToLS("problemNumber",1);
problemNumber = 1;
} else
{ // load config from local storage
//saveToLS("problemNumber",1);
problemNumber = getFromLS("problemNumber");
}
initDataStructures();
redraw();
}// init
function initDataStructures()
{
mode = "grid";
selectedArrow = -1;
guessNumber = 0;
charIndex = 0;
totalGuesses = 0;
guesses = [ ["","","","",""],["","","","",""],["","","","",""],
["","","","",""],["","","","",""],["","","","",""]];
gStyles = [ ["","","","",""],["","","","",""],["","","","",""],
["","","","",""],["","","","",""],["","","","",""]];
wordFound = { "-1":0, 0:0, 1:0, 2:0, 3:0, 4:0, 5:0 };
console.log("pb# = " + problemNumber);
var g = decode( 2*(problemNumber - 1) );
grid = [ ["","","","",""],
["","","","",""],
["","","","",""],
["","","","",""],
["","","","",""] ];
reveal = [ [0,0,0,0,0],[0,0,0,0,0],[0,0,0,0,0],[0,0,0,0,0],[0,0,0,0,0]];
for(var r = 0; r < 5; r++)
for(var c = 0; c < 5; c++)
{
grid[r][c] = g[r].charAt(c);
}
}// initDataStructures()
function showHelp()
{
mainCanvas.style.display = "none";
statsDiv.style.display = "none";
help.style.display = "block";
currentTab = help;
}
function hideHelp()
{
mainCanvas.style.display = "block";
help.style.display = "none";
currentTab = mainCanvas;
}
function showStats()
{
mainCanvas.style.display = "none";
help.style.display = "none";
drawStats();
statsDiv.style.display = "block";
currentTab = stats;
}
function hideStats()
{
mainCanvas.style.display = "block";
statsDiv.style.display = "none";
help.style.display = "none";
currentTab = mainCanvas;
}
function drawKeyboard()
{
if (mainCanvas.getContext)
{
const ctx = mainCanvas.getContext("2d");
ctx.beginPath(); // erase Canvas and do NOT draw border
ctx.fillStyle = "white";
ctx.rect(0,0,width,height);
ctx.fill();
//var cw = canvas.style.width.slice(0,-2); // canvas width
var sp, rowSp, left, top, w, h, x, y, keyboardGeometry;
if (portrait)
{
left = 10;
w = (width - 2*left)/ 10.9;
sp = 0.1*w;
rowSp = 0.2*w;
h = 1.1*w;
top = cHeight - 10 - 3*h - 2*rowSp;
keyboardGeometry =
{ "top": top, "left": left, "w": w, "h": h, "sp":sp };
}
//drawLetter(ctx,portrait+"", 100,100,400,20,
//'black',1,'center');
for( var r = 0; r < keys.length; r++)
for( var i = 0; i < keys[r].length; i++)
{
ctx.beginPath();
var x = left + i * (w+sp);
var y = top + r*(h+rowSp);
ctx.fillStyle = "lightGray";
ctx.roundRect(x, y, w, h,10);
ctx.fill();
switch (keys[r][i])
{
case "u": // up key
drawUpSign(ctx,x + w/2, y+0.35*h, w, h/3,true);
break;
case "d": // down key
drawUpSign(ctx,x + w/2, y+0.65*h, w, h/3,false);
break;
case "b": // backspace/delete
drawDeleteSign(ctx,x + w/2.3, y+0.35*h, w, h/3);
break;
default: // letter keys
drawLetter(ctx,keys[r][i], x + w/2, y+0.65*h, w, h/3,
'black',1,'center');
}
}
return keyboardGeometry;
}
}
function drawUpSign(ctx,x,y,w,h,up)
{
ctx.beginPath();
ctx.lineWith = 1;
ctx.fillStyle = 'black';
ctx.moveTo(x,y);
ctx.lineTo(x+w/6,y+(up? h : -h));
ctx.lineTo(x-w/6,y+(up? h : -h));
ctx.closePath();
ctx.fill();
}
function drawDeleteSign(ctx,x,y,w,h)
{
ctx.beginPath();
ctx.strokeStyle = 'black';
ctx.lineWidth = 8;
ctx.moveTo(x,y);
ctx.lineTo(x+w/3,y);
ctx.lineTo(x+w/3,y+w/3);
ctx.lineTo(x,y+w/3);
ctx.lineTo(x-w/5,y+w/6);
ctx.closePath();
ctx.stroke();
ctx.beginPath();
ctx.strokeStyle = 'black';
ctx.lineWidth = 10;
x -= w/30;
ctx.moveTo(x+w/15,y+w/15);
ctx.lineTo(x+4*w/15,y+4*w/15);
ctx.moveTo(x+4*w/15,y+w/15);
ctx.lineTo(x+w/15,y+4*w/15);
ctx.stroke();
}
function drawStats()
{
var canvas = statsCanvas;
var mid = width / 2;
var h = 13; // font size
var cw = canvas.style.width.slice(0,-2); // canvas width
var ch = canvas.style.height.slice(0,-2); // canvas height
if (canvas.getContext)
{
const ctx = canvas.getContext("2d");
ctx.beginPath();
ctx.fillStyle = 'white';
ctx.strokeStyle = 'black';
ctx.lineWidth = 1;
ctx.rect(1,1,cw- 2,ch-2);
ctx.fill();
ctx.stroke();
var numPlayed = getFromLS("numPlayed");
var max = getFromLS("max");
var min = numPlayed === 0 ? 0 : getFromLS("min");
var sum = getFromLS("sum");
var avg = numPlayed === 0 ? "N/A" :
(Math.round(100*(sum/numPlayed))/100);
var numPlayed = getFromLS("");
var numPlayed = getFromLS("");
// write summary stats
drawLetter(ctx,
"Games played: " + getFromLS("numPlayed") +
" - Scores: min = " + min + ", avg = " + avg
+ ", max = " + max,
mid, h+20, width, h+5, 'green',0,'center');
var l; // bar length
var y = height - 10;
for(var s = 50; s >= 5; s -= 2)
{
var val = getFromLS(s-1);
// =============== left side ====================
// score value
drawLetter(ctx,(s-1)+"", mid-35, y, 25, h, 'green',0,'left');
// bar
l = ( max === 0 || val === 0) ? 2 : (mid-50)*val / max;
ctx.fillStyle = 'gray';
ctx.rect(mid-35-l-2,y-h,l,h+2);
ctx.fill();
// number in bar
var numberFitsInBar = l > 50;
if (numberFitsInBar)
drawLetter(ctx,val+"",
mid-35-l, y-1,
50, h, 'white',1, 'left');
else
drawLetter(ctx,val+"",
mid-35-l-4, y-1,
50, h, 'gray',1,'right');
if (s < 50)
{
val = getFromLS(s);
// =============== right side ====================
// score value
drawLetter(ctx,s+"", mid+5, y, 25, h, 'green',0,'right');
// bar
l = (max === 0 || val === 0) ? 2 : (mid-50)*val/max;
ctx.fillStyle = 'gray';
ctx.rect(mid+5+2,y-h,l,h+2);
ctx.fill();
// number in bar
var numberFitsInBar = l > 50;
if (numberFitsInBar)
drawLetter(ctx,val+"",
mid+5+l, y-1,
50, h, 'white',1, 'right');
else
drawLetter(ctx,val+"",
mid+5+l+4, y-1,
50, h, 'gray',1,'left');
}
// get ready for next row
y -= h + 4;
}
}
}// drawStats
//*******************************************************************
// handle all key presses
//*******************************************************************
function handleKeyDown(event)
{
var code = Number(event.keyCode);
var key = event.key;
//console.log(code + " " + key);
if (currentTab !== mainCanvas)
return;
if (mode === "nextGame?")
{
problemNumber++;
saveToLS("problemNumber",problemNumber);
init();
} else if (mode === "grid")
{
if (key === 'ArrowDown' &&
selectedArrow < 5)
{
var newSelection = selectedArrow + 1;
while (newSelection <= 6 && wordFound[newSelection] === 1)
newSelection++;
if (newSelection != 6)
selectedArrow = newSelection;
}
else if (key === 'ArrowUp' &&
selectedArrow > -1)
{
var newSelection = selectedArrow - 1;
while (newSelection > -2 && wordFound[newSelection] === 1)
newSelection--;
if (newSelection != -2)
selectedArrow = newSelection;
}
else if (code >= 65 && code <= 90)
{
mode = "guess";
correct = getWordInGrid(selectedArrow);
}
if (event.stopPropagation)
event.stopPropagation();
redraw();
}
if (mode === "guess")
{
if (code === 13) // return/enter key
{
if (charIndex === 5) // complete guess
{
var guess = guesses[guessNumber].join("");
if (validGuesses.indexOf(guess.toLowerCase()) < 0 &&
words.indexOf(guess.toLowerCase()) < 0)
{ // not a dictionary word
notInWordList();
setTimeout(function()
{
redraw();
}, 1000);
} else // valid guess
{
var correctGuess = score(guesses[guessNumber]);
totalGuesses++;
if (correctGuess || guessNumber === 5)
{
if (guessNumber === 5) // ran out of guesses
totalGuesses++; // 1 penalty point
updateWordInGrid();
wordFound[selectedArrow] = 1;
if (0 <= selectedArrow && // if not a
selectedArrow <= 4) // diagonal
{
//check one diagonal ...
var count = 0;
for( var i = 0; i < 5; i++ )
count += reveal[i][i];
if (count === 5)
wordFound[-1] = 1;
// ... and the other
count = 0;
for( var i = 0; i < 5; i++ )
count += reveal[4-i][i];
if (count === 5)
wordFound[5] = 1;
}
guessNumber = 0;
gStyles = [ ["","","","",""],["","","","",""],
["","","","",""],["","","","",""],
["","","","",""],["","","","",""]];
guesses = [ ["","","","",""],["","","","",""],
["","","","",""],["","","","",""],
["","","","",""],["","","","",""]];
selectedArrow = -1;
while (selectedArrow < 6 &&
wordFound[selectedArrow] === 1)
selectedArrow++;
if (selectedArrow === 6)
{
gameOver();
return;
} else
{
mode = "grid";
}
} else
{
guessNumber++;
}
charIndex = 0;
redraw();
}
}
} else if (code >= 65 && code <= 90) // letter key
{
if (charIndex < 5)
{
guesses[guessNumber][charIndex] = key.toUpperCase();
charIndex++;
redraw();
}
} else if (code === 8 || code === 46) // backspace or delete
{
if (charIndex > 0)
{
charIndex--;
guesses[guessNumber][charIndex] = "";
redraw();
}
}
}// in guess mode
};
function score(guess)
{
var numCorrectLetters = 0;
var answer = [];
for(var i = 0; i < 5; i++)
answer.push(correct[i].toUpperCase());
//console.log( "answer = " + answer);
//console.log( "guess = " + guess);
// check for green letters first
for(var i = 0; i < 5; i++) // loop on letters of the guess
if (guess[i] == answer[i]) // is it in the correct position
{
answer[i] = '*'; // letter cannot be used in scoring any longer
gStyles[guessNumber][i] = 'g'; // green
numCorrectLetters++;
}
for(var i = 0; i < 5; i++) // loop on letters of the guess
{
// check if c is in the answer but at another location
for(var j = 0; j < 5; j++) // loop on the letters of the answer
if (answer[j] === guess[i])
{
answer[j] = '*'; // letter is now used up for scoring
gStyles[guessNumber][i] = 'y'; // yellow
break;
}
}
//console.log( "score = " + gStyles[guessNumber]);
return numCorrectLetters === 5;
}// score
function notInWordList()
{
if (mainCanvas.getContext)
{
const ctx = mainCanvas.getContext("2d");
var x = left+5*cw+20, y = top + guessNumber*(gw+sp) + 0.1*gw;
ctx.beginPath();
ctx.fillStyle = 'black';
ctx.rect(x+gw/2,y,4*(gw+sp),gw*0.8);
ctx.fill();
drawLetter(ctx,"Not in word list",
x + 2*(gw+sp) + gw/2,y+0.7*gw,
4*gw,0.5*gw,'white');
}
}// notInWordList()
function gameOver()
{
saveToLS("numPlayed", 1 + getFromLS("numPlayed"));
saveToLS("sum", totalGuesses + getFromLS("sum"));
saveToLS(totalGuesses, 1 + getFromLS(totalGuesses));
saveToLS("min", Math.min(totalGuesses, getFromLS("min")));
saveToLS("max", Math.max(totalGuesses, getFromLS("max")));
if (mainCanvas.getContext)
{
const ctx = mainCanvas.getContext("2d");
var x = left+5*cw+20+gw/2, y = top + 0.5*gw;
redraw();
ctx.beginPath();
ctx.fillStyle = 'green';
ctx.rect(x,y,4*(gw+sp),5*(gw+sp));
ctx.fill();
drawLetter(ctx,"Game over!",
x + 2*(gw+sp),y + gw,5*(gw+sp),0.5*gw,
'white');
drawLetter(ctx,"Score: " + totalGuesses,
x + 2*(gw+sp),y + 2*gw,5*(gw+sp),0.5*gw,
'white');
drawLetter(ctx,"Press any key",
x + 2*(gw+sp),y + 4*gw,5*(gw+sp),0.5*gw,
'white');
drawLetter(ctx,"to play again.",
x + 2*(gw+sp),y + 4.5*gw,5*(gw+sp),0.5*gw,
'white');
}
mode = "nextGame?";
}// gameOver
function drawScore()
{
var left, top, width;
if (portrait)
{
left = 2 + Math.floor(cWidth / 2 - 2.5*cw);
top = 0;
width = 5*rh-2;
height = 0.9*rh;
}
if (mainCanvas.getContext)
{
const ctx = mainCanvas.getContext("2d");
ctx.beginPath(); // erase old one
ctx.fillStyle = bkgColor;
//ctx.fillStyle = 'red';
ctx.rect(left,top, width, 0.9*rh);
ctx.fill();
ctx.beginPath();
ctx.textAlign = 'center';
ctx.fillStyle = "black";
ctx.textBaseline = 'bottom';
ctx.font = 0.5*rh + 'px Arial';
var text = "Score: " + totalGuesses;
ctx.fillText(text,left+width/2,top+0.7*rh, width);
}
}// drawScore
function drawHeaderAndFooter()
{
if (mainCanvas.getContext)
{
var width = 5*cw + 5*(gw+sp)- 50;
var height = rh-15;
var topHeader = top - rh;
var topFooter = top + 5*rh + 15;
const ctx = mainCanvas.getContext("2d");
ctx.beginPath(); // erase header and footer
ctx.fillStyle = bkgColor;
ctx.rect(left + 30,topHeader, width, height);
ctx.rect(left + 30, top + 5*rh + 20, width, height);
ctx.fill();
// header
ctx.beginPath();
ctx.lineWidth = 2;
ctx.strokeStyle = "black";
// ctx.fillStyle = "LightCyan";
ctx.fillStyle = "rgb(215, 246, 250)";
myRoundRect(ctx,left + 30,topHeader, width, height, 10, true);
ctx.beginPath();
ctx.textAlign = 'center';
ctx.fillStyle = "black";
ctx.textBaseline = 'bottom';
ctx.font = (0.5*height) + 'px Arial';
var header = "Pick a row or diagonal to guess next";
if (mode === "guess")
header = "Type in your next guess";
ctx.fillText(header,left+30 + width/2,topHeader+0.75*height);
/*
// footer
var footer = "Total guesses: " + totalGuesses;
ctx.beginPath();
ctx.lineWidth = 2;
ctx.strokeStyle = "black";
//ctx.fillStyle = "PeachPuff";
ctx.fillStyle = "rgb(215, 246, 250)";
myRoundRect(ctx,left + 30, topFooter, width, height, 10,true);
ctx.beginPath();
ctx.strokeStyle = diagWordColor;
ctx.fillStyle = "black";
ctx.fillText(footer,left+30 + width/2,topFooter+0.75*height);
ctx.stroke();
*/
}
}// drawHeaderAndFooter
function drawGuesses()
{
var x, y;
if (! portrait)
{
x = left+5*cw+2*sp;
y = top;
} else
{
x = 0.05 * width + rh;
y = top + 6 * rh;
}
if (mainCanvas.getContext)
{
const ctx = mainCanvas.getContext("2d");
for( var r = 0; r < 6; r++)
{
for(var c = 0; c < 5; c++)
{
ctx.beginPath();
ctx.strokeStyle = 'black';
if (gStyles[r][c] === "g")
{
ctx.fillStyle = '#6aaa64'; // green
ctx.fillRect(x + c*(gw+sp),y+r*(gw+sp),gw,gw);
}
else if (gStyles[r][c] === "y")
{
ctx.fillStyle = '#c9b458'; // yellow
ctx.fillRect(x + c*(gw+sp),y+r*(gw+sp),gw,gw);
}
if (guesses[r][c] !== "")
{
drawLetter(ctx,guesses[r][c],
x + c*(gw+sp) + gw/2, y+r*(gw+sp)+gw-0.2*gw,
gw,0.8*gw,
gStyles[r][c] === '' ? 'black' : 'white',true);
}
ctx.beginPath();
if (mode === "guess" && r <= guessNumber)
{
ctx.lineWidth = 1;
ctx.strokeStyle = 'black';
}
else
{
ctx.lineWidth = 1;
ctx.strokeStyle = 'lightgray';
}
ctx.strokeRect(x + c*(gw+sp),y+r*(gw+sp),gw,gw);
}
}
}
}// drawGuesses
function getWordInGrid(arrow) // arrow is the selected one in {-1,0,...,5}
{
switch (arrow)
{
case -1:
return [ grid[0][0], grid[1][1], grid[2][2], grid[3][3],
grid[4][4] ];
case 5:
return [ grid[4][0], grid[3][1], grid[2][2], grid[1][3],
grid[0][4] ];
default: return grid[arrow];
}
}// getWordInGrid
function updateWordInGrid()
{
if (selectedArrow === -1)
{
for( var c = 0; c < 5; c++)
reveal[c][c] = 1;
} else if (selectedArrow === 5)
{
for( var c = 0; c < 5; c++)
reveal[4-c][c] = 1;
} else
{
for( var c = 0; c < 5; c++)
reveal[selectedArrow][c] = 1;
}
}
function decode(code)
{
var hi = codes[code];
var low = codes[code+1];
return [ // watch out: >>> returns a 32-bit integer
words[ Math.floor(hi / Math.pow(2,24))],
words[(hi >>> 12) & 4095 ],
words[hi & 4095], words[(low >>> 12)], words[low & 4095]];
}// decode
function drawGrid()
{
//var r = 10; // radius
var bkgColor = "rgb(220, 220, 255)";
var gray = "rgb(180,180,180)";
var lightgray = "rgb(240,240,240)";
var black = "rgb(0,0,0)";
var white = "rgb(255,255,255)";
var gWidth = 0.9 * cWidth;
var gHeight = keyboardGeometry.top /2;
rh = Math.floor( Math.min( gWidth / 6, gHeight / 6) );
cw = rh;
var lw = 5; // for row and diagonal boxes
var lw2 = 2; // for vertical black lines
//var left = 50 + Math.floor( 0.1*cWidth / 2 );
var left = Math.floor(cWidth / 2 - 2.5*cw);
var top = rh;
var unselected = 'rgb(220,220,250)';
if (mainCanvas.getContext)
{
const ctx = mainCanvas.getContext("2d");
var dx, dy;
ctx.beginPath();
ctx.lineWidth = lw;
ctx.strokeStyle = horizWordColor;
//*********************** horizontal boxes *************************
var row = 0;
for( dy = 0; dy < 5*rh; dy += rh)
{
ctx.beginPath();
ctx.lineCap = "butt";
ctx.strokeStyle = horizWordColor;
if (selectedArrow == row)
ctx.fillStyle = lightgray;
else
ctx.fillStyle = unselected;
//myRoundRect(ctx, left, top + dy, 5*cw, rh, 20);
ctx.rect(left, top + dy, 5*cw, rh);
//ctx.roundRect(left, top + dy, 5*cw, rh, 20);
ctx.fill();
ctx.stroke();
row++;
}
ctx.beginPath(); // black vertical lines in the grid
ctx.lineWidth = lw2;
ctx.strokeStyle = "#000000";
ctx.lineCap = "butt";
for(var c = 1; c < 5; c++)
{
for(var r = 0; r < 5; r++)
{
if ( (r===0 && (c===2 || c===3)) ||
(r===2 && (c===1 || c===4)) ||
(r===4 && (c===2 || c===3)) )
{
ctx.moveTo(left + c*cw, top+r*rh+lw/2);
ctx.lineTo(left + c*cw, top+r*rh+rh-lw/2);
}
}
}
ctx.stroke();
//*********************** diagonal line #1 *************************
var x = left,
y = top;
//grid[0][0]
drawCell(ctx,diagWordColor,
selectedArrow <= 0 ? lightgray : unselected,
left+lw,y+lw,cw-lw-lw2,rh-2*lw,0,
's','s','s','s');
//grid[1][1]
drawCell(ctx,diagWordColor,
selectedArrow === -1 || selectedArrow === 1 ?
lightgray : unselected,
left+cw+lw2,y+rh+lw,cw-lw,rh-2*lw,0,
's','s','s','s');
//grid[2][2]
drawCell(ctx,centerColor,
selectedArrow === -1 || selectedArrow === 2
|| selectedArrow === 5? lightgray : unselected,
left+2*cw+lw2,y+2*rh+lw,cw-lw,rh-2*lw,0,
's','s','s','s');
//grid[3][3]
drawCell(ctx,diagWordColor,
selectedArrow === -1 || selectedArrow === 3 ? lightgray :
unselected,
left+3*cw+lw2,y+3*rh+lw,cw-lw,rh-2*lw,0,
's','s','s','s');
//grid[4][4]
drawCell(ctx,diagWordColor,
selectedArrow === -1 || selectedArrow === 4?
lightgray : unselected,
left+4*cw+lw2,y+4*rh+lw,cw-lw-lw2,rh-2*lw,0,