-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgobang.html
345 lines (314 loc) · 8.94 KB
/
gobang.html
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
<!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>gobang</title>
<style>
.gobang {
width: 640px;
margin: 0 auto;
}
.header {
margin-bottom: 10px;
display: flex;
justify-content: space-between;
}
.header .btns button {
margin-left: 10px;
padding: 0 5px;
}
#my-canvas {
background-color: #e6a23c;
border-radius: 4px;
}
</style>
</head>
<body>
<div class="gobang">
<div class="header">
<span class="tips">当前:黑子</span>
<div class="btns">
<button onclick="restart()">重新开始</button>
<button
:disabled="!cacheIndex || gameOver"
onclick="popStack()"
>悔棋</button>
</div>
</div>
<canvas id="my-canvas" width="640" height="640" onclick="canvasClick(event)"></canvas>
</div>
<script>
/**
* 规则:
* 大小为 20 * 20
* 棋盘数据采用稀疏数组
* 棋子:1 为白色, 0 为黑色
* 可以悔棋
* 结束规则,棋盘填满或者有同色棋子连续占领 5 个位置
*/
const canvasRef = document.querySelector('#my-canvas')
const ctx = canvasRef.getContext('2d')
const tipsRef = document.querySelector('.tips')
const rcs = 20
const gap = 30
const radius = 12
const padding = 20
let gameOver = false
let current = 1
let data = new Array(rcs + 1).fill(0).map(() => new Array(rcs + 1))
const restart = () => {
data = new Array(rcs + 1).fill(0).map(() => new Array(rcs + 1))
cacheIndex = 0
cacheData.length = 1
current = 1
gameOver = false
init()
}
const canvasClick = (e) => {
if (gameOver) {
return
}
const { offsetX, offsetY } = e
const posi = getPostions(
offsetX, offsetY, gap, padding, radius
)
// 当前位置在放置棋子范围内且没有放置棋子
if (posi && !data[posi[0]][posi[1]]) {
data[posi[0]][posi[1]] = current
init()
pushStack(data)
const res = isOver(data, posi)
if (res) {
gameOver = true
setTimeout(() => {
const msg = (Array.isArray(res) ? `${data[res[0]][res[1]] ? '白' : '黑'}方获胜!` : '平局!')
alert('游戏结束,' + msg)
}, 50)
}
}
}
/**
* Description: 五子棋脚本
* Created Date: 2022-07-02 23:17:51
* Author: Kang Dong
*/
// 右上 左下 右 左 右下 左上 下 上
const ds = [[[-1, 1], [1, -1]], [[0, 1], [0, -1]], [[1, 1], [-1, -1]], [[1, 0], [-1, 0]]]
/**
* 判读当前坐标是否满足结束要求
* @param {GobangData} data 棋盘数据
* @param {number} x x 轴
* @param {number} y y 轴
* @param {number} m 最大行数
* @param {number} n 最大列数
* @returns {boolean}
*/
function getPostionResult(data, x, y, m, n) {
const val = data[x][y]
for (let i = 0; i < ds.length; i++) {
const [[lx, ly], [rx, ry]] = ds[i]
let nx = x, ny = y, cnt = 1
for (let j = 0; j < 4; j++) {
nx += lx
ny += ly
if (!(nx >= 0 && nx < m && ny >= 0 && ny < n) || data[nx][ny] !== val) {
break
}
cnt++
}
nx = x
ny = y
for (let j = 0; j < 4; j++) {
nx += rx
ny += ry
if (!(nx >= 0 && nx < m && ny >= 0 && ny < n) || data[nx][ny] !== val) {
break
}
cnt++
}
if (cnt >= 5) {
return true
}
}
return false
}
/**
* 判断是否结束
* 从当前点查询八个方向的连续5个位置是否能连城线
* 所有格子是否全部填满
* 最后下棋的坐标是否连城线
* @param {GobangData} data 棋盘数据
* @param {[number, number]} posi 最后一个是否满足结束的坐标点
*/
const isOver = (data, posi) => {
const m = data.length, n = data[0].length
let nullCnt = m * n
// 先判断最后一个点是否满足结束
if (getPostionResult(data, posi[0], posi[1], m, n)) {
return posi
}
for (let i = 0; i < m; i++) {
for (let j = 0; j < n; j++) {
if (data[i][j] !== undefined) {
nullCnt--
}
}
}
return !nullCnt
}
/**
* 绘制棋盘
* @param ctx canvas的2d实例
* @param number 行列数
* @param gap 行列间隔距离
* @param padding 棋盘边距
*/
const drawChessboard = (ctx, rcs, gap, padding) => {
ctx.beginPath()
ctx.lineWidth = 1
// 行
for (let i = 0; i <= rcs; i++) {
ctx.moveTo(padding + gap * i, padding)
ctx.lineTo(padding + gap * i, padding + gap * rcs)
}
// 列
for (let i = 0; i <= rcs; i++) {
ctx.moveTo(padding, padding + gap * i)
ctx.lineTo(padding + gap * rcs, padding + gap * i)
}
ctx.strokeStyle = '#000'
ctx.stroke()
ctx.closePath()
// 绘制中心圆点
ctx.beginPath()
ctx.arc(
padding + gap * rcs / 2, padding + gap * rcs / 2, 5, 0, 2 * Math.PI
)
ctx.fillStyle = '#000'
ctx.fill()
ctx.closePath()
}
// 绘制白子
function drawWhitePieces(ctx, ci, cj, si, sj, radius = 12) {
ctx.beginPath()
const lg2 = ctx.createRadialGradient(
ci, cj, 5, ci, cj, 20
)
// 向圆形渐变上添加颜色
lg2.addColorStop(0.1, '#fff')
lg2.addColorStop(0.9, '#ddd')
ctx.fillStyle = lg2
ctx.arc(
si, sj, radius, 0, 2 * Math.PI
)
ctx.fill()
ctx.closePath()
}
// 绘制黑子
function drawBlackPieces(ctx, ci, cj, si, sj, radius = 12) {
ctx.beginPath()
const lg2 = ctx.createRadialGradient(
ci, cj, 5, ci, cj, 20
)
// 向圆形渐变上添加颜色
lg2.addColorStop(0.1, '#666')
lg2.addColorStop(0.9, '#000')
ctx.fillStyle = lg2
ctx.arc(
si, sj, radius, 0, 2 * Math.PI
)
ctx.fill()
ctx.closePath()
}
/**
* 绘制棋子,先循环列,再循环行
* @param ctx canvas的2d实例
* @param data 棋盘数据
* @param number 行列数
* @param gap 行列间隔距离
* @param padding 棋盘边距
* @param radius 棋子的半径
*/
const drawPieces = (ctx, data, gap, padding, radius = 12) => {
const m = data.length, n = data[0].length
for (let i = 0; i < m; i++) {
// 列坐标
const cj = i * gap + padding + 6 - padding
const sj = padding + i * gap
for (let j = 0; j < n; j++) {
if (data[i][j] === undefined) {
continue
}
// 行坐标
const ci = j * gap + padding + 6 - padding
const si = padding + j * gap
if (!data[i][j]) {
drawBlackPieces(
ctx, ci, cj, si, sj, radius
)
} else {
drawWhitePieces(
ctx, ci, cj, si, sj, radius
)
}
}
}
}
/**
* 根据点击的坐标来获取棋盘数据的坐标
* @param offsetX 相对于父级元素的 x => 列位置
* @param offsetY 相对于父级元素的 Y => 行位置
* @param gap 行列间隔距离
*/
const getPostions = (offsetX, offsetY, gap, padding, r = 12) => {
const x = Math.round((offsetY - padding) / gap)
const y = Math.round((offsetX - padding) / gap)
// x1, y1 为圆心坐标
const x1 = x * gap + padding, y1 = y * gap + padding
const nr = Math.pow(Math.pow(x1 - offsetY, 2) + Math.pow(y1 - offsetX, 2), 0.5)
if (nr <= r) {
return [x, y]
}
return false
}
// 深拷贝稀疏数组
const cloneDeep = (data) => {
const m = data.length, n = data[0].length
const res = new Array(m).fill(0).map(() => new Array(n))
for (let i = 0; i < m; i++) {
for (let j = 0; j < n; j++) {
if (data[i][j] !== undefined) {
res[i][j] = data[i][j]
}
}
}
return res
}
// 缓存
const cacheData = [cloneDeep(data)]
let cacheIndex = 0
const pushStack = (data) => {
cacheData.push(cloneDeep(data))
cacheIndex++
}
const popStack = () => {
if (cacheIndex && !gameOver) {
data = cloneDeep(cacheData[--cacheIndex])
cacheData.length = cacheIndex + 1
init()
}
}
function init() {
canvasRef.width = 640
drawChessboard(ctx, rcs, gap, padding)
drawPieces(ctx, data, gap, padding, radius)
// 换手
current ^= 1
tipsRef.innerHTML = `当前:${current ? '白子' : '黑子'}`
}
init()
</script>
</body>
</html>