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

推荐订阅源

A
Arctic Wolf
博客园 - 聂微东
F
Fortinet All Blogs
云风的 BLOG
云风的 BLOG
小众软件
小众软件
V
Visual Studio Blog
博客园 - 三生石上(FineUI控件)
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
Apple Machine Learning Research
Apple Machine Learning Research
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
The Cloudflare Blog
H
Hackread – Cybersecurity News, Data Breaches, AI and More
The GitHub Blog
The GitHub Blog
Y
Y Combinator Blog
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
博客园_首页
L
LangChain Blog
A
About on SuperTechFans
阮一峰的网络日志
阮一峰的网络日志
I
Intezer
T
The Blog of Author Tim Ferriss
Security Latest
Security Latest
C
CXSECURITY Database RSS Feed - CXSecurity.com
Know Your Adversary
Know Your Adversary
Simon Willison's Weblog
Simon Willison's Weblog
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
P
Palo Alto Networks Blog
Scott Helme
Scott Helme
S
Secure Thoughts
Spread Privacy
Spread Privacy
T
Threat Research - Cisco Blogs
Attack and Defense Labs
Attack and Defense Labs
P
Privacy & Cybersecurity Law Blog
O
OpenAI News
H
Heimdal Security Blog
www.infosecurity-magazine.com
www.infosecurity-magazine.com
Help Net Security
Help Net Security
C
Cyber Attacks, Cyber Crime and Cyber Security
Blog — PlanetScale
Blog — PlanetScale
GbyAI
GbyAI
G
Google Developers Blog
博客园 - Franky
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
K
Kaspersky official blog
Recent Commits to openclaw:main
Recent Commits to openclaw:main
T
Tor Project blog
D
Darknet – Hacking Tools, Hacker News & Cyber Security
T
Tenable Blog
Google Online Security Blog
Google Online Security Blog
PCI Perspectives
PCI Perspectives

博客园 - 里沃特

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 上把棋子画好。