我有以下代码 http://jsfiddle.net/zander_pope/307n91bj/
这是isVictory函数:
function isVictory(placedX, placedY) {
var i, j, x, y, maxX, maxY, steps, count = 0,
directions = [
{ x: 0, y: 1 }, // North-South
{ x: 1, y: 0 }, // East-West
{ x: 1, y: 1 }, // Northeast-Southwest
{ x: 1, y: -1 } // Southeast-Northwest
];
// Check all directions
outerloop:
for (i = 0; i < directions.length; i++, count = 0) {
// Set up bounds to go 3 pieces forward and backward
x = Math.min(Math.max(placedX - (3 * directions[i].x), 0), circle.length - 1);
y = Math.min(Math.max(placedY - (3 * directions[i].y), 0), circle[0].length - 1);
maxX = Math.max(Math.min(placedX + (3 * directions[i].x), circle.length - 1), 0);
maxY = Math.max(Math.min(placedY + (3 * directions[i].y), circle[0].length - 1), 0);
steps = Math.max(Math.abs(maxX - x), Math.abs(maxY - y));
for (j = 0; j < steps; j++, x += directions[i].x, y += directions[i].y) {
if (circle[x][y] == circle[placedX][placedY]) {
// Increase count
if (++count >= 4) {
return true;
break outerloop;
}
} else {
// Reset count
count = 0;
}
}
}
//return count >= 4;
}
问题在于它没有检查东南 - 西北方向。 你能指出我的错误吗?