JS+canvas五子棋人机对战实现步骤详解


Posted in Javascript onJune 04, 2020

1. 创建实例

function Gobang () {
  this.over = false; // 是否结束
  this.player = true; // true:我 false:电脑
  this.allChesses = []; // 所有棋子
  this.existChesses = [] // 已经落下的棋子
  this.winsCount = 0; // 赢法总数
  this.wins = []; // 所有赢法统计
  this.myWins = []; //我的赢法统计
  this.computerWins = []; //电脑赢法统计
}

2. 初始化

//初始化
Gobang.prototype.init = function(opts) {
  // 生成canvas棋盘
  this.createCanvas(opts);

  //棋盘初始化 
  this.boardInit();

  // 鼠标移动聚焦功能实现
  this.mouseMove();

  //算法初始化
  this.algorithmInit();

  //落子功能实现
  this.dorpChess();
}

3. 生成canvas棋盘

//初始化
//生成canvas
Gobang.prototype.createCanvas = function(opts) {
  var opts = opts || {};
  if (opts.width && opts.width%30 !== 0) throw new RangeError(opts.width+'不是30的倍数');
  this.col = (opts.width && opts.width/30) || 15; // 棋盘列

  var oCanvas = document.createElement('canvas');
  oCanvas.width = oCanvas.height = opts.width || 450;
  this.canvas = oCanvas;
  document.querySelector(opts.container || 'body').appendChild(this.canvas);
  this.ctx = oCanvas.getContext('2d');
}

4. 初始化棋盘

//棋盘初始化
Gobang.prototype.boardInit = function(opts){
  this.drawBoard();
}

// 画棋盘
Gobang.prototype.drawBoard = function(){
  this.ctx.strokeStyle = "#bfbfbf";
  for (var i = 0; i < this.col; i++) {
    this.ctx.moveTo(15+ 30*i, 15);
    this.ctx.lineTo(15+ 30*i, this.col*30-15);
    this.ctx.stroke();
    this.ctx.moveTo(15, 15+ 30*i);
    this.ctx.lineTo(this.col*30-15, 15+ 30*i);
    this.ctx.stroke();
  }
}

JS+canvas五子棋人机对战实现步骤详解

5. 画棋子

// 画棋子
Gobang.prototype.drawChess = function(x, y, player){
  var x = 15 + x * 30,
    y = 15 + y * 30;
  this.ctx.beginPath();
  this.ctx.arc(x, y, 13, 0, Math.PI*2);
  var grd = this.ctx.createRadialGradient(x + 2, y - 2, 13 , x + 2, y - 2, 0);
  if (player) { //我 == 黑棋 
    grd.addColorStop(0, '#0a0a0a');
    grd.addColorStop(1, '#636766');
  }else{ //电脑 == 白棋
    grd.addColorStop(0, '#d1d1d1');
    grd.addColorStop(1, '#f9f9f9');
  }
  this.ctx.fillStyle = grd;
  this.ctx.fill()
}

JS+canvas五子棋人机对战实现步骤详解

6. 移动聚焦

// 鼠标移动时触发聚焦效果, 需要前面的聚焦效果消失, 所有需要重绘canvas
Gobang.prototype.mouseMove = function(){
  var that = this;
  this.canvas.addEventListener('mousemove', function (e) {
    that.ctx.clearRect(0, 0, that.col*30, that.col*30);
    var x = Math.floor((e.offsetX)/30),
      y = Math.floor((e.offsetY)/30);

    //重绘棋盘
    that.drawBoard();

    //移动聚焦效果
    that.focusChess(x, y);

    //重绘已经下好的棋子
    that.redrawedChess()
  });
}

//鼠标移动聚焦
Gobang.prototype.focusChess = function(x, y){
  this.ctx.beginPath();
  this.ctx.fillStyle = '#E74343';
  this.ctx.arc(15 + x * 30, 15 + y * 30, 6, 0, Math.PI*2);
  this.ctx.fill();
}

//重绘当前下好的棋子
Gobang.prototype.redrawedChess = function(x, y){
  for (var i = 0; i < this.existChesses.length; i++) {
    this.drawChess(this.existChesses[i].x, this.existChesses[i].y, this.existChesses[i].player);
  }
}

JS+canvas五子棋人机对战实现步骤详解

7. 算法初始化

//算法初始化
Gobang.prototype.algorithmInit = function(){
  //初始化棋盘的每个位置和赢法
  for (var x = 0; x < this.col; x++) {
    this.allChesses[x] = [];
    this.wins[x] = [];
    for (var y = 0; y < this.col; y++) {
      this.allChesses[x][y] = false;
      this.wins[x][y] = [];
    }
  }
  //获取所有赢法
  this.computedWins();

  // 初始化电脑和我每个赢法当前拥有的棋子数
  for (var i = 0; i < this.winsCount; i++) {
    this.myWins[i] = 0;
    this.computerWins[i] = 0;
  }
}

8. 获取所有赢法

Gobang.prototype.computedWins = function(){
  /*
    直线赢法
    以15列为准
  */
  for (var x = 0; x < this.col; x++) { //纵向所有赢法
    for (var y = 0; y < this.col-4; y ++) {
      this.winsCount ++; 

      /*
        如: 
        1.组成的第一种赢法
          [0,0]
          [0,1]
          [0,2]
          [0,3]
          [0,4]
        
        2.组成的第二种赢法
          [0,1]
          [0,2]
          [0,3]
          [0,4]
          [0,5]
        以此类推一列最多也就11种赢法, 所有纵向x有15列 每列最多11种, 所有纵向总共15 * 11种
      */
      //以下for循环给每种赢法的位置信息储存起来
      for (var k = 0; k < 5; k ++) {
        this.wins[x][y+k][this.winsCount] = true;
        /*
          位置信息
          第一种赢法的时候:
            this.wins = [
                    [
                      [1:true],
                      [1:true],
                      [1:true],
                      [1:true],
                      [1:true]
                    ],
                    [
                      ......
                    ]
                  ]

            虽然这是一个三维数组, 我们把它拆分下就好理解了
            相当于 this.wins[0][0][1], this.wins[0][4][1], this.wins[0][5][1], this.wins[0][6][1], this.wins[0][7][1]
            
            因为对象可以这样取值:
              var obj = {
                a: 10,
                b: 'demo'
              }
              obj['a'] === obj.a

            所有也就相当于 this.wins[0][0].1, this.wins[0][8].1, this.wins[0][9].1, this.wins[0][10].1, this.wins[0][11].1 

            虽然数组不能这么取值,可以这么理解

            所以   this.wins[0][0].1 就可以理解为 在 x=0, y=0, 上有第一种赢法
                this.wins[0][12].1 就可以理解为 在 x=0, y=1, 上有第一种赢法
                ......

            以上this.wins[0][0],this.wins[0][13]...可以看作是 this.wins[x][y]
            所以第一种赢法的坐标就是: [0,0] [0,1] [0,2] [0,3] [0,4] 
        */
      }
    }
  }

  for (var y = 0; y < this.col; y++) { //横向所有赢法, 同纵向赢法一样,也是15 * 11种
    for (var x = 0; x < this.col-4; x ++) {
      this.winsCount ++;
      for (var k = 0; k < 5; k ++) {
        this.wins[x+k][y][this.winsCount] = true;
      }
    }
  }

交叉赢法

JS+canvas五子棋人机对战实现步骤详解

/*
    交叉赢法
  */
  for (var x = 0; x < this.col-4; x++) { // 左 -> 右 开始的所有交叉赢法 总共11 * 11种
    for (var y = 0; y < this.col-4; y ++) {
      this.winsCount ++;

      /*
      如:
      1. [0,0]
        [1,1]
        [2,2]
        [3,3]
        [4,4]
      
      2. [0,1]
        [1,2]
        [2,3]
        [3,4]
        [4,5]

      3. [0,2]
        [1,3]
        [2,4]
        [3,5]
        [4,6]
      ...

        [1,0]
        [2,1]
        [3,2]
        [4,3]
        [5,5]

      相当于从左至右 一列列计算过去

      */

      for (var k = 0; k < 5; k ++) {
        this.wins[x+k][y+k][this.winsCount] = true;
      }
    }
  }

  for (var x = this.col-1; x >= 4; x --) { //右 -> 左 开始的所有交叉赢法 总共11 * 11种
    for (var y = 0; y < this.col-4; y ++) {
      this.winsCount ++;
      for (var k = 0; k < 5; k ++) {
        this.wins[x-k][y+k][this.winsCount] = true;
      }
    }
  }
}

9. 落子实现

//落子实现
Gobang.prototype.dorpChess = function(){
  var that = this;
  this.canvas.addEventListener('click', function(e) {
    // 判断是否结束
    if (that.over) return;

    var x = Math.floor((e.offsetX)/30),
      y = Math.floor((e.offsetY)/30);

    //判断该棋子是否已存在
    if (that.allChesses[x][y]) return;

    // 检查落子情况
    that.checkChess(x, y)

    if (!that.over) {
      that.player = false;
      that.computerDropChess();//计算机落子
    }
  })
}


//检查落子情况
Gobang.prototype.checkChess = function(x, y){
  //画棋
  this.drawChess(x, y, this.player);
  //记录落下的棋子
  this.existChesses.push({
    x: x,
    y: y,
    player: this.player
  });
  //该位置棋子置为true,证明已经存在
  this.allChesses[x][y] = true;

  this.currWinChesses(x, y, this.player);
}

//判断当前坐标赢的方法各自拥有几粒棋子
Gobang.prototype.currWinChesses = function(x, y, player){
  var currObj = player ? this.myWins : this.computerWins;
  var enemyObj = player ? this.computerWins : this.myWins;
  var currText = player ? '我' : '电脑';
  for (var i = 1; i <= this.winsCount; i++) {
    if (this.wins[x][y][i]) { //因为赢法统计是从1开始的 所以对应我的赢法需要减1
      currObj[i-1] ++; // 每个经过这个点的赢法都增加一个棋子;

      enemyObj[i-1] = 6; //这里我下好棋了,证明电脑不可能在这种赢法上取得胜利了, 置为6就永远不会到5

      if (currObj[i-1] === 5) { //当达到 5 的时候,证明我胜利了
        alert(currText+'赢了')
        this.over = true;
      }
    }
  }
}

10. 计算机落子实现

// 计算机落子
Gobang.prototype.computerDropChess = function(){
  var myScore = [], //玩家比分
    computerScore = [], // 电脑比分
    maxScore = 0; //最大比分
  

  //比分初始化
  var scoreInit = function(){
    for( var x = 0; x < this.col; x ++) { 
      myScore[x] = [];
      computerScore[x] = [];
      for (var y = 0; y < this.col; y ++) {
        myScore[x][y] = 0;
        computerScore[x][y] = 0;
      }
    }
  }
  scoreInit.call(this);

  //电脑待会落子的坐标
  var x = 0, y = 0; 

  // 基于我和电脑的每种赢法拥有的棋子来返回对应的分数
  function formatScore(o, n) { 
    if (o < 6 && o > 0) {
      var n = 10;
      for (var i = 0; i < o; i++) {
        n *= 3;
      }
      return n
    }
    return 0
  }

  // 获取没有落子的棋盘区域
  function existChess(arr) { 
    var existArr = [];
    for (var i = 0; i < arr.length; i++) {
      for (var j = 0; j < arr[i].length; j++) {
        if (!arr[i][j]) {
          existArr.push({x:i, y:j})
        }
      }
    }
    return existArr;
  }

  var exceptArr = existChess(this.allChesses);

  // 循环未落子区域,找出分数最大的位置
  for (var i = 0; i < exceptArr.length; i++) { 
    var o = exceptArr[i];

    // 循环所有赢的方法
    for (var k = 0; k < this.winsCount; k++) {

      //判断每个坐标对应的赢法是否存在
      if (this.wins[o.x][o.y][k]) {

        // 计算每种赢法,拥有多少棋子,获取对应分数
        // 电脑起始分数需要高一些,因为现在是电脑落子, 优先权大
        myScore[o.x][o.y] += formatScore(this.myWins[k-1], 10);
        computerScore[o.x][o.y] += formatScore(this.computerWins[k-1], 11); 
      }
    }

    //我的分数判断
    if (myScore[o.x][o.y] > maxScore) { //当我的分数大于最大分数时, 证明这个位置的是对我最有利的
      maxScore = myScore[o.x][o.y];
      x = o.x;
      y = o.y;
    }else if (myScore[o.x][o.y] === maxScore) { //当我的分数与最大分数一样时, 证明我在这两个位置下的效果一样, 所以我们应该去判断在这两个位置时,电脑方对应的分数
      if (computerScore[o.x][o.y] > computerScore[x][y]) {
        x = o.x;
        y = o.y;
      }
    }

    // 电脑分数判断, 因为是电脑落子, 所以优先权大
    if (computerScore[o.x][o.y] > maxScore) {
      maxScore = computerScore[o.x][o.y];
      x = o.x;
      y = o.y;
    }else if (computerScore[o.x][o.y] === maxScore) {
      if (myScore[o.x][o.y] > myScore[x][y]) {
        x = o.x;
        y = o.y;
      }
    }
  }

  this.checkChess(x, y)

  if (!this.over) {
    this.player = true;
  }
}
var gobang = new Gobang();
gobang.init()

github地址

线上地址

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持三水点靠木。

Javascript 相关文章推荐
javascript 自定义事件初探
Aug 21 Javascript
js播放wav文件(源码)
Apr 22 Javascript
js冒泡、捕获事件及阻止冒泡方法详细总结
May 08 Javascript
浅谈jQuery为哪般去掉了浏览器检测
Aug 29 Javascript
AngularJS实现按钮提示与点击变色效果
Sep 07 Javascript
JavaScript实现两个select下拉框选项左移右移
Mar 09 Javascript
BootStrap给table表格的每一行添加一个按钮事件
Sep 07 Javascript
Babel 入门教程学习笔记
Jun 13 Javascript
vue+element实现表格新增、编辑、删除功能
May 28 Javascript
解决使用layui的时候form表单中的select等不能渲染的问题
Sep 18 Javascript
vue插件--仿微信小程序showModel实现模态提示窗功能
Aug 19 Javascript
用JS创建一个录屏功能
Nov 11 Javascript
JS删除数组指定值常用方法详解
Jun 04 #Javascript
Vue项目接入Paypal实现示例详解
Jun 04 #Javascript
Paypal支付不完全指北
Jun 04 #Javascript
微信小程序学习总结(五)常见问题实例小结
Jun 04 #Javascript
vue样式穿透 ::v-deep的具体使用
Jun 04 #Javascript
微信小程序学习总结(四)事件与冒泡实例分析
Jun 04 #Javascript
微信小程序学习总结(三)条件、模板、文件引用实例分析
Jun 04 #Javascript
You might like
通过PHP current函数获取未知字符键名数组第一个元素的值
2013/06/24 PHP
PHP让数组中有相同值的组成新的数组实例
2017/12/31 PHP
记录几个javascript有关的小细节
2007/04/02 Javascript
js用图作提交按钮或超连接
2008/03/26 Javascript
javascript面向对象的方式实现的弹出层效果代码
2010/01/28 Javascript
Jquery工作常用实例 使用AJAX使网页进行异步更新
2011/07/26 Javascript
jQuery1.6 使用方法二
2011/11/23 Javascript
深入理解JavaScript系列(9) 根本没有“JSON对象”这回事!
2012/01/15 Javascript
js随机颜色代码的多种实现方式
2013/04/23 Javascript
javascript firefox 自动加载iframe 自动调整高宽示例
2013/08/27 Javascript
JS定义网页表单提交(submit)的方法
2015/03/20 Javascript
完美实现bootstrap分页查询
2015/12/09 Javascript
基于JavaScript实现快速转换文本语言(繁体中文和简体中文)
2016/03/07 Javascript
基于jQuery实现Ajax验证用户名是否存在实例
2016/03/30 Javascript
Bootstrap 附加导航(Affix)插件实例详解
2016/06/01 Javascript
react同构实践之实现自己的同构模板
2019/03/13 Javascript
小程序自定义模板实现吸顶功能
2020/01/08 Javascript
react-native 实现购物车滑动删除效果的示例代码
2021/01/15 Javascript
[01:02:25]2014 DOTA2华西杯精英邀请赛5 24 NewBee VS VG
2014/05/25 DOTA
[57:38]2018DOTA2亚洲邀请赛3月30日 小组赛A组 OpTic VS OG
2018/03/31 DOTA
python文件和目录操作函数小结
2014/07/11 Python
详细介绍Python中的偏函数
2015/04/27 Python
老生常谈Python进阶之装饰器
2017/05/11 Python
Python数据分析:手把手教你用Pandas生成可视化图表的教程
2018/12/15 Python
在Pycharm中修改文件默认打开方式的方法
2019/01/17 Python
Python 读取串口数据,动态绘图的示例
2019/07/02 Python
解决tensorflow/keras时出现数组维度不匹配问题
2020/06/29 Python
全国税务系统先进集体事迹材料
2014/05/19 职场文书
医院安全生产月活动总结
2014/07/05 职场文书
单位活动策划方案
2014/08/17 职场文书
领导班子民主生活会整改措施(工商局)
2014/09/21 职场文书
班主任高考寄语
2015/02/26 职场文书
高一语文教学反思
2016/02/16 职场文书
中国古代史学名著《战国策》概述
2019/08/09 职场文书
opencv深入浅出了解机器学习和深度学习
2022/03/17 Python
GoFrame框架数据校验之校验结果Error接口对象
2022/06/21 Golang