forked from eternaldensity/Sandcastle-Builder
-
Notifications
You must be signed in to change notification settings - Fork 0
/
puzzles.js
268 lines (258 loc) · 9.12 KB
/
puzzles.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
Molpy.DefinePuzzles = function() {
Molpy.PuzzleGens = {};
Molpy.Puzzle = function(name, cleanupFunction) {
this.name = name;
this.cleanupFunction = cleanupFunction;
Molpy.PuzzleGens[name] = this;
this.operators = ['and', 'or'];
this.Generate = function(scoreMultiplier) {
this.firstTry = 1;
this.active = true;
this.level = Molpy.Level('Logicat');
var statementNames = 'ABCDEFGHIJ';
var shuffledNames = statementNames.split('');
ShuffleList(shuffledNames);
this.n = flandom(Math.ceil(Math.PI)) + Math.ceil(Math.PI);
this.puzzles = scoreMultiplier || 1;
var statements = [];
var i = this.n;
while(i--) {
var statement = {};
statements[i] = statement;
statement.id = i;
statement.name = shuffledNames[i];
statement.value = flandom(2) == 0;
}//we have now made a list of statements each with a truth value
var completedStatements = [];
this.groupNumber = 1;
while(completedStatements.length < this.n) {
var groupSize = flandom(statements.length) + 1;
var dist3 = Math.abs(groupSize - 3) * 2;
if(flandom(dist3 + 1)) groupSize = flandom(statements.length) + 1
var group = statements.splice(0, groupSize);
this.FillStatements(group, groupSize);
completedStatements = completedStatements.concat(group)
if(flandom(statements.length / 2) == 0) {
this.AttachStatements(statements, completedStatements);
}
this.groupNumber++;
}
for( var i in completedStatements) {
ShuffleList(completedStatements[i].claims);
}
ShuffleList(completedStatements);
this.Check = function(guess) {
if(!completedStatements) return;
var skip = 0;
if(!this.firstTry) {
if(!Molpy.Spend(this.tryCost)) {
Molpy.Notify('You can\'t afford a second try.');
return;
}
}
var correct = 0;
var incorrect = 0;
for( var i in guess) {
if(completedStatements[i].value == guess[i])
correct++;
else
incorrect++;
}
if(incorrect > 0 && correct + incorrect > 1) {
this.tryCost = {
GlassBlocks: 50 * incorrect
};
if(this.firstTry && Molpy.Got('Second Chance') && Molpy.Has(this.tryCost) && confirm('You have ' + Molpify(incorrect) + ' answer' + plural(incorrect) + ' incorrect. Retry?')) {
this.firstTry = 0;
Molpy.Notify('You may Try Again for ' + Molpy.PriceString(this.tryCost));
return;
}
}
var diff = correct - incorrect;
var points = .5 + Molpy.Level('PR') / 2;
var score = 0;
if(diff > 0) {
score = diff * (this.firstTry * .5 + points) * this.puzzles;
} else if(diff < 0) {
score = diff * (!this.firstTry + points) * this.puzzles;
}
Molpy.Notify(Molpify(correct) + ' answer' + plural(correct) + ' correct, ' + Molpify(incorrect) + ' answer' + plural(incorrect) + ' incorrect. You earned ' + Molpify(score) + ' point' + plural(score), 1);
if(diff > 0) {
Molpy.Add('Logicat', 0, score);
Molpy.UpdateFaves(1);
} else if(diff < 0) {
Molpy.Destroy('Logicat', 0, -score);
Molpy.UpdateFaves(1);
}
completedStatements = [];
this.active = false;
this.cleanupFunction();
}
this.StringifyStatements = function(noWrap) {
var str = '';
if (this.puzzles && this.puzzles > 1) str += '['+Molpify(this.puzzles,1)+' Puzzles]<br>';
for( var id in completedStatements) {
str += this.StringifyStatement(completedStatements[id], id) + '<br>';
}
str += '<input type="Button" value="Clear Guesses" onclick="Molpy.PuzzleGens[\'' + this.name + '\'].Clear()"></input><br>';
str += '<input type="Button" value="Submit Guesses" onclick="Molpy.PuzzleGens[\'' + this.name + '\'].Submit()"></input>';
if(!noWrap) str = '<div id="' + this.name + 'Puzzle" class="logipuzzle">' + str + '</div>';
return str;
}
this.Clear();
}
this.Clear = function() {
this.guess = [];
var i = this.n;
while(i--) {
this.guess.push('No Guess');
}
var d = g(this.name + 'Puzzle');
if(d) d.innerHTML = this.StringifyStatements(1);
}
this.AddStatementLastToChain = function(group, n) {
var last = group[n - 1];
var pen = group[n - 2];
var first = group[0];
if(pen.claims.length == 1) {
if(pen.claims[0].name == pen.name) {
pen.claims = [{name: last.name, value: pen.value == last.value}];
last.claims = [{name: last.name, value: true}]; //tells us nothing about last because pen's claim told us nothing about pen
last.reason = 'dummy added to chain';
} else {
pen.claims = [{name: last.name, value: pen.value == last.value}];
last.claims = [{name: first.name, value: first.value == last.value}];
last.reason = 'added to chain';
}
} else {
this.FillStatements([last], 1);//can't fit into chain so make it a single
}
}
this.FillStatements = function(group, n) {
for( var i in group) {
if(group[i].groupSize) break;
group[i].groupNumber = this.groupNumber;
group[i].groupSize = n;
}
if(n == 0)
return;//no statements: nothing to do
else if(n == 1) {
var a = group[0];
a.operator = (a.value ? 'or' : 'and');
a.reason = (a.value ? 'tautology' : 'contradiction');
a.claims = [{name: a.name, value: true}, {name: a.name, value: false}];
} else if(n == 2) {
var a = group[0];
var b = group[1];
if(a.value) {
a.operator = 'or';
if(randbool()) {
a.claims = [{name: a.name, value: false}, {name: b.name, value: b.value}];
a.reason = 'force both values with paradox';
b.claims = [{name: b.name, value: true}]; //tells us nothing
b.reason = 'dummy';
} else {
a.claims = [{name: a.name, value: true}, {name: b.name, value: !b.value}];
a.reason = 'invalid value would force contradiction';
b.claims = [{name: a.name, value: b.value}];
b.reason = 'valid claim to contradict';
}
} else {
a.operator = 'and';
var r = randbool();
a.claims = [{name: a.name, value: !r}, {name: b.name, value: b.value != r}];
a.reason = (r ? 'incorrect values lead to paradox' : 'if true, forces contradiction');
b.claims = [{name: a.name, value: !b.value}];
b.reason = (r ? 'valid but unnecessary' : 'valid claim to contradict');
}
} else if(n == 3) {
var a = group[0];
var b = group[1];
var c = group[2];
if(randbool()) {
this.FillStatements(group, n - 1);
this.AddStatementLastToChain(group, n);
} else {
a.operator = (a.value ? 'or' : 'and');
var r = randbool();
a.claims = [{name: b.name, value: !b.value == r}, {name: c.name, value: c.value == r}];
a.reason = 'critical statement of a triple';
b.claims = [{name: a.name, value: a.value == b.value}];
b.reason = 'valid, part of triple';
c.claims = [{name: a.name, value: a.value == c.value}];
c.reason = 'valid, part of triple';
}
} else {
this.FillStatements(group, n - 1);
this.AddStatementLastToChain(group, n);
}
}
this.AttachStatements = function(extra, main) {
for( var i in extra) {
var e = extra[i];
var a = GLRschoice(main);
var b = GLRschoice(main);
main.push(e);
if(a === b) {
e.claims = [{name: a.name, value: e.value == a.value}];
e.reason = 'attached singleton';
} else {
e.claims = [{name: a.name, value: a.value}, {name: b.name, value: b.value}];
e.reason = 'doubly attached';
if(randbool()) {
e.operator = 'or';
if(e.value) {
e.claims[0].value = randbool();
} else {
e.claims[0].value = !a.value;
e.claims[1].value = !b.value;
}
} else {
e.operator = 'and';
if(!e.value) {
e.claims[0].value = randbool();
e.claims[1].value = !b.value;
}
}
}
}
}
this.guessOptions = ['No Guess', 'True', 'False'];
this.SelectGuess = function(id,sel) {
// Molpy.Notify('Here ' + id + ',' + sel);
this.guess[id] = this.guessOptions[sel];
}
this.StringifyStatement = function(statement, id) {
var str = statement.name + ':';
var name = this.name + (Molpy.DisplayingFave?'F':'');
for( var i in statement.claims) {
i = parseInt(i);
str += ' ' + this.StringifyClaim(statement.claims[i]);
if(i < statement.claims.length - 1) {
str += ' ' + statement.operator;
}
}
str += '<br><div class="logipuz' + (Molpy.options.logicatcol?' collogipuz':'') + '">';
for (var i in this.guessOptions) {
str += '<input type=radio name="selectGuess' + name + id + '" id=Guess' + name + id + '_' + i +
' onclick="Molpy.PuzzleGens[\'' + this.name + '\'].SelectGuess(' + id + ',' + i + ')" ' +
(this.guess[id] == this.guessOptions[i] ? ' checked' : '') + '>'
str += '<label for=Guess' + name + id + '_' + i + '><small>' + this.guessOptions[i] + '</small></label>';
}
str += '</div>';
return str;
}
this.StringifyClaim = function(claim) {
if(claim.invert == undefined) claim.invert = Math.random() * (this.level % 100) > 25;
return claim.name + ' is ' + (claim.invert ? 'not ' + !claim.value : claim.value == true);
}
this.Submit = function() {
var neatGuess = [];
for( var i in this.guess) {
if(this.guess[i] == 'True') neatGuess[i] = true;
else if(this.guess[i] == 'False') neatGuess[i] = false;
}
this.Check(neatGuess);
}
}
}