欧美极品高清xxxxhd,国产日产欧美最新,无码AV国产东京热AV无码,国产精品人与动性XXX,国产传媒亚洲综合一区二区,四库影院永久国产精品,毛片免费免费高清视频,福利所导航夜趣136

 找回密碼
 立即注冊(cè)

QQ登錄

只需一步,快速開始

搜索
查看: 1802|回復(fù): 0
打印 上一主題 下一主題
收起左側(cè)

小程序(游戲)----五子棋(棋盤,重置,對(duì)弈)

[復(fù)制鏈接]
跳轉(zhuǎn)到指定樓層
樓主
ID:836897 發(fā)表于 2020-10-31 06:32 | 只看該作者 回帖獎(jiǎng)勵(lì) |倒序?yàn)g覽 |閱讀模式
分析

    采用微信小程序的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)化!

分享到:  QQ好友和群QQ好友和群 QQ空間QQ空間 騰訊微博騰訊微博 騰訊朋友騰訊朋友
收藏收藏 分享淘帖 頂 踩
回復(fù)

使用道具 舉報(bào)

本版積分規(guī)則

小黑屋|51黑電子論壇 |51黑電子論壇6群 QQ 管理員QQ:125739409;技術(shù)交流QQ群281945664

Powered by 單片機(jī)教程網(wǎng)

快速回復(fù) 返回頂部 返回列表