嗨,大家好,我真的需要帮助,即时通讯在计算真的不好,我在这里真的卡住。
所以我做了一个国际象棋游戏vue.js
而现在我试着在每件找出可能的行动,并且能够修为knight, pawn and bishop
法律动作验证
与主教验证我遇到一个问题工作时,就是为了能够验证,如果有一块主教之前站立
请参阅图像了解更多。
你看到红色的数字是如何越过棋子,并继续UPP。它应该停止在典当行的身影。
这里是我的主教计算代码。如果它的可能的,如果你也为女王提供验证和其他部分将是真正的帮助。
var el = { };
// sample Data
el.whiteMoves = [{"x":1,"y":1,"type":"Rook1","name":"1A","cleanType":"rook"},{"x":8,"y":1,"type":"Rook2","name":"1H","cleanType":"rook"},{"x":2,"y":1,"type":"Knight1","name":"1B","cleanType":"knight"},{"x":7,"y":1,"type":"Knight2","name":"1G","cleanType":"knight"},{"x":3,"y":1,"type":"Bishop1","name":"1C","cleanType":"bishop"},{"x":6,"y":1,"type":"Bishop2","name":"1F","cleanType":"bishop"},{"x":4,"y":1,"type":"Queen","name":"1D","cleanType":"queen"},{"x":5,"y":1,"type":"King","name":"1E","cleanType":"king"},{"x":1,"y":2,"type":"Pawn1","name":"2A","cleanType":"pawn"},{"x":2,"y":2,"type":"Pawn2","name":"2B","cleanType":"pawn"},{"x":3,"y":2,"type":"Pawn3","name":"2C","cleanType":"pawn"},{"x":4,"y":2,"type":"Pawn4","name":"2D","cleanType":"pawn"},{"x":5,"y":2,"type":"Pawn5","name":"2E","cleanType":"pawn"},{"x":6,"y":2,"type":"Pawn6","name":"2F","cleanType":"pawn"},{"x":7,"y":2,"type":"Pawn7","name":"2G","cleanType":"pawn"},{"x":8,"y":2,"type":"Pawn8","name":"2H","cleanType":"pawn"}];
el.blackMoves = [{"x":1,"y":8,"type":"Rook1","name":"8A","cleanType":"rook"},{"x":8,"y":8,"type":"Rook2","name":"8H","cleanType":"rook"},{"x":2,"y":8,"type":"Knight1","name":"8B","cleanType":"knight"},{"x":7,"y":8,"type":"Knight2","name":"8G","cleanType":"knight"},{"x":3,"y":8,"type":"Bishop1","name":"8C","cleanType":"bishop"},{"x":6,"y":8,"type":"Bishop2","name":"8F","cleanType":"bishop"},{"x":4,"y":8,"type":"Queen","name":"8D","cleanType":"queen"},{"x":5,"y":8,"type":"King","name":"8E","cleanType":"king"},{"x":1,"y":7,"type":"Pawn1","name":"7A","cleanType":"pawn"},{"x":2,"y":7,"type":"Pawn2","name":"7B","cleanType":"pawn"},{"x":3,"y":7,"type":"Pawn3","name":"7C","cleanType":"pawn"},{"x":4,"y":7,"type":"Pawn4","name":"7D","cleanType":"pawn"},{"x":5,"y":7,"type":"Pawn5","name":"7E","cleanType":"pawn"},{"x":6,"y":7,"type":"Pawn6","name":"7F","cleanType":"pawn"},{"x":7,"y":7,"type":"Pawn7","name":"7G","cleanType":"pawn"},{"x":8,"y":7,"type":"Pawn8","name":"7H","cleanType":"pawn"}]
el.rank = ["A", "B", "C", "D", "E", "F", "G", "H"];
var result = []
type = "white";
piece= "bishop";
var x = 5;
var y = 1;
var v = {
// the validation methods
bishop: function () {
var offSet = [];
for (var i = 1; i <= 8; i++) {
if (x + i < 8 && y + i < 8)
offSet.push({ x: x + i, y: y + i });
if (x + i < 8 && y - i < 8)
offSet.push({ x: x + i, y: y - i });
if (x - i < 8 && y + i < 8)
offSet.push({ x: x - i, y: y + i });
if (x - i < 8 && y - i < 8)
offSet.push({ x: x - i, y: y - i });
}
if (type == "white")
result = offSet.flatMap((item) => item.y + el.rank[item.x]).filter((item) => el.whiteMoves.filter((x) => x.name == item).length <= 0);
else result = offSet.flatMap((item) => item.y + el.rank[item.x]).filter((item) => el.blackMoves.filter((x) => x.name == item).length <= 0);
return result;
}
}
v[piece]();
// there is some invalid values like -5A or NaN but its not a problem these will be removed later on
console.log(result);
你for
循环根本不包括代码来检测碰撞。它会一路板的边缘。考虑断开回路为四个独立for
环路,在所述板的边缘或当它检测到碰撞每一个终止。碰撞需要换一块相同的颜色(非法移动)的或相反的颜色(捕获)的要被分开处理。
var dx = +1, dy = +1;
do {
x += dx;
y += dy;
// Running into any color piece terminates the loop.
// However, running into an opposite color piece adds one last legal move.
var onBoard = (x >= 0) && (x < 8) && (y >= 0) && (y < 8);
var samePiece = onBoard ? (detect_collision_with_same_color_piece) : false;
var oppPiece = onBoard ? (detect_collision_with_opp_color_piece) : false;
if (onBoard && !samePiece) {
offSet.push({ x: x, y: y });
}
} while (onBoard && !samePiece && !oppPiece);
很难提供精确的碰撞检测代码,所以我留下了一些占位符那里。一些额外的想法:
dx
和dy
遍历所有的+1和-1的组合,让你不重复上面的代码的四倍。x
和y
正确性每一次。例如,如果白色是移动和你垫白色棋子的矩阵,那么你就可以删除onBoard
一起;当samePiece
成为真正的循环将终止。这不,一旦你添加换位表增加你的董事会从64个平方到100,它可以是显著。