Please note that the code above is wrong. The final "if (...)" prior to "return t;" must go into the outer loop since it has to test, for each of the slider directions, whether the first non-empty square is occupied by an enemy piece. The code above does this for the last direction only, and will therefore produce wrong values (too small in many cases).outAtime wrote:mcostalba wrote:Try this:outAtime wrote: returns a compile warning 'q' might be used uninitialized in this function.Code: Select all
int piece_mobility_white(int sq, int offsets[]) { int q, *c = offsets, t = 0; do { for (q = sq + *c; board[q] == npiece; q += *c) t++; } while (*++c); if (board[q] && ((board[q] & 1) == 0)) t++; return t; }
An improved version (though still without weights) could look like this:
Code: Select all
int slider_mobility(int sq, int color, int offsets[])
{
// parameter "color": 0 for white, for black
int q, *c = offsets, t = 0;
do {
for (q = sq + *c; board[q] == npiece; q += *c) {
t++;
}
// if white pieces are encoded by odd numbers and black by even numbers
// then the test for enemy pieces is "!= color"
if (board[q] && ((board[q] & 1) != color)) {
t++;
}
} while (*++c);
return t;
}