|
分析
采用微信小程序的canvas制作五子棋;
確定棋盤大小及格數(shù);
繪制棋盤----通過(guò)棋盤寬高和格數(shù)計(jì)算間距,同時(shí)保存坐標(biāo)點(diǎn);
黑方和白方下子----定義一個(gè)布爾變量代表各自的身份;
重置棋盤----重新開始;
通過(guò)判斷當(dāng)前棋手,悔棋時(shí)進(jìn)行改變。
繪制棋盤drawLine(arr){ arr.forEach(current => { this.ctx.setFillStyle(this.lineColor); this.ctx.beginPath(); this.ctx.lineWidth = 1; this.ctx.moveTo(current[0].x, current[0].y); for (var i = 1; i < current.length; i++) { this.ctx.lineTo(current.x, current.y); } this.ctx.stroke(); this.ctx.closePath(); this.ctx.draw(true); }); } drawChessboard(){ // 每個(gè)格子的寬高 var everyLen = this.everyLen; // 標(biāo)記坐標(biāo)的個(gè)數(shù) var count = 0; // 從縱向保存坐標(biāo) var arrY = []; // 雙循環(huán)計(jì)算每個(gè)坐標(biāo)的橫縱坐標(biāo) for(var i = 0;i <= this.type; i++){ var arr = [],arr0 = []; for(var j = 0;j <= this.type; j++){ count++; arr.push({ y: this.margin + i * everyLen, x: this.margin + j * everyLen, pointX: j, pointY: i, index: count }); arr0.push({ x: this.margin + i * everyLen, y: this.margin + j * everyLen }) } // 清空canvas this.ctx.clearRect(0, 0, this.width, this.height); // 保存橫線坐標(biāo)和豎線坐標(biāo) this.everyPoint.push(arr); arrY.push(arr0); } // 繪制橫向線 this.drawLine(this.everyPoint); // 繪制豎向線 this.drawLine(arrY); }
繪制當(dāng)前點(diǎn)擊坐標(biāo)的棋子// 獲取當(dāng)前點(diǎn)擊位置的坐標(biāo) getPosition(e){ return { x: e.touches[0].x, y: e.touches[0].y }; } // 將當(dāng)前坐標(biāo)和棋盤坐標(biāo)數(shù)組對(duì)比,找到精確坐標(biāo) checkPoint(arr,po){ for (var i = 0; i < this.everyPoint.length; i++){ for (var j = 0; j < this.everyPoint.length; j++){ if (Math.abs(this.everyPoint[j].x - po.x) < this.everyLen/2 && Math.abs(this.everyPoint[j].y - po.y) < this.everyLen/2){ // 將棋盤精確坐標(biāo)保存到當(dāng)前持棋方數(shù)組 arr.push(this.everyPoint[j]); // 同時(shí)刪除棋盤坐標(biāo)數(shù)組的該值,表示當(dāng)前位置已經(jīng)存在棋子 this.everyPoint.splice(j,1); break; } } } } // 繪制當(dāng)前坐標(biāo)棋子 drawCle(opts,color){ this.ctx.setFillStyle(color); this.ctx.beginPath(); this.ctx.arc(opts.x, opts.y, this.r, 0, Math.PI * 2, true); this.ctx.closePath(); this.ctx.fill(); this.ctx.draw(true); } drawLastPoint(type){ // 判斷是黑方持棋還是白方持棋,進(jìn)行繪制棋子 if(type == 'AI'){ this.AIPoint.forEach((current, index) => { this.drawCle(current, '#000000'); }); }else{ this.myPoint.forEach((current, index) => { this.drawCle(current, '#ffffff'); }); } } this.page.changeTouchStart = function (e) { // 判斷游戲是否開始 if (self.START_GAME){ // 獲取當(dāng)前坐標(biāo) var newPo = self.getPosition(e); // 獲取棋盤精確坐標(biāo) if (!self.boolAI && self.boolMy) { self.checkPoint(self.myPoint, newPo); } else if (self.boolAI && !self.boolMy) { self.checkPoint(self.AIPoint, newPo); } } } this.page.changeTouchEnd = function (e) { if (self.START_GAME) { // 繪制棋子 if (!self.boolAI && self.boolMy) { self.boolAI = !self.boolAI; self.boolMy = !self.boolMy; self.drawLastPoint('PO'); // 判斷白棋是否五子勝利 if (self.myPoint.length >= 5 && self.checkWinner(self.myPoint)){ wx.showToast({title: '白棋勝利!'}); self.START_GAME = false; } } else if (self.boolAI && !self.boolMy) { self.boolAI = !self.boolAI; self.boolMy = !self.boolMy; self.drawLastPoint('AI'); // 判斷黑棋是否五子勝利 if(self.AIPoint.length >= 5 && self.checkWinner(self.AIPoint)){ wx.showToast({ title: '黑棋勝利!' }); self.START_GAME = false; } } } }
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
五子棋勝利方判斷
五子棋勝利就是橫向、縱向、45度斜線方向、135度斜線方向連成五個(gè)顏色相同的棋子,為了更加清楚的表示,我將四個(gè)方向的判斷做四個(gè)函數(shù)處理。
checkTransverse(arr,po){//橫向檢查 var len = arr.length - 1; var count = 1; // 東 for(var i = 1; i < this.CHESS_LEN ; i++){ for (var j = 0; j < len; j++){ if(arr[j].pointX == po.pointX - i && arr[j].pointY == po.pointY){ count++; } } } if (count == this.CHESS_LEN) { return true;} // 西 for (var i = 1; i < this.CHESS_LEN; i++) { for (var j = 0; j < len; j++) { if (arr[j].pointX == po.pointX + i && arr[j].pointY == po.pointY) { count++; } } } if (count == this.CHESS_LEN) { return true; } } checkPortrait(arr,po){//縱向檢查 var len = arr.length - 1; var count = 1; // 南 for (var i = 1; i < this.CHESS_LEN; i++) { for (var j = 0; j < len; j++) { if (arr[j].pointX == po.pointX && arr[j].pointY == po.pointY - i) { count++; } } } if (count == this.CHESS_LEN) { return true; } // 北 for (var i = 1; i < this.CHESS_LEN; i++) { for (var j = 0; j < len; j++) { if (arr[j].pointX == po.pointX && arr[j].pointY == po.pointY + i) { count++; } } } if (count == this.CHESS_LEN) { return true; } } checkNortheast(arr,po){//45度 var len = arr.length - 1; var count = 1; // 西南 for (var i = 1; i < this.CHESS_LEN; i++) { for (var j = 0; j < len; j++) { if (arr[j].pointX == po.pointX - i && arr[j].pointY == po.pointY - i) { count++; } } } if (count == this.CHESS_LEN) { return true; } // 東北 for (var i = 1; i < this.CHESS_LEN; i++) { for (var j = 0; j < len; j++) { if (arr[j].pointX == po.pointX + i && arr[j].pointY == po.pointY + i) { count++; } } } if (count == this.CHESS_LEN) { return true; } } checkNorthwest(arr,po){//135度 var len = arr.length - 1; var count = 1; // 西北 for (var i = 1; i < this.CHESS_LEN; i++) { for (var j = 0; j < len; j++) { if (arr[j].pointX == po.pointX - i && arr[j].pointY == po.pointY + i) { count++; } } } if (count == this.CHESS_LEN) { return true; } // 東南 for (var i = 1; i < this.CHESS_LEN; i++) { for (var j = 0; j < len; j++) { if (arr[j].pointX == po.pointX + i && arr[j].pointY == po.pointY - i) { count++; } } } if (count == this.CHESS_LEN) { return true; } } checkWinner(arr){ var currentPo = arr[arr.length - 1]; var win1 = this.checkTransverse(arr, currentPo); var win2 = this.checkPortrait(arr, currentPo); var win3 = this.checkNortheast(arr, currentPo); var win4 = this.checkNorthwest(arr, currentPo); if (win1 || win2 || win3 || win4){ return true; }else{ return false; } }
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
重置棋盤resetChessBoard(){ this.page.setData({ isHide: false }); this.init();}this.page.changeReset = function(e){ self.resetChessBoard();}
1
2
3
4
5
6
7
注意
繪制棋盤前必須清空canvas,方便最后的重新開始和重置棋盤;
對(duì)當(dāng)前棋子的坐標(biāo)四個(gè)方向的判斷,采用的原始坐標(biāo)而不是計(jì)算后的繪制坐標(biāo);
在判斷持棋人時(shí),各自采用一個(gè)值,方便添加悔棋功能。
只是實(shí)現(xiàn)了簡(jiǎn)單的對(duì)下五子棋功能,后續(xù)添加悔棋、記分、記時(shí)等功能!同時(shí)向判斷勝利的函數(shù)可以合并為一進(jìn)行優(yōu)化!
|
|