惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
宝玉的分享
宝玉的分享
V
Visual Studio Blog
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
Recent Announcements
Recent Announcements
F
Fortinet All Blogs
MongoDB | Blog
MongoDB | Blog
Hugging Face - Blog
Hugging Face - Blog
H
Help Net Security
博客园 - 三生石上(FineUI控件)
博客园_首页
Jina AI
Jina AI
aimingoo的专栏
aimingoo的专栏
S
SegmentFault 最新的问题
T
Tor Project blog
云风的 BLOG
云风的 BLOG
T
Troy Hunt's Blog
K
Kaspersky official blog
T
Tenable Blog
Project Zero
Project Zero
有赞技术团队
有赞技术团队
S
Schneier on Security
P
Proofpoint News Feed
C
Check Point Blog
GbyAI
GbyAI
P
Proofpoint News Feed
C
Cyber Attacks, Cyber Crime and Cyber Security
Microsoft Security Blog
Microsoft Security Blog
T
Threatpost
Know Your Adversary
Know Your Adversary
Help Net Security
Help Net Security
D
Docker
U
Unit 42
S
Securelist
月光博客
月光博客
博客园 - Franky
The Hacker News
The Hacker News
L
LangChain Blog
博客园 - 叶小钗
大猫的无限游戏
大猫的无限游戏
Hacker News: Ask HN
Hacker News: Ask HN
S
Security @ Cisco Blogs
AI
AI
人人都是产品经理
人人都是产品经理
The Last Watchdog
The Last Watchdog
Hacker News - Newest:
Hacker News - Newest: "LLM"
I
Intezer
The Register - Security
The Register - Security
阮一峰的网络日志
阮一峰的网络日志

博客园 - 里沃特

XR806开发板环境搭建记录 谈谈把网站迁移到阿里云的一些感想和其中遇到的一些问题 HTML5+JS 《五子飞》游戏实现(八)人机对战 HTML5+JS 《五子飞》游戏实现(七)游戏试玩 HTML5+JS 《五子飞》游戏实现(六)鼠标响应与多重选择 HTML5+JS 《五子飞》游戏实现(五)移动棋子 HTML5+JS 《五子飞》游戏实现(三)页面和棋盘棋子 HTML5+JS 《五子飞》游戏实现(二)路线分析和资源准备 HTML5+JS 《五子飞》游戏实现(一)规则 cocos2dx-2.2.1 免 Cygwin 环境搭建(Win8+VS2013+ADT Bundle+android-ndk-r9c) 深入理解 cocos2d-x 坐标系 在VS2010 下编译 cocos2d-x-2.1.4 FFmpeg 1.2 for Android 生成一个动态库 FFmpeg 1.2 for Android 编译动态库 Linq 批量更新数据 C# 统一对 try...catch 的调用,方便保存错误日志。 手机:由全触摸屏失效所想到的 愚人节奉献给大家的礼物,敬请收下 C# 让控件全屏显示(WinForm)
HTML5+JS 《五子飞》游戏实现(四)夹一个和挑一对
里沃特 · 2015-01-12 · via 博客园 - 里沃特

在第一章我们已经说了怎么才能“夹一个”以及怎样才能挑一对,但那毕竟只是书面上的,对码农来讲,我们还是用代码讲解起来会更容易了解。

为了更容易对照分析,我们先把路线再次贴出来:

    // 可走的路线
    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]]]);// 找到了

现在实现了两个基本的函数,下一章里沃特再跟大家分析移动棋子。本章实现的两个函数归纳如下:

    // 是否可“挑一对”
    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+JS 《五子飞》游戏实现(五)移动棋子

HTML5+JS 《五子飞》游戏实现(六)鼠标响应与多重选择