大家好,我真的需要帮助,我的计算能力很差,而且我真的被困在这里。
所以我用vue.js
制作了一个国际象棋游戏,现在我正试图找出每个棋子的可能动作,并且能够修正knight, pawn and bishop
的合法动作验证
在进行主教验证时遇到一个问题,就是要能够验证主教面前是否有一块东西
请查看图片以了解更多信息。
您会看到红色数字越过典当并继续上升。它应该停止在典当行。
这是我用于主教计算的代码。如果可能的话,如果您还可以提供对皇后区的验证,那么其他部分将真的很有帮助。
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);
答案 0 :(得分:5)
您的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平方,一旦添加了转置表,这将非常重要。