实现过程是
var QiZi = function (qipan, r, c, text, isred) {
this.text = text;
this.isred = isred;
this.ctx = qipan.ctx;
this.qipan = qipan;
this.r = r;
this.c = c;
this.draw();
var that = this, maxR = that.qipan.maxR, maxC = that.qipan.maxC;
this.checkstep = (function () {
if (text == "兵" || text == "卒") {
if (that.r > 4) {
return function (r, c) {
return this.r > 4 && (this.r - 1) == r && this.c == c
|| this.r <= 4 && this.r == r && ((this.c - 1) == c || (this.c + 1) == c)
|| this.r <= 4 && (this.r - 1) == r && this.c == c;
}
} else {
return function (r, c) {
return this.r < 5 && (this.r + 1) == r && this.c == c || this.r >= 5 && this.r == r && ((this.c - 1) == c || (this.c + 1) == c) || this.r >= 5 && (this.r + 1) == r && this.c == c;
}
}
} else if (text == "炮") {
return function (r, c) {
if (this.r != r && this.c != c) {
return false;
}
var bodys = this.qipan.bodys, step = this.getMoveRange(r, c);
return bodys[r][c].qizi != null && step == 1 || bodys[r][c].qizi == null && step == 0;
}
} else if (text == "车" || text == "車") {
return function (r, c) {
if (this.r != r && this.c != c) {
return false;
}
return this.getMoveRange(r, c) == 0;
}
} else if (text == "马" || text == "馬") {
return function (r, c) {
return this.r - 2 == r && this.c - 1 == c && this.isEnemyOrEmpty(this.r - 1, this.c)
|| this.r - 2 == r && this.c + 1 == c && this.isEnemyOrEmpty(this.r - 1, this.c)
|| this.r + 2 == r && this.c - 1 == c && this.isEnemyOrEmpty(this.r + 1, this.c)
|| this.r + 2 == r && this.c + 1 == c && this.isEnemyOrEmpty(this.r + 1, this.c)
|| this.r - 1 == r && this.c - 2 == c && this.isEnemyOrEmpty(this.r, this.c - 1)
|| this.r - 1 == r && this.c + 2 == c && this.isEnemyOrEmpty(this.r, this.c + 1)
|| this.r + 1 == r && this.c - 2 == c && this.isEnemyOrEmpty(this.r, this.c - 1)
|| this.r + 1 == r && this.c + 2 == c && this.isEnemyOrEmpty(this.r, this.c + 1)
;
}
} else if (text == "相" || text == "象") {
return function (r, c) {
return this.r - 2 == r && this.c - 2 == c && this.isEnemyOrEmpty(this.r - 1, this.c - 1)
|| this.r - 2 == r && this.c + 2 == c && this.isEnemyOrEmpty(this.r - 1, this.c + 1)
|| this.r + 2 == r && this.c - 2 == c && this.isEnemyOrEmpty(this.r + 1, this.c - 1)
|| this.r + 2 == r && this.c + 2 == c && this.isEnemyOrEmpty(this.r + 1, this.c + 1)
;
}
} else if (text == "士" || text == "仕") {
if (that.r > 4) {
return function (r, c) {
var l = 7, l2 = 9, b = 3, b2 = 5;
if (r < l || r > l2 || c < b || c > b2) {
return false;
}
return this.r - 1 == r && this.c - 1 == c
|| this.r - 1 == r && this.c + 1 == c
|| this.r + 1 == r && this.c - 1 == c
|| this.r + 1 == r && this.c + 1 == c
;
}
} else {
return function (r, c) {
var l = 0, l2 = 3, b = 3, b2 = 5;
if (r < l || r > l2 || c < b || c > b2) {
return false;
}
return this.r - 1 == r && this.c - 1 == c
|| this.r - 1 == r && this.c + 1 == c
|| this.r + 1 == r && this.c - 1 == c
|| this.r + 1 == r && this.c + 1 == c
;
}
}
} else if (text == "将" || text == "帅") {
if (that.r > 4) {
return function (r, c) {
var l = 7, l2 = 9, b = 3, b2 = 5;
if (r < l || r > l2 || c < b || c > b2) {
return false;
}
return this.r - 1 == r && this.c == c
|| this.r + 1 == r && this.c == c
|| this.r == r && this.c - 1 == c
|| this.r == r && this.c + 1 == c
;
}
} else {
return function (r, c) {
var l = 0, l2 = 2, b = 3, b2 = 5;
if (r < l || r > l2 || c < b || c > b2) {
return false;
}
return this.r - 1 == r && this.c == c
|| this.r + 1 == r && this.c == c
|| this.r == r && this.c - 1 == c
|| this.r == r && this.c + 1 == c
;
}
}
}
return function () {
};
}());
}
QiZi.prototype = {
isEnemyOrEmpty: function (r, c) {
return this.qipan.bodys[r][c].qizi == null;
},