Repository navigation
Expand file tree
/
Copy pathApp.js
More file actions
1524 lines (1468 loc) · 40.1 KB
/
Copy pathApp.js
File metadata and controls
1524 lines (1468 loc) · 40.1 KB
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
import React, { Component } from "react";
import "./App.css";
import Home from "./Components/Home";
import AppTitle from "./Components/AppTitle";
import Numbers from "./Components/Numbers";
import TimerOnScreen from "./Components/TimerOnScreen";
import TimerStart from "./Components/TimerStart";
import shuffleArray from "./Functions/ShuffleArray";
import sumTheArrayGrid from "./Functions/ArrayGridSum";
import checkForValue from "./Functions/CheckforValue";
//
class App extends Component {
timer = "";
valid = false;
state = {
so: 0,
st: 0,
sm: 0,
tm: 0,
solving: false,
timerCount: 0,
difficulty: ["Easy", "Medium", "Hard"],
difficultyIndex: 0,
colors: [
"black",
"red",
"blue",
"purple",
"pink",
"green",
"orange",
"yellow"
],
backTracker: {
/*Purpose is to locate the exact spot of the grid. Purpose of backTracker
is to iterate through the pointers to the grid to make the change. I will also decrement
to redo previous if there is a problem with a current position */
1: [0, 0],
2: [0, 1],
3: [0, 2],
4: [0, 3],
5: [0, 4],
6: [0, 5],
7: [0, 6],
8: [0, 7],
9: [0, 8],
10: [1, 0],
11: [1, 1],
12: [1, 2],
13: [1, 3],
14: [1, 4],
15: [1, 5],
16: [1, 6],
17: [1, 7],
18: [1, 8],
19: [2, 0],
20: [2, 1],
21: [2, 2],
22: [2, 3],
23: [2, 4],
24: [2, 5],
25: [2, 6],
26: [2, 7],
27: [2, 8],
28: [3, 0],
29: [3, 1],
30: [3, 2],
31: [3, 3],
32: [3, 4],
33: [3, 5],
34: [3, 6],
35: [3, 7],
36: [3, 8],
37: [4, 0],
38: [4, 1],
39: [4, 2],
40: [4, 3],
41: [4, 4],
42: [4, 5],
43: [4, 6],
44: [4, 7],
45: [4, 8],
46: [5, 0],
47: [5, 1],
48: [5, 2],
49: [5, 3],
50: [5, 4],
51: [5, 5],
52: [5, 6],
53: [5, 7],
54: [5, 8],
55: [6, 0],
56: [6, 1],
57: [6, 2],
58: [6, 3],
59: [6, 4],
60: [6, 5],
61: [6, 6],
62: [6, 7],
63: [6, 8],
64: [7, 0],
65: [7, 1],
66: [7, 2],
67: [7, 3],
68: [7, 4],
69: [7, 5],
70: [7, 6],
71: [7, 7],
72: [7, 8],
73: [8, 0],
74: [8, 1],
75: [8, 2],
76: [8, 3],
77: [8, 4],
78: [8, 5],
79: [8, 6],
80: [8, 7],
81: [8, 8]
},
colorsIndex: 0,
grid: [],
board: [],
boardArray: [],
group1: [],
group2: [],
group3: [],
group4: [],
group5: [],
group6: [],
group7: [],
group8: [],
group9: [],
col1: [],
col2: [],
col3: [],
col4: [],
col5: [],
col6: [],
col7: [],
col8: [],
col9: [],
row1: [],
row2: [],
row3: [],
row4: [],
row5: [],
row6: [],
row7: [],
row8: [],
row9: [],
cellClicked: false,
currentCell: {}, //upon clicked will set currentCell to the cell clicked
numberClicked: false,
currentNumber: false,
gameOver: false
};
componentWillMount() {
this.createBoard();
}
setUpPlayingBoard = () => {
/*This function will set up the game board.
If difficulty is set to easy 45 cells of the grid will be revealed.
If difficulty is set to medium 27 cells of the grid will be revealed.
If difficulty is set to hard 18 cells of the grid will be revealed
*/
let revealing = 0;
switch (Number(this.state.difficultyIndex)) {
case 0:
revealing = 45;
break;
case 1:
revealing = 27;
break;
case 2:
revealing = 18;
break;
default:
console.log(
"shouldn't have a index over two",
this.state.difficultyIndex
);
}
const revealingIndexs = [];
// this.state.solving ? [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]
while (revealingIndexs.length < revealing) {
const j = Math.ceil(Math.random() * 81);
if (revealingIndexs.includes(j) === false) {
revealingIndexs.push(j);
}
}
const board = [];
for (let i = 0; i < this.state.grid.length; i++) {
board[i] = this.state.grid[i].slice();
}
// removed back tracker so make backTracker equal this.state.backTracker
const indexToGroup = {
1: "group1",
2: "group1",
3: "group1",
4: "group2",
5: "group2",
6: "group2",
7: "group3",
8: "group3",
9: "group3",
10: "group1",
11: "group1",
12: "group1",
13: "group2",
14: "group2",
15: "group2",
16: "group3",
17: "group3",
18: "group3",
19: "group1",
20: "group1",
21: "group1",
22: "group2",
23: "group2",
24: "group2",
25: "group3",
26: "group3",
27: "group3",
28: "group4",
29: "group4",
30: "group4",
31: "group5",
32: "group5",
33: "group5",
34: "group6",
35: "group6",
36: "group6",
37: "group4",
38: "group4",
39: "group4",
40: "group5",
41: "group5",
42: "group5",
43: "group6",
44: "group6",
45: "group6",
46: "group4",
47: "group4",
48: "group4",
49: "group5",
50: "group5",
51: "group5",
52: "group6",
53: "group6",
54: "group6",
55: "group7",
56: "group7",
57: "group7",
58: "group8",
59: "group8",
60: "group8",
61: "group9",
62: "group9",
63: "group9",
64: "group7",
65: "group7",
66: "group7",
67: "group8",
68: "group8",
69: "group8",
70: "group9",
71: "group9",
72: "group9",
73: "group7",
74: "group7",
75: "group7",
76: "group8",
77: "group8",
78: "group8",
79: "group9",
80: "group9",
81: "group9"
};
const indexToColumn = {
1: "col1",
2: "col2",
3: "col3",
4: "col4",
5: "col5",
6: "col6",
7: "col7",
8: "col8",
9: "col9",
10: "col1",
11: "col2",
12: "col3",
13: "col4",
14: "col5",
15: "col6",
16: "col7",
17: "col8",
18: "col9",
19: "col1",
20: "col2",
21: "col3",
22: "col4",
23: "col5",
24: "col6",
25: "col7",
26: "col8",
27: "col9",
28: "col1",
29: "col2",
30: "col3",
31: "col4",
32: "col5",
33: "col6",
34: "col7",
35: "col8",
36: "col9",
37: "col1",
38: "col2",
39: "col3",
40: "col4",
41: "col5",
42: "col6",
43: "col7",
44: "col8",
45: "col9",
46: "col1",
47: "col2",
48: "col3",
49: "col4",
50: "col5",
51: "col6",
52: "col7",
53: "col8",
54: "col9",
55: "col1",
56: "col2",
57: "col3",
58: "col4",
59: "col5",
60: "col6",
61: "col7",
62: "col8",
63: "col9",
64: "col1",
65: "col2",
66: "col3",
67: "col4",
68: "col5",
69: "col6",
70: "col7",
71: "col8",
72: "col9",
73: "col1",
74: "col2",
75: "col3",
76: "col4",
77: "col5",
78: "col6",
79: "col7",
80: "col8",
81: "col9"
};
let startIndex = 1;
const boardArray = [];
while (startIndex < 82) {
const tempObj = {};
const row = this.state.backTracker[startIndex][0];
const col = this.state.backTracker[startIndex][1];
if (revealingIndexs.includes(startIndex)) {
tempObj.changable = false;
tempObj.value = board[row][col];
tempObj.repeated = false;
} else {
tempObj.changable = true;
tempObj.value = " ";
tempObj.repeated = false;
board[row][col] = " ";
}
tempObj.row = `row${row + 1}`;
tempObj.col = indexToColumn[startIndex];
tempObj.group = indexToGroup[startIndex];
tempObj.index = startIndex; //used to know exactly which cell to get back to
tempObj.clicked = false; // used to know if the cell is clicked or not.
boardArray.push(tempObj);
startIndex += 1;
} //end of while loop
this.setState(
{ solving: false },
this.putGroupsTogether(board, boardArray)
);
// this.putGroupsTogether(board, boardArray);
};
putGroupsTogether = (board, boardArray) => {
/*This function will put the groups for the grid together */
const group1 = [];
const group2 = [];
const group3 = [];
const group4 = [];
const group5 = [];
const group6 = [];
const group7 = [];
const group8 = [];
const group9 = [];
const col1 = [];
const col2 = [];
const col3 = [];
const col4 = [];
const col5 = [];
const col6 = [];
const col7 = [];
const col8 = [];
const col9 = [];
const row1 = [];
const row2 = [];
const row3 = [];
const row4 = [];
const row5 = [];
const row6 = [];
const row7 = [];
const row8 = [];
const row9 = [];
for (let obj of boardArray) {
switch (obj.group) {
case "group1":
group1.push(obj);
break;
case "group2":
group2.push(obj);
break;
case "group3":
group3.push(obj);
break;
case "group4":
group4.push(obj);
break;
case "group5":
group5.push(obj);
break;
case "group6":
group6.push(obj);
break;
case "group7":
group7.push(obj);
break;
case "group8":
group8.push(obj);
break;
case "group9":
group9.push(obj);
break;
default:
console.log(obj.group, "is not recognized");
}
switch (obj.col) {
case "col1":
col1.push(obj);
break;
case "col2":
col2.push(obj);
break;
case "col3":
col3.push(obj);
break;
case "col4":
col4.push(obj);
break;
case "col5":
col5.push(obj);
break;
case "col6":
col6.push(obj);
break;
case "col7":
col7.push(obj);
break;
case "col8":
col8.push(obj);
break;
case "col9":
col9.push(obj);
break;
default:
console.log(obj.col, "is not recognized");
}
switch (obj.row) {
case "row1":
row1.push(obj);
break;
case "row2":
row2.push(obj);
break;
case "row3":
row3.push(obj);
break;
case "row4":
row4.push(obj);
break;
case "row5":
row5.push(obj);
break;
case "row6":
row6.push(obj);
break;
case "row7":
row7.push(obj);
break;
case "row8":
row8.push(obj);
break;
case "row9":
row9.push(obj);
break;
default:
console.log(obj.row, "is not recognized");
}
}
this.setState({
// cellClicked: false,
// numberClicked: false,
solving: false,
board,
boardArray,
group1,
group2,
group3,
group4,
group5,
group6,
group7,
group8,
group9,
col1,
col2,
col3,
col4,
col5,
col6,
col7,
col8,
col9,
row1,
row2,
row3,
row4,
row5,
row6,
row7,
row8,
row9
});
};
shuffle = array => {
/*Calls ShuffleArray and will will return an array and a boolean inside of an array */
let arrayAndValidStatus;
if (array) {
arrayAndValidStatus = shuffleArray(array);
this.valid = arrayAndValidStatus[1];
return arrayAndValidStatus[0];
}
};
valueNeeded = (section, toBeAdded, from) => {
/*Checks to see if value is needed in section
section should be an array and toBeAdded should be an integer
returns true or false performs a check for if section is an array. */
return checkForValue(section, toBeAdded, from);
};
sumTheGrid = (one, two, three, four, five, six, seven, eight, nine) => {
/* This function will use sumArray on each of the paramaters. Paramaters should be array objects
Performs checks if each paramater is an array.
*/
return sumTheArrayGrid(
one,
two,
three,
four,
five,
six,
seven,
eight,
nine
);
};
createBoard = () => {
/*This will create the game board */
console.log("new board");
const grid = [
[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, 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, 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, 0, 0, 0, 0, 0, 0]
];
const maxInput = 405;
const orginalNumbers = [1, 2, 3, 4, 5, 6, 7, 8, 9];
let numbersIndex = 0; //purpose is to be able to go through this group of numbers
let numbers = this.shuffle(orginalNumbers); // shuffles the orginalNumbers.
//purpose of the shuffle is so that the top row isn't 1-9;
let backTrackerIndex = 1; //Starts back track.
/*Start at the backTrackerIndex 1 check to see if the associated groups columns and rows need the value at numbersIndex.
If a value is not needed try all avaiable values before back tracking.
If unable to find a needed value:
backtrack (decrement) the backTrackerIndex reset to a new number.
If we are successful at setting a new number, move forward increment backTrackerIndex.
*/
while (
this.sumTheGrid(
grid[0],
grid[1],
grid[2],
grid[3],
grid[4],
grid[5],
grid[6],
grid[7],
grid[8],
grid[9]
) !== maxInput
// ||
//this.state.backTracker > 81 //This line was done on accdient but doesn't make sense
//when i put it to the correct && backTrackerIndex > 81 it doesn't work need to research this.
//however right now doesn't seem to be needed.
) {
/*There are three neccessary areas to check
horizontal sections which go east and west, or side to side.
We will also need to check vertically north and south or top to bottom.
We will also need to check the 3 cells by 3 cells sections of the board.
If a number is already in this section then we do not have a valid number to add to this section.
*/
//The gird will already have the rows avaialble to us.
//Groups of 9 boxes 3 cells by 3 cells.
// [][][]
// [][][]
// [][][]
const group1 = [
grid[0][0],
grid[0][1],
grid[0][2],
grid[1][0],
grid[1][1],
grid[1][2],
grid[2][0],
grid[2][1],
grid[2][2]
];
const group2 = [
grid[0][3],
grid[0][4],
grid[0][5],
grid[1][3],
grid[1][4],
grid[1][5],
grid[2][3],
grid[2][4],
grid[2][5]
];
const group3 = [
grid[0][6],
grid[0][7],
grid[0][8],
grid[1][6],
grid[1][7],
grid[1][8],
grid[2][6],
grid[2][7],
grid[2][8]
];
const group4 = [
grid[3][0],
grid[3][1],
grid[3][2],
grid[4][0],
grid[4][1],
grid[4][2],
grid[5][0],
grid[5][1],
grid[6][2]
];
const group5 = [
grid[3][3],
grid[3][4],
grid[3][5],
grid[4][3],
grid[4][4],
grid[4][5],
grid[5][3],
grid[5][4],
grid[6][5]
];
const group6 = [
grid[3][6],
grid[3][7],
grid[3][8],
grid[4][6],
grid[4][7],
grid[4][8],
grid[5][6],
grid[5][7],
grid[6][8]
];
const group7 = [
grid[6][0],
grid[6][1],
grid[6][2],
grid[7][0],
grid[7][1],
grid[7][2],
grid[8][0],
grid[8][1],
grid[8][2]
];
const group8 = [
grid[6][3],
grid[6][4],
grid[6][5],
grid[7][3],
grid[7][4],
grid[7][5],
grid[8][3],
grid[8][4],
grid[8][5]
];
const group9 = [
grid[6][6],
grid[6][7],
grid[6][8],
grid[7][6],
grid[7][7],
grid[7][8],
grid[8][6],
grid[8][7],
grid[8][8]
];
//vertical up and down.
const column1 = [
grid[0][0],
grid[1][0],
grid[2][0],
grid[3][0],
grid[4][0],
grid[5][0],
grid[6][0],
grid[7][0],
grid[8][0]
];
const column2 = [
grid[0][1],
grid[1][1],
grid[2][1],
grid[3][1],
grid[4][1],
grid[5][1],
grid[6][1],
grid[7][1],
grid[8][1]
];
const column3 = [
grid[0][2],
grid[1][2],
grid[2][2],
grid[3][2],
grid[4][2],
grid[5][2],
grid[6][2],
grid[7][2],
grid[8][2]
];
const column4 = [
grid[0][3],
grid[1][3],
grid[2][3],
grid[3][3],
grid[4][3],
grid[5][3],
grid[6][3],
grid[7][3],
grid[8][3]
];
const column5 = [
grid[0][4],
grid[1][4],
grid[2][4],
grid[3][4],
grid[4][4],
grid[5][4],
grid[6][4],
grid[7][4],
grid[8][4]
];
const column6 = [
grid[0][5],
grid[1][5],
grid[2][5],
grid[3][5],
grid[4][5],
grid[5][5],
grid[6][5],
grid[7][5],
grid[8][5]
];
const column7 = [
grid[0][6],
grid[1][6],
grid[2][6],
grid[3][6],
grid[4][6],
grid[5][6],
grid[6][6],
grid[7][6],
grid[8][6]
];
const column8 = [
grid[0][7],
grid[1][7],
grid[2][7],
grid[3][7],
grid[4][7],
grid[5][7],
grid[6][7],
grid[7][7],
grid[8][7]
];
const column9 = [
grid[0][8],
grid[1][8],
grid[2][8],
grid[3][8],
grid[4][8],
grid[5][8],
grid[6][8],
grid[7][8],
grid[8][8]
];
const indexToGroup = {
1: group1,
2: group1,
3: group1,
4: group2,
5: group2,
6: group2,
7: group3,
8: group3,
9: group3,
10: group1,
11: group1,
12: group1,
13: group2,
14: group2,
15: group2,
16: group3,
17: group3,
18: group3,
19: group1,
20: group1,
21: group1,
22: group2,
23: group2,
24: group2,
25: group3,
26: group3,
27: group3,
28: group4,
29: group4,
30: group4,
31: group5,
32: group5,
33: group5,
34: group6,
35: group6,
36: group6,
37: group4,
38: group4,
39: group4,
40: group5,
41: group5,
42: group5,
43: group6,
44: group6,
45: group6,
46: group4,
47: group4,
48: group4,
49: group5,
50: group5,
51: group5,
52: group6,
53: group6,
54: group6,
55: group7,
56: group7,
57: group7,
58: group8,
59: group8,
60: group8,
61: group9,
62: group9,
63: group9,
64: group7,
65: group7,
66: group7,
67: group8,
68: group8,
69: group8,
70: group9,
71: group9,
72: group9,
73: group7,
74: group7,
75: group7,
76: group8,
77: group8,
78: group8,
79: group9,
80: group9,
81: group9
}; /*Purpose to index to the group to check if value is needed */
const indexToColumn = {
1: column1,
2: column2,
3: column3,
4: column4,
5: column5,
6: column6,
7: column7,
8: column8,
9: column9,
10: column1,
11: column2,
12: column3,
13: column4,
14: column5,
15: column6,
16: column7,
17: column8,
18: column9,
19: column1,
20: column2,
21: column3,
22: column4,
23: column5,
24: column6,
25: column7,
26: column8,
27: column9,
28: column1,
29: column2,
30: column3,
31: column4,
32: column5,
33: column6,
34: column7,
35: column8,
36: column9,
37: column1,
38: column2,
39: column3,
40: column4,
41: column5,
42: column6,
43: column7,
44: column8,
45: column9,
46: column1,
47: column2,
48: column3,
49: column4,
50: column5,
51: column6,
52: column7,
53: column8,
54: column9,
55: column1,
56: column2,
57: column3,
58: column4,
59: column5,
60: column6,
61: column7,
62: column8,