forked from nie-ny/blog
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmi.html
More file actions
206 lines (181 loc) · 5.08 KB
/
mi.html
File metadata and controls
206 lines (181 loc) · 5.08 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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Document</title>
</head>
<body>
<canvas id="c2d" class="c2d" width="390" height="290"></canvas>
<script type="text/javascript">
var oC2 = document.getElementById('c2d')
var ctx = oC2.getContext('2d')
// 获取宽高
var width = oC2.width
var height = oC2.height
var maxX = 18 // 路的 列数
var maxY = 13 // 路的 行数
var firstGrid // 开始点
var endGrid // 结束点
/**
* 创建格
*/
function Grid(x, y) {
this.x = x
this.y = y
this.choosed = false // 是否标记
this.children = []
this.initNeighbor()
}
// 获取当前格 对应的邻格
Grid.prototype.initNeighbor = function () {
var x = this.x
var y = this.y
this.neighbor = []
if (y > 0) {
this.neighbor.push({
x: x,
y: y - 1
})
}
if (y < maxY) {
this.neighbor.push({
x: x,
y: y + 1
})
}
if (x > 0) {
this.neighbor.push({
x: x - 1,
y: y
})
}
if (x < maxX) {
this.neighbor.push({
x: x + 1,
y: y
})
}
// 随机排序
this.neighbor.sort(function () {
return 0.5 - Math.random()
})
}
// 获取 邻格 并设置 当前格为父级 -- 进入就会标记 保证 格子 子关联一次
Grid.prototype.getNeighbor = function () {
var x, y, neighbor
// 标记格
this.choosed = true
// console.log("🚀 ~ file: mi.html ~ line 82 ~ this", this);
for (var i = 0; i < this.neighbor.length; i++) {
x = this.neighbor[i].x
y = this.neighbor[i].y
// 迷宫 对应位置的白色格子
neighbor = maze.grids[y][x]
// 当格子未标记 返回当前格子 并设置parent为 操作的格子
if (!neighbor.choosed) {
neighbor.parent = this
return neighbor
}
}
//
if (this.parent === firstGrid) {
return 0
} else {
return 1
}
}
/**
* 迷宫实例
*/
function Maze() {
this.path = []
this.grids = [] // 格子
this.stack = []
this.init()
}
/**
* 组装 整个迷宫 白色格子数据
*/
Maze.prototype.init = function () {
for (var i = 0; i <= maxY; i++) {
this.grids[i] = []
for (var j = 0; j <= maxX; j++) {
this.grids[i][j] = new Grid(j, i)
}
}
firstGrid = this.grids[0][0] // 开始格
endGrid = this.grids[13][18] // 结束格
}
/**
* 开始寻找路径
*/
Maze.prototype.findPath = function () {
var tmp
// 得到第一格
var curr = firstGrid
while (1) {
tmp = curr.getNeighbor()
if (tmp === 0) {
console.log('路径找寻结束')
break
} else if (tmp === 1) {
// 当邻格 都标记了 操作父级
curr = curr.parent
} else {
// 返回的 相邻格子 设置为 children 形成树
curr.children[curr.children.length] = tmp
curr = tmp
}
}
}
// 循环绘制 路
function drawPath(node) {
// console.log("🚀 ~ file: mi.html ~ line 154 ~ drawPath ~ node", node);
var i = 0
// 绘制 邻格
drawRect(node.x * 20, node.y * 20)
for (; i < node.children.length; i++) {
if (node.children[i]) {
// 邻格 中间打通
drawRect(node.x * 20 + (node.children[i].x - node.x) * 10, node.y * 20 + (node.children[i].y - node.y) * 10)
// 画路
drawPath(node.children[i])
}
}
}
/**
* 绘制方格
*/
function drawRect(x, y) {
// + 10 从内框开始画
ctx.fillRect(x + 10, y + 10, 10, 10)
}
// function drawDebug(x, y, color) {
// // 开始一条路径
// ctx.beginPath();
// ctx.fillStyle = color;
// // 创建弧/曲线
// ctx.arc(x, y, 1, 0, Math.PI * 2, false);
// // 填充当前绘图
// ctx.fill();
// // 创建从当前点回到起始点的路径
// ctx.closePath();
// }
// 绘制迷宫背景 黑色
ctx.fillStyle = 'black'
ctx.fillRect(0, 0, width, height)
ctx.fillStyle = 'white'
var maze = new Maze()
maze.findPath()
drawPath(firstGrid)
// 开始 结束方格 画上
drawStartEnd()
function drawStartEnd() {
ctx.fillRect(0, 10, 10, 10)
ctx.fillRect(19 * 20, 13 * 20 + 10, 10, 10)
}
</script>
</body>
</html>