-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinit.html
More file actions
174 lines (174 loc) · 6.69 KB
/
Copy pathinit.html
File metadata and controls
174 lines (174 loc) · 6.69 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
<html>
<head>
<meta charset="utf-8">
<title>Magic Sword</title>
</head>
<style type="text/css">
.main-div{
position: absolute;
left: 800px;
bottom: 30px;
}
.main-textarea{
height: 300px;
width: 400px;
}
</style>
<body>
<canvas id="myCanvas" width="600" height="600"></canvas>
<div class="main-div">
<textarea id="code" rows="7" class="main-textarea">Code</textarea>
<textarea id="output" rows="7" class="main-textarea">Output</textarea>
<button id="reset">Reset</button>
<button id="submit">Submit</button>
</div>
</body>
<script>
/******** START OF MOCK *********/
var mockData = {
/* map format:
row[0][0], col[0][0], row[0][1], col[0][1], ..., row[0][m],
col[0][m]
....
row[n][0], col[n][0], row[n][1], col[n][1], ...
NOTE:
* row[i][j]=1 means there is a segment above the cell (i,j)
* col[i][j]=1 means there is a segment on the left of (i,j)
* The map is a (n*m) map
* The mth column and nth row is set up for the simplicity of plotting the addtional border
*/
map: [
[1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 0, 1],
[1, 1, 1, 1, 0, 0, 0, 1, 1, 1, 1, 0, 1, 1, 1, 1, 0, 1],
[1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 0, 1],
[1, 1, 1, 1, 0, 0, 1, 1, 1, 1, 0, 1, 1, 1, 0, 1, 0, 1],
[1, 1, 1, 1, 0, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 0, 1],
[1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 0, 0, 0, 1, 0, 1],
[1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 0, 1],
[1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 0, 0, 0, 1, 0, 1],
[1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 0, 1, 1, 1, 0, 1],
[1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 0, 0, 0, 1, 0, 1],
],
source: [2, 3],
dest: [5, 7]
};
var code = `function solveMaze(map, sx, sy, tx, ty) {
report("left");
report("right");
report("up");
report("down");
report("solved");
}
`; // use report to tell the computer what to do now
document.getElementById("code").value = code;
/******** END OF MOCK *********/
var outputQueue = [];
function report(s) {
outputQueue.push(s);
}
viewConstructor = function () {
var c = document.getElementById("myCanvas");
var ctx = c.getContext("2d");
var grid_width, grid_height;
this.repaint = function (width, height, data, sx, sy, tx, ty) {
c.height = c.height; // reset height to clear canvas
grid_width = 600 / (width - 1); // reduce one for border cell
grid_height = 600 / (height - 1);
for (var i = 0; i < height; ++i)
for (var j = 0; j < width; ++j) {
if (data[i][j * 2]) {
// there is a line above the cell
ctx.moveTo(j * grid_width, i * grid_height);
ctx.lineTo((j + 1) * grid_width, i * grid_height);
} if (data[i][j * 2 + 1]) {
// there is a line on the left of the cell
ctx.moveTo(j * grid_width, i * grid_height);
ctx.lineTo(j * grid_width, (i + 1) * grid_height);
}
}
ctx.stroke();
this.paintCircle(sx, sy, "#34E749");
this.paintCircle(tx, ty, "#3494E7");
};
this.paintCircle = function (j, i, color) {
ctx.strokeStyle = color;
var grd = ctx.createRadialGradient(grid_width / 2 + grid_width * i, grid_height * j + grid_height / 2, grid_width / 2, grid_width / 2 + grid_width * i, grid_height * j + grid_height / 2, 0);
grd.addColorStop(0, color);
grd.addColorStop(1, "white");
ctx.fillStyle = grd;
ctx.beginPath();
ctx.arc(grid_width / 2 + grid_width * i, grid_height * j + grid_height / 2, grid_width / 2, 0, 2 * Math.PI);
ctx.fill();
ctx.stroke();
};
};
controllerConstructor = function () {
this.getRandomNum = function (Min, Max) {
var Range = Max - Min;
var Rand = Math.random();
return (Min + Math.round(Rand * Range));
};
this.repaint = function () {
// repaint wrapper
this.view.repaint(this.width, this.height, this.data, this.sx, this.sy, this.tx, this.ty);
}
this.initMap = function (view, mockData, source, dest) {
this.view = view;
this.data = mockData;
this.width = mockData[0].length / 2;
this.height = mockData.length;
this.sx = source[0]; this.sy = source[1];
this.tx = dest[0]; this.ty = dest[1];
this.repaint();
};
this.run = function () {
if (this.hasOwnProperty("timer"))
clearTimeout(this.timer); // clear previous timeout
var cd = document.getElementById("code").value;
var call = '\n\nsolveMaze(this.data, this.sx, this.sy, this.tx, this.ty);';
eval(cd + call);
console.log(outputQueue);
};
this.shiftResult = function () {
if (outputQueue.length > 0) {
var token = outputQueue.shift();
outputText.value += token + "\n";
if (token === "left") {
// TODO: judge legal
this.sy -= 1;
} else if (token === "right") {
this.sy += 1;
} else if (token === "up") {
this.sx -= 1;
} else if (token === "down") {
this.sx += 1;
} else if (token === "solved") {
// TODO: ???
} else if (token === "gg") {
// ???????????
}
this.repaint();
setTimeout(function () {
this.shiftResult();
}.bind(this), 500);
}
};
this.displayResult = function () {
// wrapper
outputText.value = "";
setTimeout(function () {
this.shiftResult();
}.bind(this), 500);
};
};
var view = new viewConstructor();
var controller = new controllerConstructor();
controller.initMap(view, mockData.map, mockData.source, mockData.dest);
var submitBtn = document.getElementById("submit");
var outputText = document.getElementById("output");
submitBtn.onclick = function (event) {
controller.run();
controller.displayResult();
}
</script>
</html>