本文实例为大家分享了canvas实现五子棋游戏的具体代码,供大家参考,具体内容如下
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
|
<!DOCTYPE html> < html lang = "en" > < head > < meta charset = "UTF-8" > < meta name = "viewport" content = "width=device-width, initial-scale=1.0" > < title >Document</ title > </ head > < body style = "margin: 0;" > < canvas style = "margin-left: 20px;" id = "five" ></ canvas > < h1 style = "font-size: 40px;font-weight: bolder;margin-left: 200px;margin-top: 0;margin-bottom: 0;" >五子棋</ h1 > < h2 style = "font-size: 40px;font-weight: bolder;margin-left: 200px;margin-top: 0;" id = "currole" >轮到:黑</ h2 > < div id = "res" style = "font-size: 40px;font-weight: bolder;margin-left: 200px;" >胜者:</ div > </ body > < script > let cav = document.getElementById('five') let ctx = cav.getContext('2d') let k = 50 //倍数 let r = k/4 cav.width = cav.height = 16*k ctx.strokeStyle="#000000"; for(let i = 0;i< 15 ;i++){ ctx.beginPath(); ctx.moveTo((i+1)*k,k); ctx.lineTo((i+1)*k,15*k); ctx.stroke(); } for(let i = 0 ;i<15;i++){ ctx.beginPath(); ctx.moveTo(k,(i+1)*k); ctx.lineTo(15*k,(i+1)*k); ctx.stroke(); } function initer(event){ let x = event .offsetX - k let y = event .offsetY - k let xclose = close(x,0,14) let yclose = close(y,0,14) let nodearr = [] let minxd = Math .abs(x-xclose[0]*k)<Math.abs(x-xclose[1]*k)? Math.abs(x-xclose[0]*k)<r?xclose[0]*k:undefined : Math.abs(x-xclose[1]*k)<r?xclose[1]*k:undefined let maxyd = Math .abs(y-yclose[0]*k)<Math.abs(y-yclose[1]*k)? Math.abs(y-yclose[0]*k)<r?yclose[0]*k:undefined : Math.abs(y-yclose[1]*k)<r?yclose[1]*k:undefined if((minxd!==undefined)&&(maxyd!==undefined)){ nodearr = [minxd,maxyd] } if(nodearr.length!==0){ setNode(nodearr) } } cav.addEventListener("click",initer) function close(coo,lort,rorb){ coo<0? coo = 0 :coo =coo coo>14*k?coo=14*k:coo=coo let dect = rorb - lort if(dect===1){ return [lort,rorb] }else{ if(coo<(lort+Math.ceil(dect/2))*k){ let nlort=lort let nrorb=lort+Math.ceil(dect/2) return close(coo,nlort,nrorb) }else{ let nlort=lort+Math.ceil(dect/2) let nrorb=rorb return close(coo,nlort,nrorb) } } } let colorflag = 0 function setNode(arr){ if(gomoku[arr[0]/k][arr[1]/k]!==undefined){ return } if(colorflag===0){ ctx.fillStyle="black" ctx.beginPath(); ctx.arc(arr[0]+k,arr[1]+k,r,0,2*Math.PI); ctx.stroke(); ctx.fill(); checkFinish([arr[0]/k,arr[1]/k]) gomoku[arr[0]/k][arr[1]/k] = colorflag colorflag = 1 document.getElementById('currole').innerHTML="轮到:白" }else{ ctx.fillStyle="white" ctx.beginPath(); ctx.arc(arr[0]+k,arr[1]+k,r,0,2*Math.PI); ctx.stroke(); ctx.fill(); checkFinish([arr[0]/k,arr[1]/k]) gomoku[arr[0]/k][arr[1]/k] = colorflag colorflag = 0 document.getElementById('currole').innerHTML="轮到:黑" } } </ script > < script src = "./five.js" ></ script > </ html > |
外部js文件部分
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
|
let gomoku = new Array(15) for (let i = 0;i<gomoku.length;i++){ gomoku[i]= new Array(15) } //black 0 white 1 function checkFinish(curnode){ for (let i =0;i<4;i++){ let totalLength = 1 let aflag = true let bflag = true for (let j=0;j<4;j++){ let dictNum = j+1 switch (i){ case 0: if (aflag){ (gomoku[curnode[0]+dictNum][curnode[1]]===colorflag) &&((curnode[0]+dictNum)<=14)? totalLength++:aflag= false } if (bflag){ (gomoku[curnode[0]-dictNum][curnode[1]]===colorflag) &&((curnode[0]-dictNum)>=0)? totalLength++:bflag= false } break ; case 1: if (aflag){ (gomoku[curnode[0]][curnode[1]+dictNum]===colorflag) &&((curnode[1]+dictNum)<=14)? totalLength++:aflag= false } if (bflag){ (gomoku[curnode[0]][curnode[1]-dictNum]===colorflag) &&((curnode[1]-dictNum)>=0)? totalLength++:bflag= false } break ; case 2: if (aflag){ (gomoku[curnode[0]+dictNum][curnode[1]+dictNum]===colorflag) &&((curnode[1]+dictNum)<=14) &&((curnode[0]+dictNum)<=14)? totalLength++:aflag= false } if (bflag){ (gomoku[curnode[0]-dictNum][curnode[1]-dictNum]===colorflag) &&((curnode[1]-dictNum)>=0) &&((curnode[0]-dictNum)>=0)? totalLength++:bflag= false } break ; case 3: if (aflag){ (gomoku[curnode[0]+dictNum][curnode[1]-dictNum]===colorflag) &&((curnode[1]-dictNum)>=0) &&((curnode[0]+dictNum)<=14)? totalLength++:aflag= false } if (bflag){ (gomoku[curnode[0]-dictNum][curnode[1]+dictNum]===colorflag) &&((curnode[1]+dictNum)<=14) &&((curnode[0]-dictNum)>=0)? totalLength++:bflag= false } break ; } if (totalLength>=5){ if (colorflag===0){ document.getElementById( 'res' ).innerHTML= '胜者:黑方' cav.removeEventListener( "click" ,initer) } else { document.getElementById( 'res' ).innerHTML= '胜者:白方' cav.removeEventListener( "click" ,initer) } break ; } if ((aflag=== false )&&(bflag=== false )){ break ; } } if (totalLength>=5){ break ; } } } //all work and no play makes jack a dull boy |
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。
原文链接:https://blog.csdn.net/weixin_51364599/article/details/108950537