新浦京娱乐场官网-301net-新浦京娱乐www.301net
做最好的网站

只要找出符合条件的路线和位置就可把对方的棋

HTML5 JS 《五子飞》游戏实现(四)夹一个和挑一对,html5《五子飞》

在首先章我们早已说了怎么才具“夹一个”以及怎么样技艺挑一对,但那到底只是书面上的,对码农来说,大家依旧用代码讲授起来会更易于驾驭。

 

为了更易于对照分析,大家先把渠道再度贴出来:

    // 可走的路线
    this.lines = [
        [ 0,  1,  2,  3,  4],
        [ 5,  6,  7,  8,  9],
        [10, 11, 12, 13, 14],
        [15, 16, 17, 18, 19],
        [20, 21, 22, 23, 24],
        [ 0,  5, 10, 15, 20],
        [ 1,  6, 11, 16, 21],
        [ 2,  7, 12, 17, 22],
        [ 3,  8, 13, 18, 23],
        [ 4,  9, 14, 19, 24],
        [ 0,  6, 12, 18, 24],
        [ 4,  8, 12, 16, 20],
        [ 2,  6, 10],
        [ 2,  8, 14],
        [10, 16, 22],
        [14, 18, 22]
    ];              

 

一、夹一个:

基于下面给出的一定量路径中,要促成“夹三个”,首页这颗棋子的index得在[0,1,2...24]中间,大家循环寻找每条门路,只要寻觅符合条件的门路和地点就可把对方的棋类给吃掉。

首先我们寻找棋子的目的地方是在哪条路线中:

int index = $.inArray(chess.point.index, this.lines[i]);//chess被移动的棋子,下同
if(index!=-1)//...

下一场再寻找该条线上能被吃掉的棋子是哪三个。要是根据水平方一向看,被吃掉的棋类有望在左边,也会有希望在侧面,借使在右臂,那么该方还只怕有几个棋子应该在被吃掉的棋子的侧边:

var p1 = index > 1 ? this.chesses[this.lines[i][index - 1]].player : Player.None;
    if (p1 != Player.None && p1 != chess.player) {
        if (this.chesses[this.lines[i][index - 2]].player == chess.player) {
            //...找到符合条件的路线

同理,倘诺被吃掉的棋子在右侧,那么该方还会有三个棋子应该在被吃掉的棋类的侧边:

var p2 = index < this.lines[i].length - 2 ? this.chesses[this.lines[i][index   1]].player : Player.None;
    if (p2 != Player.None && p2 != chess.player) {
        if (this.chesses[this.lines[i][index   2]].player == chess.player) {
            //...找到符合条件的路线

但是,因为依照准绳,能夹对方棋子的同期,该条路线上仅且不得不有三颗棋子,已方两颗,对方一颗,其余岗位上是不能够有棋子存在的:

对此在左边的情事:

var bfind = true;// 是否找到能被吃的棋子
for (j = 0; j < index - 2; j  ) {
    if (this.chesses[this.lines[i][j]].player != Player.None) { bfind = false; break; }
}
if (!bfind) return;
bfind = true;
for (j = index   1; j < this.lines[i].length; j  ) {
    if (this.chesses[this.lines[i][j]].player != Player.None) { bfind = false; break; }
}
if (!bfind) return;
chessArray.push([this.chesses[this.lines[i][index - 1]]]);// 找到了

对此在右侧的情状:

var bfind = true;
for (j = 0; j < index; j  ) {
    if (this.chesses[this.lines[i][j]].player != Player.None) { bfind = false; break; }
}
if (!bfind) return;
bfind = true;
for (j = index   3; j < this.lines[i].length; j  ) {
    if (this.chesses[this.lines[i][j]].player != Player.None) { bfind = false; break; }
}
if (!bfind) return;
chessArray.push([this.chesses[this.lines[i][index   1]]]);// 找到了

 对于找到可以被夹掉的棋类我们记录下来,存到 chessArray 里面,以便进行其余操作。

二、挑一对:

长期以来,大家先寻觅棋子在哪条门路中:

index = $.inArray(chess.point.index, this.lines[i]);
if (index > 0 && index < this.lines[i].length - 1) {

下一场相对于夹叁个来讲轻易比很多,大家只要寻找该棋子左右邻座的多个棋子是对方的棋子,且该条直线上别样位置都以空位就行了。

先找寻左右紧邻的两颗棋子:

var p1 = this.chesses[this.lines[i][index - 1]].player;
var p2 = this.chesses[this.lines[i][index   1]].player;
if (p1 != chess.player && p2 != chess.player && p1 != Player.None && p2 != Player.None) {

再推断任何职责是空位:

var bfind = true;
for (j = 0; j < index - 1; j  ) {
    if (this.chesses[this.lines[i][j]].player != Player.None) { bfind = false; break; }
}
if (!bfind) return;
bfind = true;
for (j = this.lines[i].length - 1; j > index   1; j--) {
    if (this.chesses[this.lines[i][j]].player != Player.None) { bfind = false; break; }
}
if (!bfind) return;
chessArray.push([this.chesses[this.lines[i][index - 1]], this.chesses[this.lines[i][index   1]]]);// 找到了

 

今日落到实处了七个核心的函数,下一章里Wat再跟大家分析运动棋子。本章实现的四个函数总结如下:

图片 1 // 是否可“挑一对” this.canCarry = function (chess) { var p1, p2, j, index, bfind, chessArray = []; for (var i = 0; i < this.lines.length; i ) { index = $.inArray(chess.point.index, this.lines[i]); if (index > 0 && index < this.lines[i].length - 1) { p1 = this.chesses[this.lines[i][index - 1]].player; p2 = this.chesses[this.lines[i][index 1]].player; if (p1 != chess.player && p2 != chess.player && p1 != Player.None && p2 != Player.None) { bfind = true; for (j = 0; j < index - 1; j ) { if (this.chesses[this.lines[i][j]].player != Player.None) { bfind = false; break; } } if (!bfind) continue; bfind = true; for (j = this.lines[i].length - 1; j > index 1; j--) { if (this.chesses[this.lines[i][j]].player != Player.None) { bfind = false; break; } } if (!bfind) continue; chessArray.push([this.chesses[this.lines[i][index - 1]], this.chesses[this.lines[i][index 1]]]); } } } return chessArray.length == 0 ? false : chessArray; }; // 是否可“夹一个” this.canClip = function (chess) { var p1, p2, j, index, bfind, chessArray = []; for (var i = 0; i < this.lines.length; i ) { index = $.inArray(chess.point.index, this.lines[i]); if (index != -1) { p1 = index > 1 ? this.chesses[this.lines[i][index - 1]].player : Player.None; p2 = index < this.lines[i].length - 2 ? this.chesses[this.lines[i][index 1]].player : Player.None; if (p1 != Player.None && p1 != chess.player) { if (this.chesses[this.lines[i][index - 2]].player == chess.player) { bfind = true; for (j = 0; j < index - 2; j ) { if (this.chesses[this.lines[i][j]].player != Player.None) { bfind = false; break; } } if (!bfind) continue; bfind = true; for (j = index 1; j < this.lines[i].length; j ) { if (this.chesses[this.lines[i][j]].player != Player.None) { bfind = false; break; } } if (!bfind) continue; chessArray.push([this.chesses[this.lines[i][index - 1]]]); } } else if (p2 != Player.None && p2 != chess.player) { if (this.chesses[this.lines[i][index 2]].player == chess.player) { bfind = true; for (j = 0; j < index; j ) { if (this.chesses[this.lines[i][j]].player != Player.None) { bfind = false; break; } } if (!bfind) continue; bfind = true; for (j = index 3; j < this.lines[i].length; j ) { if (this.chesses[this.lines[i][j]].player != Player.None) { bfind = false; break; } } if (!bfind) continue; chessArray.push([this.chesses[this.lines[i][index 1]]]); } } } } return chessArray.length == 0 ? false : chessArray; }; View Code

 

HTML5 JS 《五子飞》游戏达成(一)准绳

HTML5 JS 《五子飞》游戏实现(二)路径剖判和能源筹划

HTML5 JS 《五子飞》游戏达成(三)页面和棋盘棋子

《五子飞》游戏完结(四)夹多个和挑一对,html5《五子飞》 在第一章大家早已说了怎么技术夹三个以及怎么样本领挑一对,但那究竟...

本文由新浦京娱乐场官网-301net-新浦京娱乐www.301net发布于www.301net,转载请注明出处:只要找出符合条件的路线和位置就可把对方的棋

您可能还会对下面的文章感兴趣: