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

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

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

HTML5+JS 《五子飞》游戏实现(一)规则

HTML5+JS 《五子飞》游戏实现(二)路线分析和资源准备

HTML5+JS 《五子飞》游戏实现(三)页面和棋盘棋子

HTML5+JS 《五子飞》游戏实现(五)移动棋子

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

HTML5+JS 《五子飞》游戏实现(四)夹一个和挑一对的更多相关文章

  1. HTML5+JS 《五子飞》游戏实现(八)人机对战

    要想实现人机对战,就必须让电脑自动下棋,而且要知道自动去查找对方的棋子,看看有没有可以挑一对的,有没有可以夹一个的,这样下起来才有意思. 当电脑用户下完棋后,电脑应立即搜索用户的棋子,然后如果没有被吃 ...

  2. HTML5+JS 《五子飞》游戏实现(七)游戏试玩

    前面第一至第六章我们已经把<五子飞>游戏的基本工作都已经讲得差不多了,这一章主要是把所有的代码分享给大家,然后小伙伴们也可以玩一玩. 至于人机对战的我们放到后面讲进行分析. 试玩地址:ht ...

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

    上一章我们提到了如果有多条线上的棋子可以被吃掉,那么游戏需要提示用户,让用户选择吃哪条线上的.另外因为是网页游戏,所以一定要实现鼠标单击棋子可以进行操作. 当鼠标移动棋子上面后,切换鼠标指针为手形,移 ...

  4. HTML5+JS 《五子飞》游戏实现(五)移动棋子

    上一章 我们知道了怎么处理两个重要的吃棋动作,想要吃对方的棋子,首先得移动自己的棋子.现在里沃特跟大家分享分享,怎么移动棋子. 想要移动棋子,在页面上,首先要点击一下要移动的棋子,然后再点击一下目标位 ...

  5. HTML5+JS 《五子飞》游戏实现(三)页面和棋盘棋子

    前面两节,我们已经对<五子飞>有个初步的认识,对走棋路线也有了基本的了解,现在里沃特继续跟大家分享HTML页面,另外把棋盘棋子也画出来. 演示地址:http://www.lyout.com ...

  6. HTML5+JS 《五子飞》游戏实现(一)规则

    很久没写文章了,这个游戏其实已经写了有段时间了,一直没有完善,赶在新年之际,分享给大家. 该<五子飞>游戏,不是平常大家所说的<五子棋>,这个玩法简单,是我们老家儿时常玩的一种 ...

  7. HTML5+JS 《五子飞》游戏实现(二)路线分析和资源准备

    上一节 里沃特与我们分享了<五子飞>的下棋规则,可能有些伙伴看得不清楚,像我们码农还是看到代码比较靠谱.下面就把可以走棋的路线跟大家说一下. 假设从左上角开始,以0开始编号,往右数(没看第 ...

  8. 用html5+js实现掌机游戏赛车demo

    最近无聊,用html5+js做了一个以前玩过的掌机赛车游戏,顺便学习一下画布的api以及巩固一下js基础. 游戏界面,没做什么美化. 游戏规则:游戏界面分为三列,黑色方块随机落下,红色方块可以在三列自 ...

  9. 100行JS实现HTML5的3D贪吃蛇游戏

    js1k.com收集了小于1k的javascript小例子,里面有很多很炫很酷的游戏和特效,今年规则又增加了新花样,传统的classic类型基础上又增加了WebGL类型,以及允许增加到2K的++类型, ...

随机推荐

  1. redis 下载启动,设置、查询超时时间

    1.定义 redis是一个key-value存储系统.和Memcached类似,它支持存储的value类型相对更多,包括string(字符串).list(链表).set(集合).zset(sorted ...

  2. C#基础---事件的使用

    一:什么是事件     事件是可以被控件识别的操作,如按下确定按钮,选择某个单选按钮或者复选框.每一种控件有自己可以识别的事件,如窗体的加载.单击.双击等事件,编辑框(文本框)的文本改变事件,等等.事 ...

  3. C#基础---IComparable用法,实现List<T>.sort()排序

    List<T>.sort()可以实现对T的排序,比如List<int>.sort()执行后集合会按照int从小到大排序.如果T是一个自定义的Object,可是我们想按照自己的方 ...

  4. 使用jMeter测试Solr服务接口

    之前一直用ab做简单的服务接口测试,ab功能强悍,使用简单,但是没有生成专题图和表格等功能,因此,我们决定使用jmeter来作为我们测试工具.接下来,我们将详细介绍jmeter使用的步骤,主要包括:j ...

  5. Java 三大特性——封装、继承、多态

    一.封装 封装,实际就是把属于同一类事物的共性(包括属性与方法)归到一个类中,以方便使用. 概念:在面向对象程式设计方法中,封装(英语:Encapsulation)是指,一种将抽象性函式接口的实作细节 ...

  6. Quantum Bogo sort浅谈

    1.普通的猴子排序(bogo sort) 猴子排序百科 en.wikipedia.org/wiki/Bogosort 不停的随机打乱序列,然后检查,直到排好序 复杂度O(n*n!) while not ...

  7. 网页动物园2.0发布,经过几个月的努力,采用JAVA编写!

    网页动物园2.0发布,经过几个月的努力,采用JAVA编写! 网页动物园2.0 正式发布!游戏发布 游戏名称: 网页动物园插件 游戏来源: 原创插件 适用版本: Discuz! X1.5 - X3.5 ...

  8. Java之单例模式

    单例模式: public class Person{ public static Person per//定义一个静态变量,用来储存当前类的对象 private Person()//构造方法私有化 { ...

  9. 安卓手机已保存WiFi密码查看助手(开源)

    一.需求分析 最近电脑需要连接WiFi,却发现WiFi密码给忘记了.而手机里有保存过的WiFi密码,但是在手机的设置界面看不到. 虽然已经有一些可以查看WiFi密码的app,但是主要还是担心密码被那些 ...

  10. window10 安装出现the error code is 2503错误的解决方法

    window10 安装出现the error code is 2503错误的解决方法:  设置 C:\WINDOWS\TEMP的权限