if we have a grid that has some stuff and we need to search it in several directions, what's the best approach ?

Loop each direction?

Or have a single loop like:

Code: Select all

```
interation = 0;
while (condition)
iteration++;
x = i+iteration;
y = j+iterarion;
/* check direction 1 */
if ( first ) {
if ( matrix[x][y] is not empty ) {
if ( other condition ) {
return found; /* break the loop */
}
else {
first = 0; /* so it doesnt keep checking this direction if theres a block or no capture */
}
}
}
x = i-iteration;
y = y-iteration;
/* check direction 2 */
if ( second ) {
if ( matrix[x][y] is not empty ) {
if () {
}
else {
second = 0;
}
}
}
/* check other directions changing x and y */
}
```

Example:

Given this matrix,

00000

00010

00000

02002

00000

Check if piece called '1' can capture some piece (as in chess bishops), like pieces numbered 2, if it can move in any diagonal any number of steps. The problem could also be, "if it can move in any direction".

some additional info:

* function only needs to check if theres a "capture" (so it can return as soon as it finds one, not needing to continue the other checks).

* if there's a block in either direction (like a piece of the same color), that direction should/must not be checked anymore.

Thanks.