-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
247 lines (233 loc) · 7.69 KB
/
main.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
var ArrOfJson = [];
var count = 0;
var calculationArray = [[undefined, undefined, undefined, undefined], [undefined, undefined, undefined, undefined], [undefined, undefined, undefined, undefined], [undefined, undefined, undefined, undefined]];
function calcJacobian() {
var myArr = document.forms.inputField;
var myControls = myArr.elements;
for (var i = 0; i < myControls.length; i++) {
var aControl = myControls[i].getAttribute("name");;
ArrOfJson.push({ "name": aControl, "status": true });
}
// for firsrt time call
getRandomNumber().then((newValue) => {
document.getElementsByName(newValue.name)[0].value = 2;
var updateStatus = ArrOfJson.filter((obj) => {
return obj.name === newValue.name;
});
var column = parseInt(newValue.name) % 10;
var row = (Math.floor(parseInt(newValue.name) / 10));
calculationArray[row][column] = 2;
updateStatus[0].status = false;
});
}
var getRandomNumber = () => {
return new Promise((resolve, reject) => {
count++;
var emptyBox = ArrOfJson.filter((obj) => {
return obj.status == true;
});
if (emptyBox.length === 0) {
return resolve("null");
} else {
var random = getRandomInt(0, emptyBox.length - 1);
return resolve({ "name": emptyBox[Math.floor(random)].name, "value": count % 8 === 0 ? 4 : 2 });
}
});
}
document.addEventListener("keydown", (event) => {
switch (event.keyCode) {
case 37:
var shiftStatusArr = leftAdditionStart();
if (shiftStatusArr.indexOf('false') !== -1) {
common();
}
break;
case 39:
var shiftStatusArr = rightAdditionStart();
if (shiftStatusArr.indexOf('false') !== -1) {
common();
}
break;
case 38:
var shiftStatusArr = upAdditionStart();
if (shiftStatusArr.indexOf('false') !== -1) {
common();
}
break;
case 40:
var shiftStatusArr = downAdditionStart();
if (shiftStatusArr.indexOf('false') !== -1) {
common();
}
break;
}
});
var leftAdditionStart = () => {
var trueArr = [];
for (var i = 0; i < 4; i++) {
var cal = [];
var cal = cal.concat(calculationArray[i]);
var shiftedArr = leftShift(calculationArray[i]);
var addedArray = leftAddition(shiftedArr);
var addedShiftedArr = leftShift(addedArray);
if ((cal.join() === shiftedArr.join()) && (addedArray.join() === shiftedArr.join())) {
trueArr.push('true');
} else {
trueArr.push('false');
}
calculationArray[i] = addedShiftedArr;
var cal = [];
}
return trueArr;
}
var upAdditionStart = () => {
var trueArr = [];
for (var i = 0; i < 4; i++) {
var arr = [];
for (var j = 0; j < 4; j++) {
arr.push(calculationArray[j][i]);
}
var cal = [];
var cal = cal.concat(arr);
var shiftedArr = leftShift(arr);
var addedArray = leftAddition(shiftedArr);
var addedShiftedArr = leftShift(addedArray);
if ((cal.join() === shiftedArr.join()) && (addedArray.join() === shiftedArr.join())) {
trueArr.push('true');
} else {
trueArr.push('false');
}
for (var j = 0; j < 4; j++) {
calculationArray[j][i] = arr[j];
}
var cal = [];
}
return trueArr;
}
var rightAdditionStart = () => {
var trueArr = [];
for (var i = 0; i < 4; i++) {
var cal = [];
var cal = cal.concat(calculationArray[i]);
var shiftedArr = rightShift(calculationArray[i]);
var addedArray = rightAddition(shiftedArr);
var addedShiftedArr = rightShift(addedArray);
if ((cal.join() === shiftedArr.join()) && (addedArray.join() === shiftedArr.join())) {
trueArr.push('true');
} else {
trueArr.push('false');
}
calculationArray[i] = addedShiftedArr;
var cal = [];
}
return trueArr;
}
var downAdditionStart = () => {
var trueArr = [];
for (var i = 0; i < 4; i++) {
var arr = [];
for (var j = 0; j < 4; j++) {
arr.push(calculationArray[j][i]);
}
var cal = [];
var cal = cal.concat(arr);
var shiftedArr = rightShift(arr);
var addedArray = rightAddition(shiftedArr);
var addedShiftedArr = rightShift(addedArray);
if ((cal.join() === shiftedArr.join()) && (addedArray.join() === shiftedArr.join())) {
trueArr.push('true');
} else {
trueArr.push('false');
}
var cal = [];
for (var j = 0; j < 4; j++) {
calculationArray[j][i] = arr[j];
}
}
return trueArr;
}
var leftShift = (arr) => {
for (var i = 1; i <= arr.length; i++) {
var j = i - 1;
if (arr[i]) {
while (j >= 0) {
if (!arr[j]) {
// shift
arr[j] = arr[j + 1];
arr[j + 1] = undefined;
// update the value and status
}
j--;
}
}
}
return arr;
}
var leftAddition = (arr) => {
for (var i = 0; i < 3; i++) {
if (arr[i] === arr[i + 1] && arr[i] && arr[i + 1]) {
arr[i] = arr[i] + arr[i + 1];
arr[i + 1] = undefined;
}
}
return arr;
}
var rightShift = (arr) => {
for (var i = 2; i >= 0; i--) {
var j = i + 1;
if (arr[i]) {
while (j <= 3) {
if (!arr[j]) {
arr[j] = arr[j - 1];
arr[j - 1] = undefined;
// update the value and status
}
j++;
}
}
}
return arr;
}
var rightAddition = (arr) => {
for (var i = 3; i > 0; i--) {
if (arr[i] === arr[i - 1] && arr[i] && arr[i - 1]) {
arr[i] = arr[i] + arr[i - 1];
arr[i - 1] = undefined;
}
}
return arr;
}
var updateStatusFunction = () => {
for (var i = 0; i < 4; i++) {
for (var j = 0; j < 4; j++) {
var updateStatus = ArrOfJson.filter((obj) => {
return obj.name === i + '' + j + 'field';
});
if (calculationArray[i][j]) {
document.getElementsByName(i + '' + j + 'field')[0].value = calculationArray[i][j];
updateStatus[0].status = false;
} else {
document.getElementsByName(i + '' + j + 'field')[0].value = '';
updateStatus[0].status = true;
}
}
}
}
function getRandomInt(min, max) {
min = Math.ceil(min);
max = Math.floor(max);
return Math.floor(Math.random() * (max - min)) + min; //The maximum is exclusive and the minimum is inclusive
}
var common = () => {
updateStatusFunction();
getRandomNumber().then((newValue) => {
document.getElementsByName(newValue.name)[0].value = newValue.value;
var updateStatus = ArrOfJson.filter((obj) => {
return obj.name === newValue.name;
});
var column = parseInt(newValue.name) % 10;
var row = (Math.floor(parseInt(newValue.name) / 10));
calculationArray[row][column] = newValue.value;
updateStatus[0].status = false;
})
}