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

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]
];


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


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);
}


posted @ 2015-01-05 21:41 里沃特 阅读(...) 评论(...) 编辑 收藏