質問編集履歴
2
修正
test
CHANGED
File without changes
|
test
CHANGED
@@ -34,143 +34,7 @@
|
|
34
34
|
|
35
35
|
```
|
36
36
|
|
37
|
-
<!DOCTYPE html>
|
38
37
|
|
39
|
-
<html lang="ja">
|
40
|
-
|
41
|
-
<head>
|
42
|
-
|
43
|
-
<meta charset="utf-8">
|
44
|
-
|
45
|
-
<title>JSー迷路</title>
|
46
|
-
|
47
|
-
<style>
|
48
|
-
|
49
|
-
body {
|
50
|
-
|
51
|
-
font-family: "Century Gothic";
|
52
|
-
|
53
|
-
font-size: 16px;
|
54
|
-
|
55
|
-
}
|
56
|
-
|
57
|
-
#container {
|
58
|
-
|
59
|
-
text-align: center;
|
60
|
-
|
61
|
-
margin: 20px auto;
|
62
|
-
|
63
|
-
}
|
64
|
-
|
65
|
-
#mycanvas {
|
66
|
-
|
67
|
-
background: skyblue;
|
68
|
-
|
69
|
-
}
|
70
|
-
|
71
|
-
.btn {
|
72
|
-
|
73
|
-
margin: 3px auto;
|
74
|
-
|
75
|
-
width: 200px;
|
76
|
-
|
77
|
-
padding: 5px;
|
78
|
-
|
79
|
-
background: blue;
|
80
|
-
|
81
|
-
color: white;
|
82
|
-
|
83
|
-
border-radius: 3px;
|
84
|
-
|
85
|
-
cursor: pointer;
|
86
|
-
|
87
|
-
}
|
88
|
-
|
89
|
-
.btn:hover {
|
90
|
-
|
91
|
-
opacity: 0.7;
|
92
|
-
|
93
|
-
}
|
94
|
-
|
95
|
-
</style>
|
96
|
-
|
97
|
-
</head>
|
98
|
-
|
99
|
-
<body>
|
100
|
-
|
101
|
-
<div id="container">
|
102
|
-
|
103
|
-
<canvas width="100" height="100" id="mycanvas">
|
104
|
-
|
105
|
-
Canvasに対応したブラウザを用意してください。
|
106
|
-
|
107
|
-
</canvas>
|
108
|
-
|
109
|
-
<div id="reset" class="btn">RESET</div>
|
110
|
-
|
111
|
-
</div>
|
112
|
-
|
113
|
-
<script>
|
114
|
-
|
115
|
-
(function() {
|
116
|
-
|
117
|
-
|
118
|
-
|
119
|
-
var map = [];
|
120
|
-
|
121
|
-
|
122
|
-
|
123
|
-
// Canvasで描画
|
124
|
-
|
125
|
-
var col = 13;
|
126
|
-
|
127
|
-
var row = 13;
|
128
|
-
|
129
|
-
|
130
|
-
|
131
|
-
for (var x = 0; x < col; x++) {
|
132
|
-
|
133
|
-
map[x] = [];
|
134
|
-
|
135
|
-
for (var y = 0; y < row; y++) {
|
136
|
-
|
137
|
-
map[x][y] = 0;
|
138
|
-
|
139
|
-
}
|
140
|
-
|
141
|
-
}
|
142
|
-
|
143
|
-
|
144
|
-
|
145
|
-
for (var x = 1; x < col; x += 2) {
|
146
|
-
|
147
|
-
for (var y = 1; y < row; y += 2) {
|
148
|
-
|
149
|
-
map[x][y] = 1;
|
150
|
-
|
151
|
-
}
|
152
|
-
|
153
|
-
}
|
154
|
-
|
155
|
-
|
156
|
-
|
157
|
-
var points = [
|
158
|
-
|
159
|
-
[0, -1],
|
160
|
-
|
161
|
-
[0, 1],
|
162
|
-
|
163
|
-
[1, 0],
|
164
|
-
|
165
|
-
[-1, 0] // 左
|
166
|
-
|
167
|
-
];
|
168
|
-
|
169
|
-
function rand(n) {
|
170
|
-
|
171
|
-
return Math.floor(Math.random() * (n + 1));
|
172
|
-
|
173
|
-
}
|
174
38
|
|
175
39
|
|
176
40
|
|
@@ -198,137 +62,7 @@
|
|
198
62
|
|
199
63
|
map[x + r[0]][y + r[1]] = 1;
|
200
64
|
|
201
|
-
}
|
202
65
|
|
203
|
-
}
|
204
|
-
|
205
|
-
|
206
|
-
|
207
|
-
var startX = 0;
|
208
|
-
|
209
|
-
var startY = 0;
|
210
|
-
|
211
|
-
var goalX = col - 1;
|
212
|
-
|
213
|
-
var goalY = row - 1;
|
214
|
-
|
215
|
-
|
216
|
-
|
217
|
-
var wallSize = 10;
|
218
|
-
|
219
|
-
var wallColor = '#3261AB';
|
220
|
-
|
221
|
-
var routeColor = '#FF0088';
|
222
|
-
|
223
|
-
|
224
|
-
|
225
|
-
var canvas = document.getElementById('mycanvas');
|
226
|
-
|
227
|
-
if (!canvas || !canvas.getContext) {
|
228
|
-
|
229
|
-
return false;
|
230
|
-
|
231
|
-
}
|
232
|
-
|
233
|
-
var ctx = canvas.getContext('2d');
|
234
|
-
|
235
|
-
|
236
|
-
|
237
|
-
canvas.width = (col + 2) * wallSize;
|
238
|
-
|
239
|
-
canvas.height = (row + 2) * wallSize;
|
240
|
-
|
241
|
-
// 上下の壁
|
242
|
-
|
243
|
-
for (var x = 0; x < col + 2; x++) {
|
244
|
-
|
245
|
-
drawWall(x, 0);
|
246
|
-
|
247
|
-
drawWall(x, row + 1);
|
248
|
-
|
249
|
-
}
|
250
|
-
|
251
|
-
|
252
|
-
|
253
|
-
// 左右の壁
|
254
|
-
|
255
|
-
for (var y = 0; y < row + 2; y++) {
|
256
|
-
|
257
|
-
drawWall(0, y);
|
258
|
-
|
259
|
-
drawWall(col + 1, y);
|
260
|
-
|
261
|
-
}
|
262
|
-
|
263
|
-
|
264
|
-
|
265
|
-
// 迷路の内部
|
266
|
-
|
267
|
-
for (var x = 0; x < col; x++) {
|
268
|
-
|
269
|
-
for (var y = 0; y < row; y++) {
|
270
|
-
|
271
|
-
if (map[x][y] === 1) {
|
272
|
-
|
273
|
-
drawWall(x + 1, y + 1);
|
274
|
-
|
275
|
-
}
|
276
|
-
|
277
|
-
if ((x === startX && y === startY) || (x === goalX && y === goalY)) {
|
278
|
-
|
279
|
-
drawRoute(x + 1, y + 1);
|
280
|
-
|
281
|
-
}
|
282
|
-
|
283
|
-
}
|
284
|
-
|
285
|
-
}
|
286
|
-
|
287
|
-
// 壁を描画
|
288
|
-
|
289
|
-
function drawWall(x, y) {
|
290
|
-
|
291
|
-
ctx.fillStyle = wallColor;
|
292
|
-
|
293
|
-
drawRect(x, y);
|
294
|
-
|
295
|
-
}
|
296
|
-
|
297
|
-
|
298
|
-
|
299
|
-
function drawRoute(x, y) {
|
300
|
-
|
301
|
-
ctx.fillStyle = routeColor;
|
302
|
-
|
303
|
-
drawRect(x, y);
|
304
|
-
|
305
|
-
}
|
306
|
-
|
307
|
-
|
308
|
-
|
309
|
-
function drawRect(x, y) {
|
310
|
-
|
311
|
-
ctx.fillRect(
|
312
|
-
|
313
|
-
x * wallSize,
|
314
|
-
|
315
|
-
y * wallSize,
|
316
|
-
|
317
|
-
wallSize,
|
318
|
-
|
319
|
-
wallSize);
|
320
|
-
|
321
|
-
}
|
322
|
-
|
323
|
-
|
324
|
-
|
325
|
-
})();
|
326
|
-
|
327
|
-
</script>
|
328
|
-
|
329
|
-
</body>
|
330
|
-
|
331
|
-
</html>
|
332
66
|
|
333
67
|
### 試したこと
|
334
68
|
|
1
自分のコードに変更
test
CHANGED
File without changes
|
test
CHANGED
@@ -4,7 +4,7 @@
|
|
4
4
|
|
5
5
|
JavaScriptで迷路を作成しています。
|
6
6
|
|
7
|
-
|
7
|
+
ソースコードのWhile文のmap[x + r[0]][y + r[1]]とmap[x + r[0]][y + r[0]]の挙動の違いがよくわかりません
|
8
8
|
|
9
9
|
説明していただけると助かります。
|
10
10
|
|
@@ -42,7 +42,7 @@
|
|
42
42
|
|
43
43
|
<meta charset="utf-8">
|
44
44
|
|
45
|
-
<title>迷路</title>
|
45
|
+
<title>JSー迷路</title>
|
46
46
|
|
47
47
|
<style>
|
48
48
|
|
@@ -64,7 +64,7 @@
|
|
64
64
|
|
65
65
|
#mycanvas {
|
66
66
|
|
67
|
-
background:
|
67
|
+
background: skyblue;
|
68
68
|
|
69
69
|
}
|
70
70
|
|
@@ -76,9 +76,9 @@
|
|
76
76
|
|
77
77
|
padding: 5px;
|
78
78
|
|
79
|
-
background:
|
79
|
+
background: blue;
|
80
|
-
|
80
|
+
|
81
|
-
color:
|
81
|
+
color: white;
|
82
82
|
|
83
83
|
border-radius: 3px;
|
84
84
|
|
@@ -88,7 +88,7 @@
|
|
88
88
|
|
89
89
|
.btn:hover {
|
90
90
|
|
91
|
-
opacity: 0.
|
91
|
+
opacity: 0.7;
|
92
92
|
|
93
93
|
}
|
94
94
|
|
@@ -114,61 +114,15 @@
|
|
114
114
|
|
115
115
|
(function() {
|
116
116
|
|
117
|
-
|
117
|
+
|
118
|
-
|
119
|
-
|
120
|
-
|
121
|
-
/*
|
122
|
-
|
123
|
-
0 0 0
|
124
|
-
|
125
|
-
0 1 0
|
126
|
-
|
127
|
-
0 1 0
|
128
|
-
|
129
|
-
map[x][y]
|
130
|
-
|
131
|
-
*/
|
132
|
-
|
133
|
-
|
134
|
-
|
135
|
-
/*
|
136
|
-
|
137
|
-
棒倒し法
|
138
|
-
|
139
|
-
(1) 一つ飛びに壁(棒)を作る
|
140
|
-
|
141
|
-
(2) 1列目の棒を上下左右のどちらかに倒す
|
142
|
-
|
143
|
-
(3) 2列目以降の棒を左以外のどれかに倒す
|
144
|
-
|
145
|
-
|
146
|
-
|
147
|
-
0 0 0 1 0
|
148
|
-
|
149
|
-
1 1 0 1 0
|
150
|
-
|
151
|
-
0 0 0 0 0
|
152
|
-
|
153
|
-
0 1 0 1 1
|
154
|
-
|
155
|
-
0 1 0 0 0
|
156
|
-
|
157
|
-
*/
|
158
118
|
|
159
119
|
var map = [];
|
160
120
|
|
161
|
-
|
121
|
+
|
162
|
-
|
163
|
-
// map[1] = [0, 1, 1];
|
164
|
-
|
165
|
-
// map[2] = [0, 0, 0];
|
166
|
-
|
167
|
-
|
168
122
|
|
169
123
|
// Canvasで描画
|
170
124
|
|
171
|
-
var col = 13;
|
125
|
+
var col = 13;
|
172
126
|
|
173
127
|
var row = 13;
|
174
128
|
|
@@ -284,7 +238,7 @@
|
|
284
238
|
|
285
239
|
canvas.height = (row + 2) * wallSize;
|
286
240
|
|
287
|
-
// 上下の壁
|
241
|
+
// 上下の壁
|
288
242
|
|
289
243
|
for (var x = 0; x < col + 2; x++) {
|
290
244
|
|
@@ -376,8 +330,6 @@
|
|
376
330
|
|
377
331
|
</html>
|
378
332
|
|
379
|
-
|
380
|
-
|
381
333
|
### 試したこと
|
382
334
|
|
383
335
|
|
@@ -388,6 +340,4 @@
|
|
388
340
|
|
389
341
|
### 補足情報(FW/ツールのバージョンなど)
|
390
342
|
|
391
|
-
URL https://dotinstall.com/lessons/maze_js/30909
|
392
|
-
|
393
343
|
ここにより詳細な情報を記載してください。
|