-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
276 lines (226 loc) · 6.38 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
272
273
274
275
// Copyright © 2016 M. Helmy Hemeda.
function get_random_int(max, except) {
if(typeof except === "undefined") {
except = []
}
var outcome;
do {
outcome = Math.floor(Math.random() * (max + 1));
} while (except.indexOf(outcome) > -1);
return outcome;
};
function get_random_triplet(except) {
var digits = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9];
var triplet = [];
// Every element from except will be removed from digits.
for(var i = 0; i < except.length && digits.length > 0; i++) {
var removal_index = digits.indexOf(except[i]);
if(removal_index > -1) {
digits.splice(removal_index, 1);
}
}
// Select a random element from digits, push it to triplet, remove it from
// digits, then repeat 3 times.
for(var i = 0; i < 3 && digits.length > 0; i++) {
var selection_index = get_random_int(digits.length - 1);
triplet.push(digits[selection_index]);
digits.splice(selection_index, 1);
}
return triplet;
};
constraints = [
{
"description": "One number is correct and is in the right place.",
"get_random_hint": function(answer) {
var hints = get_random_triplet(answer);
var selection_index = get_random_int(answer.length - 1);
hints[selection_index] = answer[selection_index];
return hints;
},
"check": function(input, hints) {
if(input.length != hints.length) {
return false;
}
for(var i = 0; i < input.length; i++) {
if(input[i] == hints[i]) {
return true;
}
}
return false;
}
},
{
"description": "One number is correct but is at the wrong place.",
"get_random_hint": function(answer) {
var selection_index = get_random_int(answer.length - 1);
var selection = answer[selection_index];
var hints = get_random_triplet([selection]);
var insertion_index;
do {
insertion_index = get_random_int(answer.length - 1);
} while (insertion_index == selection_index);
hints[insertion_index] = answer[selection_index];
return hints;
},
"check": function(input, answer) {
if(input.length != answer.length) {
return false;
}
for(var i = 0; i < input.length; i++) {
if(answer.indexOf(input[i]) > - 1 && answer[i] != input[i]) {
return true;
}
}
return false;
}
},
{
"description": "Two numbers are correct but are at wrong places",
"get_random_hint": function(answer) {
var temp = answer.slice();
var replacement_index = get_random_int(answer.length - 1);
temp[replacement_index] = get_random_int(9, answer);
var selection_index1 = get_random_int(answer.length - 1, [replacement_index]);
var selection_index2 = get_random_int(answer.length - 1, [replacement_index, selection_index1]);
var hints = Array(3)
var permutation_type = get_random_int(2);
if(permutation_type == 0) {
hints[selection_index1] = temp[selection_index2];
hints[selection_index2] = temp[selection_index1];
hints[replacement_index] = temp[replacement_index];
}
else if(permutation_type == 1) {
hints[selection_index1] = temp[replacement_index];
hints[selection_index2] = temp[selection_index1];
hints[replacement_index] = temp[selection_index2];
}
else if(permutation_type == 2) {
hints[selection_index1] = temp[selection_index2];
hints[selection_index2] = temp[replacement_index];
hints[replacement_index] = temp[selection_index1];
}
else {
alert("Internal error: unknown permutation");
}
return hints;
},
"check": function(input, answer) {
if(input.length != answer.length) {
return false;
}
var found_correct_number_wrong_place = false;
for(var i = 0; i < input.length; i++) {
if(answer.indexOf(input[i]) > -1 && answer[i] != input[i]) {
if(found_correct_number_wrong_place) {
return true;
}
else {
found_correct_number_wrong_place = true;
}
}
}
return false;
}
},
{
"description": "Nothing is correct.",
"get_random_hint": function(answer) {
var hint = get_random_triplet(answer);
return hint;
},
"check": function(input, hints) {
if(input.length != hints.length) {
return false;
}
for(var i = 0; i < input.length; i++) {
if(hints.indexOf(input[i]) > -1) {
return false;
}
return true;
}
}
},
];
constraints.push(constraints[1]);
function reload_hints () {
window.hints_list = [];
window.valid_answer = get_random_triplet([]);
document.getElementById("digit0").value = "";
document.getElementById("digit1").value = "";
document.getElementById("digit2").value = "";
var table = document.getElementById("hints");
table.innerHTML = "";
for(var c=0; c < constraints.length; c++) {
var row = table.insertRow(-1);
var hints = constraints[c]["get_random_hint"](window.valid_answer);
window.hints_list.push(hints);
for(var h = 0; h < hints.length; h++) {
var cell = row.insertCell(-1);
cell.innerHTML = hints[h];
}
var cell = row.insertCell(-1);
cell.innerHTML = constraints[c]["description"];
}
check_answer();
};
function giveup() {
document.getElementById("digit0").value = window.valid_answer[0];
document.getElementById("digit1").value = window.valid_answer[1];
document.getElementById("digit2").value = window.valid_answer[2];
check_answer();
};
function check_answer() {
var ans = [
document.getElementById("digit0").value,
document.getElementById("digit1").value,
document.getElementById("digit2").value
];
var digit_pattern = new RegExp("^[0-9]$");
for(var i = 0; i < ans.length; i++) {
if(!digit_pattern.test(ans[i])) {
document.getElementById("status").innerHTML = "Enter a decimal digit in every cell.";
return;
}
ans[i] = Number(ans[i]);
}
if(check_triplet(ans)) {
document.getElementById("status").innerHTML = "Correct answer!";
}
else {
document.getElementById("status").innerHTML = "Wrong answer!";
}
};
var check_triplet = function(triplet) {
for(var c = 0; c < constraints.length; c++) {
if(!constraints[c]["check"](triplet, window.hints_list[c])) {
return false;
}
}
return true;
};
var bruteforce = function(limit) {
var hits = 0;
for(var i=0; i < 1000; i++) {
var ans = [
parseInt((i % 1000) / 100),
parseInt((i % 100) / 10),
i % 10
];
if(check_triplet(ans)) {
hits++;
if(hits > limit) {
return false;
}
}
}
return true;
};
var generate_unique_problem = function() {
for(var i = 0; i < 1000; i++) {
reload_hints();
if(bruteforce(1)) {
console.log("Unique answer after " + (i + 1) + " tries");
break;
}
}
};