I have an array, lets say
var array = [ [1, 0, 0, 0, 0, 0, 0],
[0, 1, 0, 0, 0, 1, 0],
[0, 0, 1, 0, 1, 0, 0],
[0, 0, 0, 1, 0, 0, 0],
[0, 0, 1, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0]
]
and I would like create a to find any matches where a number appears four times diagonally.
Currently I am using
function checkDiagonal(array, bottomToTop) {
var Ylength = array.length;
var Xlength = array[0].length;
var maxLength = Math.max(Xlength, Ylength);
var temp;
var returnArray = [];
for (var k = 0; k <= 2 * (maxLength - 1); ++k) {
temp = [];
for (var y = Ylength - 1; y >= 0; --y) {
var x = k - (bottomToTop ? Ylength - y : y);
if (x >= 0 && x < Xlength) {
temp.push(array[y][x]);
}
}
if(temp.length > 0) {
returnArray.push(temp.join(''));
}
}
return returnArray;
}
however it doesn't always find all the solutions
Interesting case. Actually hard to find/write an easy method for that. I tried to understand your script but found it a bit hard to follow/debug, so tried to reproduce what you did in my own script and managed to get the desired result. It's more lines of codes than yours has but it has some variables declared together with some comments so it's easier to understand (for others, in the future).
Hope this helps:
function checkDiagonal(array, matchCount) {
var result = [];
if(array.length >= matchCount) {
// Search towards bottom-right.
result = result.concat(getDiagonalResult(array, matchCount, 1));
// Search towards top-right.
result = result.concat(getDiagonalResult(array, matchCount, -1));
} else {
// No use searching if not enough rows are present.
}
return result;
}
function getDiagonalResult(array, matchCount, direction) {
var result = [];
// Specific from and to points to only search in possible rows (e.g. no use searching top-right on first row).
var yFrom, yTo;
// Search direction (bottom-right vs top-right).
switch(direction) {
// Bottom-right.
case 1:
yFrom = 0;
yTo = (array.length - matchCount);
break;
// Top-right.
case -1:
yFrom = (matchCount - 1);
yTo = (array.length - 1);
break;
}
// Loop through all 'rows'.
for(var y = yFrom; y <= yTo; y++) {
// Loop through all 'columns'.
for(var x = 0; x <= (array[y].length - matchCount); x++) {
// Current value to match on.
var originalValue = array[y][x];
var matches = [];
// Get matches.
for(var i = 0; i < matchCount; i++) {
// Search direction (row up or down).
var yDirection = (i * direction);
var value = array[y+yDirection][x+i];
if(value === originalValue) {
matches.push(value);
}
}
if(matches.length == matchCount) {
result.push(matches.join(""));
}
}
}
return result;
}
var array = [
[1, 0, 0, 0, 0, 0, 0],
[0, 1, 0, 0, 0, 1, 0],
[0, 0, 1, 0, 1, 0, 0],
[0, 0, 0, 1, 0, 0, 0],
[0, 0, 1, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0]
];
console.log(checkDiagonal(array, 4));
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With