-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.js
272 lines (237 loc) · 7.4 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
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
//Define difficulty enum
/**
* @return a set of four card ids (4 distinct numbers from 1-52)
*/
function drawHand() {
const cards = new Set();
while(cards.size !== 4) {
const c = Math.floor(Math.random() * 52) + 1;
if (!cards.has(c))
cards.add(c);
}
return cards;
}
/**
*
* @param {int} cardId (an integer between 1 and 52)
* @return the corresponding image token
*/
function cardIdToValue(cardId) {
return ((cardId-1) % 13) + 1;
}
/**
*
* @param {int} cardId (an integer between 1 and 52)
* @return the corresponding image token
*/
function cardIdToToken(cardId) {
const s = ['C','D','S','H'][Math.floor((cardId-1)/13)];
const r = cardIdToValue(cardId);
const v = (r == 1) ? 'a'
: (r == 11) ? 'J'
: (r == 12) ? 'Q'
: (r == 13) ? 'K'
: r;
return v+s;
}
/**
*
* @param {int} cardId (an integer between 1 and 52)
* @return the corresponding image
*/
function cardIdToImage(cardId) {
return `deck/Minicard_${cardIdToToken(cardId)}.svg`
}
const permutator = (inputArr) => {
let result = [];
const permute = (arr, m = []) => {
if (arr.length === 0) {
result.push(m)
} else {
for (let i = 0; i < arr.length; i++) {
let curr = arr.slice();
let next = curr.splice(i, 1);
permute(curr.slice(), m.concat(next))
}
}
}
permute(inputArr)
return result;
}
function getSolution(cards) {
const ops = ["+", "-", "*", "/", "^"];
var solutions = [];
function f(a, o, b) {
if (a == NaN || b == NaN)
return NaN;
if (o == "+")
return a+b;
if (o == "-")
return a-b;
if (o == "*")
return a*b;
if (o == "/")
return (a % b) == 0 ? a/b : NaN; //Exclude fractions
if (o == "^")
return Math.pow(a,b);
}
const permutations = permutator(cards);
for(l in permutations) { //4!
p = permutations[l];
for(i = 0; i < 4; i ++) {
p[i] = cardIdToValue(p[i]);
}
for(i in ops) { //5
for(j in ops) { //5
for(k in ops) {//5
const a = p[0];
const b = p[1];
const c = p[2];
const d = p[3];
const x = ops[i];
const y = ops[j];
const z = ops[k];
//a.(b.(c.d))
const v0 = f(a, x, f(b, y, f(c, z, d)));
const s0 = `${a} ${x} (${b} ${y} (${c} ${z} ${d}))`
//((a.b).c).d
const v1 = f(f(f(a, x, b), y, c), z, d);
const s1 = `((${a} ${x} ${b}) ${y} ${c}) ${z} ${d}`
//(a.b).(c.d)
const v2 = f(f(a,x, b), y, f(c, z, d));
const s2 = `(${a} ${x} ${b}) ${y} (${c} ${z} ${d})`
//(a.(b.c)).d
const v3 = f(f(a, x, f(b, y, c), z, d));
const s3 = `(${a} ${x} (${b} ${y} ${c})) ${z} ${d}`
//a.((b.c).d)
const v4 = f(a, x, f(f(b, y, c), z, d));
const s4 = `${a} ${x} ((${b} ${y} ${c}) ${z} ${d})`
if (v0 == 24)
solutions.push(s0);
if (v1 == 24)
solutions.push(s1);
if (v2 == 24)
solutions.push(s2);
if (v3 == 24)
solutions.push(s3);
if (v4 == 24)
solutions.push(s4);
}
}
}
}
return solutions;
}
function getCardsAndSolution(minSolutionCount, maxSolutionCount) {
console.log("Generating hand with solution between ", minSolutionCount, maxSolutionCount)
let cards = null;
let solutions = [];
while (solutions.length < minSolutionCount || solutions.length > maxSolutionCount) {
cards = Array.from(drawHand());
solutions = getSolution(cards)
}
return {
cards: cards,
solutions: solutions
}
}
function getCardsAndSolutionByDifficulty(difficulty) {
let min = 1;
let max = 1000;
switch (difficulty) {
case "Anything":
min = 1
max = 1000
break;
case "Easy":
min = 51
max = 1000
break;
case "Medium":
min = 11
max = 50
break;
case "Hard":
min = 2
max = 10
break;
case "Anya":
min = 1
max = 1
break;
}
return getCardsAndSolution(min, max);
}
function findGetParameter(parameterName) {
var result = null,
tmp = [];
location.search
.substr(1)
.split("&")
.forEach(function (item) {
tmp = item.split("=");
if (tmp[0] === parameterName) result = decodeURIComponent(tmp[1]);
});
return result;
}
function readDifficulty(str) {
switch (str) {
case "Easy":
return "Easy"
case "Medium":
return "Medium"
case "Hard":
return "Hard"
case "Anya":
return "Anya"
}
return "Anything"
}
window.onload = function() {
//Define help Functionality
document.getElementById('help').onclick = function() {
alert("Combine the four cards using addition, subtraction, multiplication, division, and exponentiation such that it totals 24.\n(A=1, J=11, Q=12, K=13)")
}
//Read difficulty
const difficulty = readDifficulty(findGetParameter("difficulty"));
const diff_select = document.getElementById('difficulty');
console.log("Selected difficulty: "+ difficulty)
//Set Difficulty
diff_select.value = difficulty
function changeDifficulty() {
console.log("Chanigng difficulty to:"+diff_select.value)
var url = new URL(window.location.href);
var search_params = url.searchParams;
// new value of "id" is set to "101"
search_params.set('difficulty', diff_select.value);
// change the search property of the main url
url.search = search_params.toString();
// the new url string
var new_url = url.toString();
// output : http://demourl.com/path?id=101&topic=main
console.log("new_url:", new_url);
// Simulate an HTTP redirect:
window.location.replace(new_url);
}
diff_select.addEventListener('change', changeDifficulty, false);
//Generate Cards
let cardsAndSolutionPair = getCardsAndSolutionByDifficulty(difficulty)
const cards = cardsAndSolutionPair.cards
const solutions = cardsAndSolutionPair.solutions
for(i = 0; i < 4; i ++) {
console.log("c"+i, cards[i], cardIdToToken(cards[i]));
const img = document.getElementById("c"+i);
const src = cardIdToImage(cards[i]);
setTimeout(function() {
img.src = src;
}, 250);
}
//Deefine Soluton funcitnlaity
document.getElementById('sol').onclick = function() {
const l = solutions.length;
const body = solutions.join('\n')
if (l == 1 ? confirm(`There is ${1} solution:\n${body}`)
:confirm(`There are ${l} solutions:\n${body}`))
location.reload();
}
}