-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlevel1.html
More file actions
334 lines (278 loc) · 10.1 KB
/
level1.html
File metadata and controls
334 lines (278 loc) · 10.1 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
<!DOCTYPE HTML>
<html>
<head>
<link rel="stylesheet" href="styles/level1.css">
<link rel="stylesheet" href="https://code.getmdl.io/1.2.1/material.indigo-pink.min.css">
<title>Level 1 : Sequential</title>
<!--add blockly scripts-->
<script src="blockly/blockly_compressed.js"></script>
<script src="blockly/blocks_compressed.js"></script>
<script src="blockly/msg/js/en.js"></script>
<script src="blockly/javascript_compressed.js"></script>
<!-- -->
</head>
<body>
<h2 class="Header">Sequential </h2>
<h3>Drag the blocks to move X to O</h3>
<script>
// Global variables.
var x = 3;
var y = 1;
var winningX = 0;
var winningY = 4;
// Initialize the game.
function init2() {
x = 3;
y = 1;
winningX = 0;
winningY = 4;
SingleMoveReset();
}
function SingleMoveReset() {
for(var i = 0; i < 25; i++) {
var c = document.getElementById('canvas' + i);
c.width = c.width;
}
DrawXorO(winningX, winningY, 0);
DrawXorO(x, y, 1);
}
function MoveLeft(){
if(y > 0){
y--;
SingleMoveReset();
CheckForWin();
}
}
function MoveUp(){
if(x > 0){
x--;
SingleMoveReset();
CheckForWin();
}
}
function MoveRight(){
if(y < 4){
y++;
SingleMoveReset();
CheckForWin();
}
}
function MoveDown(){
if(x < 4){
x++;
SingleMoveReset();
CheckForWin();
}
}
function execute(s){
init2();
var code = s.split("\n");
for(var i = 0 ; i<code.length ; i++){
var temp = code[i];
switch(temp){
case 'moveDown();':
setTimeout(() => { MoveDown();}, i*1000);
break;
case 'moveUp();':
setTimeout(() => { MoveUp();}, i*1000);
break;
case 'moveLeft();':
setTimeout(() => { MoveLeft();}, i*1000);
break;
case 'moveRight();':
setTimeout(() => { MoveRight();}, i*1000);
break;
}
}
}
function DrawXorO(x, y, XorO){
var c = document.getElementsByName('' + x + y)[0];
var cxt = c.getContext('2d');
cxt.beginPath();
if (XorO == 1) {
cxt.moveTo(10, 10);
cxt.lineTo(40, 40);
cxt.moveTo(40, 10);
cxt.lineTo(10, 40);
} else {
cxt.arc(25, 25, 20, 0, Math.PI * 2, true);
}
cxt.lineWidth = 2;
cxt.stroke();
cxt.closePath();
}
function CheckForWin() {
if(x == winningX){
if(y == winningY){
// alert('Congratulations, You Won!');
// init2();
document.getElementById('nextlevelbutton').style.visibility = 'visible';
// alert('you succeeded now you can move to next level!')
}
else{
document.getElementById('nextlevelbutton').style.visibility = 'hidden';
}
}
}
// blockly blocks
// move left
Blockly.Blocks['move_left'] = {
init: function() {
this.appendDummyInput()
.setAlign(Blockly.ALIGN_CENTRE)
.appendField("Move_Left");
this.setPreviousStatement(true, null);
this.setNextStatement(true, null);
this.setColour(225);
this.setTooltip("");
this.setHelpUrl("");
}
};
Blockly.JavaScript['move_left'] = function(block) {
// TODO: Assemble JavaScript into code variable.
var code = 'moveLeft();\n';
return code;
};
// move right
Blockly.Blocks['move_right'] = {
init: function() {
this.appendDummyInput()
.setAlign(Blockly.ALIGN_CENTRE)
.appendField("Move_Right");
this.setPreviousStatement(true, null);
this.setNextStatement(true, null);
this.setColour(0);
this.setTooltip("");
this.setHelpUrl("");
}
};
Blockly.JavaScript['move_right'] = function(block) {
// TODO: Assemble JavaScript into code variable.
var code = 'moveRight();\n';
return code;
};
// move up
Blockly.Blocks['move_up'] = {
init: function() {
this.appendDummyInput()
.setAlign(Blockly.ALIGN_CENTRE)
.appendField("Move_Up");
this.setPreviousStatement(true, null);
this.setNextStatement(true, null);
this.setColour(290);
this.setTooltip("");
this.setHelpUrl("");
}
};
Blockly.JavaScript['move_up'] = function(block) {
// TODO: Assemble JavaScript into code variable.
var code = 'moveUp();\n';
return code;
};
// move down
Blockly.Blocks['move_down'] = {
init: function() {
this.appendDummyInput()
.setAlign(Blockly.ALIGN_CENTRE)
.appendField("Move_Down");
this.setPreviousStatement(true, null);
this.setNextStatement(true, null);
this.setColour(180);
this.setTooltip("");
this.setHelpUrl("");
}
};
Blockly.JavaScript['move_down'] = function(block) {
// TODO: Assemble JavaScript into code variable.
var code = 'moveDown();\n';
return code;
};
function ShowTextualAndRun() {
Blockly.JavaScript.addReservedWords('code');
var code = Blockly.JavaScript.workspaceToCode();
document.getElementById("textual-code-paragraph").innerHTML = code;
finalCode= code;
execute(code);
try {
eval(code);
} catch (e) {
// alert(e);
}
}
window.onload = init2;
</script>
<div class="stage-container">
<canvas id="canvas0" name = "00" x = "0" y = "0" width="50" height="50" style="border:1px solid black" ></canvas>
<canvas id="canvas1" name = "01" x = "0" y = "1" width="50" height="50" style="border:1px solid black" ></canvas>
<canvas id="canvas2" name = "02" x = "0" y = "2" width="50" height="50" style="border:1px solid black" ></canvas>
<canvas id="canvas3" name = "03" x = "0" y = "3" width="50" height="50" style="border:1px solid black" ></canvas>
<canvas id="canvas4" name = "04" x = "0" y = "4" width="50" height="50" style="border:1px solid black" ></canvas><br/>
<canvas id="canvas5" name = "10" x = "1" y = "0" width="50" height="50" style="border:1px solid black" ></canvas>
<canvas id="canvas6" name = "11" x = "1" y = "1" width="50" height="50" style="border:1px solid black" ></canvas>
<canvas id="canvas7" name = "12" x = "1" y = "2" width="50" height="50" style="border:1px solid black" ></canvas>
<canvas id="canvas8" name = "13" x = "1" y = "3" width="50" height="50" style="border:1px solid black" ></canvas>
<canvas id="canvas9" name = "14" x = "1" y = "4" width="50" height="50" style="border:1px solid black" ></canvas><br/>
<canvas id="canvas10" name = "20" x = "2" y = "0" width="50" height="50" style="border:1px solid black" ></canvas>
<canvas id="canvas11" name = "21" x = "2" y = "1" width="50" height="50" style="border:1px solid black" ></canvas>
<canvas id="canvas12" name = "22" x = "2" y = "2" width="50" height="50" style="border:1px solid black" ></canvas>
<canvas id="canvas13" name = "23" x = "2" y = "3" width="50" height="50" style="border:1px solid black" ></canvas>
<canvas id="canvas14" name = "24" x = "2" y = "4" width="50" height="50" style="border:1px solid black" ></canvas><br/>
<canvas id="canvas15" name = "30" x = "3" y = "0" width="50" height="50" style="border:1px solid black" ></canvas>
<canvas id="canvas16" name = "31" x = "3" y = "1" width="50" height="50" style="border:1px solid black" ></canvas>
<canvas id="canvas17" name = "32" x = "3" y = "2" width="50" height="50" style="border:1px solid black" ></canvas>
<canvas id="canvas18" name = "33" x = "3" y = "3" width="50" height="50" style="border:1px solid black" ></canvas>
<canvas id="canvas19" name = "34" x = "3" y = "4" width="50" height="50" style="border:1px solid black" ></canvas><br/>
<canvas id="canvas20" name = "40" x = "4" y = "0" width="50" height="50" style="border:1px solid black" ></canvas>
<canvas id="canvas21" name = "41" x = "4" y = "1" width="50" height="50" style="border:1px solid black" ></canvas>
<canvas id="canvas22" name = "42" x = "4" y = "2" width="50" height="50" style="border:1px solid black" ></canvas>
<canvas id="canvas23" name = "43" x = "4" y = "3" width="50" height="50" style="border:1px solid black" ></canvas>
<canvas id="canvas24" name = "44" x = "4" y = "4" width="50" height="50" style="border:1px solid black" ></canvas><br/>
</div>
<!--all available blocks-->
<div class="blockly-container" id="blocklyDiv" >
<xml id="toolbox" style="display: none">
<category name="Motion"colour="150">
<block type="move_down"></block>
<block type="move_up"></block>
<block type="move_left"></block>
<block type="move_right"></block>
</category>
</xml>
</div>
<!--ends here -->
<!--textual mapping-->
<div class="textual-code-container">
<!--add a button to show connection between blockly and js-->
<button class="button1" onclick="ShowTextualAndRun()"> Show textual program and run</button>
<div class="paragraph-container" id="textual-code-paragraph"></div>
</div>
<button class="button1" onclick="window.location.href='level2.html'" id="nextlevelbutton" style="visibility: hidden;" > Go to next level</button>
</div>
<!--button script-->
<script>
// sequential level code
// gets the textual mapping of the written code
function runJS() {
Blockly.JavaScript.addReservedWords('code');
var code = Blockly.JavaScript.workspaceToCode();
document.getElementById("textual-code-paragraph").innerHTML = code;
execute(code);
finalCode= code;
try {
eval(code);
} catch (e) {
// alert(e);
}
}
</script>
<!--ends here-->
<script>
</script>
<!--the blockly blocks script should be at the end-->
<script>
var workspace = Blockly.inject('blocklyDiv',
{toolbox: document.getElementById('toolbox')});
</script>
<!--ends here-->
</body>
</html>