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

推荐订阅源

Security Latest
Security Latest
量子位
博客园 - 三生石上(FineUI控件)
小众软件
小众软件
S
SegmentFault 最新的问题
The GitHub Blog
The GitHub Blog
AWS News Blog
AWS News Blog
T
Threat Research - Cisco Blogs
博客园 - Franky
Vercel News
Vercel News
H
Help Net Security
Martin Fowler
Martin Fowler
Security Archives - TechRepublic
Security Archives - TechRepublic
L
LINUX DO - 热门话题
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
L
Lohrmann on Cybersecurity
Cyberwarzone
Cyberwarzone
W
WeLiveSecurity
V2EX - 技术
V2EX - 技术
C
CERT Recently Published Vulnerability Notes
S
Secure Thoughts
C
Cyber Attacks, Cyber Crime and Cyber Security
B
Blog RSS Feed
H
Hacker News: Front Page
P
Proofpoint News Feed
博客园 - 聂微东
N
News and Events Feed by Topic
C
Cybersecurity and Infrastructure Security Agency CISA
D
Docker
博客园_首页
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
人人都是产品经理
人人都是产品经理
The Hacker News
The Hacker News
S
Security @ Cisco Blogs
博客园 - 【当耐特】
F
Fortinet All Blogs
The Register - Security
The Register - Security
A
About on SuperTechFans
D
Darknet – Hacking Tools, Hacker News & Cyber Security
S
Schneier on Security
NISL@THU
NISL@THU
Attack and Defense Labs
Attack and Defense Labs
Help Net Security
Help Net Security
Cisco Talos Blog
Cisco Talos Blog
月光博客
月光博客
IT之家
IT之家
有赞技术团队
有赞技术团队
Know Your Adversary
Know Your Adversary
Hugging Face - Blog
Hugging Face - Blog

博客园 - 里沃特

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-05 · via 博客园 - 里沃特

上一节 里沃特与我们分享了《五子飞》的下棋规则,可能有些伙伴看得不清楚,像我们码农还是看到代码比较靠谱。下面就把可以走棋的路线跟大家说一下。

假设从左上角开始,以0开始编号,往右数(没看第一节棋盘的先去看一下)(因为路线比较简单,就直接写固定的数据了):

1.横着走有5条直线:

var lines_h = [
        [ 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]
];

2.竖着走也有5条直线:

var lines_v = [
        [ 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]
];

3.另外还有6条斜线可走:

var lines_o = [
        [ 0,  6, 12, 18, 24],
        [ 4,  8, 12, 16, 20],
        [ 2,  6, 10],
        [ 2,  8, 14],
        [10, 16, 22],
        [14, 18, 22]
];

合起来即理论下可以走的路线如下:

// 可走的路线
var 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]
    ];              

分别用 A,B 表示对战双方:

var Player = { A: 0, B: 1, None: -1 };

棋盘我们直接在 canvas 上画,棋子准备两个小图片:

 

为棋子定义一个对象:

function Point(x, y, index) {
    this.x = x;
    this.y = y;
    this.index = index;
}
function Bounds(x, y, w, h) {
    this.x = x;
    this.y = y;
    this.w = w;
    this.h = h;

    this.toArray = function () {
        return [this.x, this.y, this.w, this.h];
    };

    this.toArrayXY = function () {
        return [this.x, this.y, this.x + this.w, this.y + this.h];
    };
}
// 棋子
function Chess(player) {
    this.player = player;
    this.point = new Point(-1, -1, -1);
    this.bounds = new Bounds(-1, -1, -1, -1);
    this.moveTo = function (chess) {
        chess.player = this.player;
        this.player = Player.None;
    };
}

棋盘上棋子定义(棋子初始化):

var i;
var cpc = 5;
var ctc = Math.pow(cpc, 2);
var chesses = [];

// 分配棋子
for (i = 0; i < cpc; i++) {
    chesses[i].player = Player.A;
}
for (i = cpc; i < ctc - cpc; i++) {
    chesses[i].player = Player.None;
}
for (i = ctc - cpc; i < ctc; i++) {
    chesses[i].player = Player.B;
}
for (i = 0; i < ctc; i++) {
    chesses[i].point = new Point(i % cpc, parseInt(i / cpc, 10), i);
}

这样,路线和棋子初始化就已经比较清楚了,下一节我们开始在 canvas 上把棋子画好。