直接上代码

    1. <!doctype html>
    2. <html lang="en">
    3. <head>
    4. <meta charset="UTF-8">
    5. <meta name="viewport"
    6. content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
    7. <meta http-equiv="X-UA-Compatible" content="ie=edge">
    8. <title>五子棋</title>
    9. <style>
    10. body {
    11. height: 100vh;
    12. margin: 0;
    13. }
    14. canvas {
    15. position: absolute;
    16. left: 50%;
    17. top: 50%;
    18. transform: translate(-50%, -50%);
    19. }
    20. #chessboard {
    21. background-color: #fff;
    22. box-shadow: 2px 2px 18px 2px rgba(0, 0, 0, 0.1);
    23. z-index: 1;
    24. }
    25. #chess {
    26. z-index: 2;
    27. }
    28. .mask {
    29. width: 100vw;
    30. height: 100vh;
    31. position: absolute;
    32. left: 0;
    33. top: 0;
    34. background-color: rgba(0, 0, 0, 0.4);
    35. display: flex;
    36. justify-content: center;
    37. align-items: flex-start;
    38. visibility: hidden;
    39. }
    40. .mask-content {
    41. margin-top: 40px;
    42. height: 120px;
    43. padding: 0 20px;
    44. border-radius: 8px;
    45. background-color: #fff;
    46. box-shadow: 6px 6px 38px 6px rgba(255, 255, 255, 0.1);
    47. text-align: center;
    48. display: flex;
    49. flex-direction: column;
    50. justify-content: center;
    51. }
    52. .mast-content__reset {
    53. margin-top: 15px;
    54. border-radius: 20px;
    55. padding: 8px 23px;
    56. color: #fff;
    57. background-color: #67c23a;
    58. border-color: #67c23a;
    59. cursor: pointer;
    60. font-size: 14px;
    61. letter-spacing: 4px;
    62. }
    63. .control {
    64. position: absolute;
    65. right: 40px;
    66. bottom: 100px;
    67. width: 100px;
    68. height: 80px;
    69. background-color: #fff;
    70. box-shadow: 0 2px 12px 0 rgba(0,0,0,.1);
    71. display: flex;
    72. flex-direction: column;
    73. justify-content: space-between;
    74. padding: 10px 15px;
    75. }
    76. .control div {
    77. border-radius: 20px;
    78. padding: 8px 23px;
    79. color: #fff;
    80. background-color: #67c23a;
    81. border-color: #67c23a;
    82. cursor: pointer;
    83. font-size: 14px;
    84. letter-spacing: 4px;
    85. text-align: center;
    86. }
    87. .dialog-fade-enter-active {
    88. animation: dialog-fade-in .3s;
    89. }
    90. .dialog-fade-leave-active {
    91. animation: dialog-fade-out .3s;
    92. }
    93. @keyframes dialog-fade-in {
    94. 0% {
    95. transform: translate3d(0, -20px, 0);
    96. opacity: 0;
    97. }
    98. 100% {
    99. transform: translate3d(0, 0, 0);
    100. opacity: 1;
    101. }
    102. }
    103. @keyframes dialog-fade-out {
    104. 0% {
    105. transform: translate3d(0, 0, 0);
    106. opacity: 1;
    107. }
    108. 100% {
    109. transform: translate3d(0, -20px, 0);
    110. opacity: 0;
    111. }
    112. }
    113. </style>
    114. </head>
    115. <body>
    116. <canvas id="chessboard" width="500" height="500"></canvas>
    117. <canvas id="chess" width="500" height="500"></canvas>
    118. <div class="mask">
    119. <div class="mask-content">
    120. <div>五子连珠,赢家是:<span class="winner">白子</span></div>
    121. <div class="mast-content__reset">再来一局</div>
    122. </div>
    123. </div>
    124. <div class="control">
    125. <div class="control-prev">悔棋</div>
    126. <div class="control-bye">认输</div>
    127. </div>
    128. <script>
    129. /*
    130. * 1. 棋盘 2. 下棋 3. 判断输赢
    131. *
    132. * 下棋 1. 黑白子 2. 在不在格点, 在那个格点, 能不能落子
    133. * */
    134. const $ = (cls) => {
    135. return document.querySelector(cls)
    136. }
    137. const board = $('#chessboard')
    138. const boardCtx = board.getContext('2d')
    139. const cvs = $('#chess')
    140. const ctx = cvs.getContext('2d')
    141. const LINES = 9
    142. const space = 500 / (LINES + 1) // 格口大小
    143. const r = 15 // 棋子半径
    144. const whiteColor = '#eee' // 白子颜色
    145. const blackColor = '#000' // 黑子颜色
    146. let turn = 'white' // 当前玩家, 白子先落
    147. // 1. 棋盘
    148. const drawGrid = () => {
    149. boardCtx.beginPath()
    150. const grid = Array(LINES).fill('')
    151. grid.forEach((el, index) => {
    152. boardCtx.strokeStyle = '#333'
    153. boardCtx.moveTo(0, space * (index + 1))
    154. boardCtx.lineTo(500, space * (index + 1))
    155. boardCtx.stroke()
    156. boardCtx.moveTo(space * (index + 1), 0)
    157. boardCtx.lineTo(space * (index + 1), 500)
    158. boardCtx.stroke()
    159. })
    160. boardCtx.closePath()
    161. }
    162. drawGrid()
    163. // 2. 下棋
    164. let points = [] // 点位集合
    165. const getPointPos = (x, y) => {
    166. return {
    167. x: x % space < space / 2 ? ~~(x / space) : ~~(x / space) + 1,
    168. y: y % space < space / 2 ? ~~(y / space) : ~~(y / space) + 1
    169. }
    170. }
    171. const posCanDrop = (pos) => { // 点位能否落子
    172. // 边界就不让落子了
    173. if (pos.x === undefined || pos.y === undefined || pos.x === 0 || pos.y === 0) return
    174. return !points.filter(el => el.x === pos.x && el.y === pos.y).length
    175. }
    176. const drop = (pos) => {
    177. if (pos.x === undefined || pos.y === undefined) return
    178. ctx.beginPath()
    179. ctx.arc(pos.x * space, pos.y * space, r, 0, Math.PI * 2)
    180. ctx.fillStyle = pos.turn === 'white' ? whiteColor : blackColor
    181. ctx.fill()
    182. ctx.closePath()
    183. }
    184. const transformPlayer = () => { // 切换玩家
    185. turn = turn === 'white' ? 'black' : 'white'
    186. }
    187. const win = (turn) => {
    188. $('.mask').style.visibility = 'visible'
    189. $('.mask-content').className = 'mask-content dialog-fade-enter-active'
    190. $('.winner').innerText = turn === 'white' ? '白子' : '黑子 '
    191. }
    192. /*
    193. * 以当前点为中心 八方延展 左右和上下很好判断 斜向上和斜向下 则是一个一元一次方程 也很简单(原点为左上角, y轴向下)
    194. * 因为单个网格是一个正方形 所以直线的斜度是45度, 那么方程的 k=1
    195. * 斜向上 y=-x+b 斜向下 y=x+b
    196. * */
    197. const fivePointLine = (pos) => { // 五子连珠 取巧的算法
    198. const pointFilter = points.filter(el => el.turn === turn)
    199. if (pointFilter.length < 5) return
    200. // 只取额外的4颗棋子
    201. const lr = pointFilter.filter(el => el.y === pos.y && Math.abs(el.x - pos.x) < 5) // 左右
    202. const ud = pointFilter.filter(el => el.x === pos.x && Math.abs(el.y - pos.y) < 5) // 上下
    203. const lu = pointFilter.filter(el => el.x + el.y === pos.x + pos.y && Math.abs(el.x - pos.x) < 5) // 斜向上
    204. const ld = pointFilter.filter(el => el.x - el.y === pos.x - pos.y && Math.abs(el.x - pos.x) < 5) // 斜向下
    205. const posCount = (pos, key) => {
    206. const xs = pos.map(el => el[key]).sort()
    207. let i = 0
    208. let count = 1
    209. while (i < xs.length - 1) {
    210. count = Math.abs(xs[i] - xs[i + 1]) === 1 ? count + 1 : 1
    211. i++
    212. }
    213. return count
    214. }
    215. if (
    216. [posCount(lr, 'x'),
    217. posCount(ud, 'y'),
    218. posCount(lu, 'x'),
    219. posCount(ld, 'x')].includes(5)
    220. ) {
    221. win(turn)
    222. }
    223. }
    224. // 点击棋盘
    225. cvs.addEventListener('click', (e) => {
    226. const pos = getPointPos(e.offsetX, e.offsetY)
    227. if (posCanDrop(pos)) {
    228. // 储存棋子位置信息
    229. points.push({ ...pos, turn })
    230. // 落子
    231. drop({ ...pos, turn })
    232. fivePointLine(pos)
    233. transformPlayer()
    234. }
    235. })
    236. // 再来一局
    237. const restart = () => {
    238. ctx.clearRect(0, 0, 500, 500) // 清空落子区域的画布, 这里就可以体现出canvas分层的好处了
    239. }
    240. const once = $('.mast-content__reset')
    241. once.addEventListener('click', () => {
    242. $('.mask-content').className = 'mask-content dialog-fade-leave-active'
    243. setTimeout(() => {
    244. $('.mask').style.visibility = 'hidden'
    245. restart()
    246. }, 300)
    247. })
    248. // 悔棋
    249. const rePaint = () => {
    250. points.forEach(el => {
    251. drop(el)
    252. })
    253. }
    254. $('.control-prev').addEventListener('click', () => {
    255. restart()
    256. points.pop()
    257. rePaint()
    258. transformPlayer()
    259. })
    260. // 认输
    261. $('.control-bye').addEventListener('click', () => {
    262. win(turn === 'white' ? 'black' : 'white')
    263. })
    264. </script>
    265. </body>
    266. </html>