This repository was archived by the owner on Nov 14, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscriptEngine.js
More file actions
414 lines (318 loc) · 10.3 KB
/
Copy pathscriptEngine.js
File metadata and controls
414 lines (318 loc) · 10.3 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
var initScriptEngine = function(){
//can you take a ladder?
window.atLadder=false;
//are you at the canyon?
window.atCanyon=false;
//are you at the princess?
window.atPrincess=false;
//have you got the ladder?
window.hasLadder=false;
//can you cross the canyon (because you put the ladder on it)?
window.canyonTraversable=false;
//where the ladder is
window.ladderLocation=-3;
//where the canyon is
window.canyonLocation=11;
//where the princess is
window.princessLocation=14;
//where the hero is
window.heroLocation=0;
//the code generated
var plan='';
//flag for syntax checking during code generation
var inCondition=false;
var justClosedWhile=false;
//current execution step
var currentStep=1;
//list of actions for the B panel
var planList=new Array();
//call trace to move the viewPort
var callTrace= new Array();
//maximum execution steps allowed to prevent infinite loops
var maxSteps=100;
//victory flag, to fix final check
gameOver=false;
//container for current instruction
var scriptedAction=$('<div>');
//add visible element to script list
$('#scriptContainer').append(scriptedAction);
//printable actions map
var stringActions=new Array();
stringActions['takeLadder']= 'take the ladder';
stringActions['dropLadder']= 'drop the ladder';
stringActions['walkRight']= 'walk right';
stringActions['walkLeft']= 'walk left';
stringActions['AtLadder']= 'at the ladder, ';
stringActions['AtCanyon']= 'at the canyon, ';
stringActions['AtPrincess']= 'at the princess, ';
stringActions['HasLadder']= 'have the ladder, ';
stringActions['while']= 'while ';
stringActions['if']= 'if ';
stringActions['not']= 'not ';
//terminates the game
function loseGame(){
message='after some time, the Princess gets tired and goes away: you lost';
gameLog(message);
gameOver=true;
//add to list function calls for the viewport (filter actions that have no graphical relevance)
callTrace.push('loseGame');
}
//checks whether the player won
function checkGameOutcome(){
//if you are at the princess, announce victory
if(AtPrincess()){
message='you got at the princess: you won';
gameLog(message);
gameOver=true;
//add to list function calls for the viewport (filter actions that have no graphical relevance)
callTrace.push('winGame');
}
//if you reached maximum number of steps, terminate
if(currentStep >= maxSteps){
loseGame();
}
}
//increments game step, checks if maximum has been reached and takes action;
function incrementStepAndCheckExecutionTime(){
//increment step and check if maximum;
currentStep+=1;
//checks victory
checkGameOutcome();
}
//populate the output log
function gameLog(message){
//add to list of game events to be sent to B
planList.push(message);
//debug in panel A
console.log(message);
}
//check if you have a ladder
function HasLadder(){
if(hasLadder) gameLog('you got the ladder');
else gameLog('you have no ladder');
return hasLadder;
}
//check if you are not at the princess
function AtPrincess(){
if(atPrincess) gameLog('you are at the princess');
else gameLog('you are not yet at the princess');
return atPrincess;
}
//check if you are not at the ladder
function AtLadder(){
if(atLadder) gameLog('you are at the ladder');
else gameLog('you are not yet at the ladder');
return atLadder;
}
//check if you are not at the canyon
function AtCanyon(){
if(atCanyon) gameLog('you are at the canyon');
else gameLog('you are not yet at the canyon');
return atCanyon;
}
//check if the player can reach ladder and canyon
function updateSurroundingStatus(){
atCanyon=heroLocation == canyonLocation;
atLadder=heroLocation == ladderLocation;
atPrincess=heroLocation == princessLocation;
}
//move right
function walkRight(){
//count time
incrementStepAndCheckExecutionTime();
//check whether you can move past the canyon
if (atCanyon){
if(canyonTraversable){
//move past
heroLocation+=1;
//add to list function calls for the viewport
callTrace.push(arguments.callee.name);
//update log
gameLog('you walk over the canyon');
//winning status, perform check
checkGameOutcome();
}else{
//deny the action
gameLog('can\'t walk: there is a canyon');
}
}else{
//move right
heroLocation+=1;
//add to list function calls for the viewport
callTrace.push(arguments.callee.name);
//output log
gameLog('you walk right');
}
//if you have the ladder, the ladder moves with you
if(hasLadder){
//update ladder location
ladderLocation=heroLocation;
}
//check what you have at reach
updateSurroundingStatus();
}
//move left
function walkLeft(){
//count time
incrementStepAndCheckExecutionTime();
//move left
heroLocation-=1;
//add to list function calls for the viewport
callTrace.push(arguments.callee.name);
//if you have the ladder, the ladder moves with you
if(hasLadder){
//update ladder location
ladderLocation=heroLocation;
}
//output log
gameLog('you walk left');
//check what you have at reach
updateSurroundingStatus();
}
//take ladder, if you can
function takeLadder(){
//count time
incrementStepAndCheckExecutionTime();
//output log
gameLog('you try to take the ladder');
//check whether you can take the ladder
if(atLadder){
//take it
hasLadder=true;
//add to list function calls for the viewport
callTrace.push(arguments.callee.name);
//output log
gameLog('you got the ladder');
}else{
//deny the action
gameLog('there is no ladder to take');
}
}
//drop ladder, if you have it
function dropLadder(){
//count time
incrementStepAndCheckExecutionTime();
//check if you have it
if(hasLadder){
//drop it
hasLadder=false;
//add to list function calls for the viewport (filter actions that have no graphical relevance)
callTrace.push(arguments.callee.name);
//update ladder location
ladderLocation=heroLocation;
//special case: dropping it on the canyon makes it traversable
if(atCanyon){
//update world status
canyonTraversable=true;
//output special log
gameLog('you drop the ladder over the canyon');
}else{
//output log
gameLog('you drop the ladder');
}
}else{
//deny action
gameLog('you didn\' have any ladder to drop');
}
//check what you have at reach
updateSurroundingStatus();
}
//execute script
$('#play').on('click',function(){
//if the flag is still raised, the player left a while without subject action; stop
if(justClosedWhile){
$('#modalErrorNoInstruction').openModal();
return;
}
//execute generated code
eval(plan);
console.log(plan);
checkGameOutcome();
//if plan was not sufficient to carry out goal, kill the player
if(!gameOver) loseGame();
console.log(planList);
console.log(callTrace);
viewportFunctions.playAnimation(callTrace);
});
//reset game
$('#reset').on('click',function(){
location.reload()
});
//wheneven the player clicks on an action
$('#actionsContainer').on('click','.line',function(){
//get action name
var instruction=$(this).attr("id");
//while has a strict syntax; if while is selected
if(('while'==instruction || 'if'==instruction) && !inCondition){
//take note and demand a condition afterwards
inCondition=true;
//select right behaviour for conditional instructions
switch(instruction) {
case 'while':
//generate code for open while (with infinite loop avoidance condition)
plan=plan.concat('while(currentStep<'+maxSteps+' && ');
//generate output in the current container
scriptedAction.append($('<span>').text(stringActions[instruction]));
break;
case 'if':
//generate code for open if
plan=plan.concat('if( ');
//generate output in the current container
scriptedAction.append($('<span>').text(stringActions[instruction]));
break;
}
}else{
//if it's another action
//if in while, demand a condition
if(inCondition){
//if not a condition
if(!$(this).hasClass('condition')){
//demand it
$('#modalErrorNoCondition').openModal();
//and refuse to add the action
return;
}else{
//if it's a negation
if('not'==instruction){
//generate code for not
plan=plan.concat('!');
//generate output in the current container
scriptedAction.append($('<span>').text(stringActions[instruction]));
}else{
//add the condition with closing while parenthese
plan=plan.concat(instruction+'()) ');
//generate output in the current container
scriptedAction.append($('<span>').text(stringActions[instruction]));
//close the while after action is added
inCondition=false;
//raise flag for syntax check of while termination block
justClosedWhile=true;
}
}
}else{
//prevent putting a boolean or negation without being in the need of it
switch(instruction) {
case 'not':
case 'AtCanyon':
case 'AtLadder':
case 'HasLadder':
//demand it
$('#modalErrorConditionNegation').openModal();
//and refuse to add the action
return;
}
//add the action
plan=plan.concat(instruction+'(); ');
//generate output in the current container
scriptedAction.append($('<span>').text(stringActions[instruction]));
//create a new visible element container
scriptedAction=$('<div>');
//add visible element to script list
$('#scriptContainer').append(scriptedAction);
//switch off the flag so we know the syntax is good
if(justClosedWhile) justClosedWhile=false;
}
}
});
};
window.initScriptEngine = initScriptEngine