五子棋游戏最简单的玩法,五子棋怎么玩得最简单

首页 > 游戏 > 作者:YD1662023-05-18 22:08:45

  1. 左斜线情况 - 此时白子获胜

五子棋游戏最简单的玩法,五子棋怎么玩得最简单(5)

  1. 右斜线情况 - 此时黑子获胜

五子棋游戏最简单的玩法,五子棋怎么玩得最简单(6)

否则,黑白双方打成平手(即平局)。

项目的难点

1. 将 canvas 上绘制的棋盘和矩阵一一对应

五子棋游戏最简单的玩法,五子棋怎么玩得最简单(7)

体验下代码你就明白了:

// 监听鼠标点击棋盘的位置 listenDownChessman() { this.checkerboardDom.onclick = event => { let { offsetX: x, offsetY: y } = event; const { width, height } = this.lattice; x = Math.round((x - 15) / width); y = Math.round((y - 15) / height); if(this.checkerboardMatrix[x][y] !== undefined && Object.is(this.checkerboardMatrix[x][y], 0)) { this.checkerboardMatrix[x][y] = this.role; // 刻画棋子 this.drawChessman({ x, y }, Object.is(this.role, EnumRoles.BLACK)); this.role = Object.is(this.role, EnumRoles.BLACK) ? EnumRoles.WHITE : EnumRoles.BLACK; // 设置当前执棋 if(!this.win) { // 游戏中 this.setCurrentRole(); } else { // 游戏结束 Object.is(this.role, EnumRoles.BLACK) ? this.currentRoleDom.setAttribute("class", "chessman win-white") : this.currentRoleDom.setAttribute("class", "chessman win-black") this.message = '赢得本场胜利' this.setResultMsgHint(); } } } } 复制代码

我们需要获取 canvas 上相对原点(左上角)的偏移值,计算出在矩阵上对应的位置。然后判断该位置是否已经落子,如果没有落子,则进行棋子的绘制。

2. 判断输赢

在上面获胜情况中我们已经了解。下面我们看看代码实现:

/* * @method gameReferee 游戏裁判 * @param coordinate 坐标 (x, y) * @paran role 角色 */ gameReferee(coordinate, role) { const { x, y } = coordinate; if( x === undefined || y === undefined || role === undefined) return; // 连*的次数 let countContinuous = 0; // x 轴的情况 const XContinuous = this.checkerboardMatrix.map(x => x[y]); // y 轴的情况 const YContinuous = this.checkerboardMatrix[x]; const S1Continuous = []; const S2Continuous = []; this.checkerboardMatrix.forEach((_x,i) => { // 左斜线 const S1Item = _x[y - (x - i)]; if(S1Item !== undefined){ S1Continuous.push(S1Item); } // 右斜线 const S2Item = _x[y (x - i)]; if(S2Item !== undefined) { S2Continuous.push(S2Item); } }); // 当前落棋点所在的X轴/Y轴/交叉斜轴,只要有能连起来的5个子的角色即有胜者 [XContinuous, YContinuous, S1Continuous, S2Continuous].forEach(axis => { if(axis.some((x, i) => axis[i] !== 0 && axis[i - 2] === axis[i - 1] && axis[i - 1] === axis[i] && axis[i] === axis[i 1] && axis[i 1] === axis[i 2])) { countContinuous } }); if(countContinuous) { this.win = true; this.checkerboardDom.onclick = null; // 游戏结束,不允许点击 } } 复制代码

上面代码的注释已经讲解得很清楚了。简单来说,我们就是将二维数组分成四种情况处理,将其形成四个一维数组,再对四个一维数组进行判断,如果它们中有连续 5 个相同的数值,则可判定输赢。

比如,在 15 * 15 的棋盘中,某次落棋后,可得:

XContinuous = [0, 1, 2, 2, 2, 2, 2, 0, 2, 1, 0, 1, 1, 0, 0] 复制代码

0 代表空, 1 代表黑子,2 代表白子

这里 XContinuous 横轴中就有连续五个白子,则白方获胜。

体验地址:

赠人玫瑰,手留花香。俺不要玫瑰,只要读者的一个举手之劳的赞~ 逃:)

上一页12末页

栏目热文

文档排行

本站推荐

Copyright © 2018 - 2021 www.yd166.com., All Rights Reserved.