Saturday, 24 August 2013

Connect 4 game javascript diagonal checking from Southeast to Northwest

Connect 4 game javascript diagonal checking from Southeast to Northwest

This is related to the post "algorithm connect four javascript" answered
by user:462803 Koviko. I just used the codes of Koviko in my connect 4
javascript. But I encountered issue.
This is the codes of Koviko:
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 );
y = Math.min(Math.max(placedY - (3 * directions[i].y), 0),
circle[0].length );
maxX = Math.max(Math.min(placedX + (3 * directions[i].x),
circle.length ), 0);
maxY = Math.max(Math.min(placedY + (3 * directions[i].y),
circle[0].length ), 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;
}
I slightly edit the code. But that is the final code that is working with
my connect four game. The only problem is the Southeast to Northwest
traverse is not working, and it will only win this coordinates
Southeast-Northwest 6,0 5,1 4,2 3,3.
Though I have completed this in a hardcode way but I just want to finish
this game that using javascript math object for better runtime. Here is
the demo: http://jsfiddle.net/iahboy/Cwp8x/.
Thank you so much. Hope someone will help me.

No comments:

Post a Comment