Skip to content

Instantly share code, notes, and snippets.

@Ravenslofty
Created December 22, 2016 17:36
Show Gist options
  • Save Ravenslofty/74ab917cc23d3b482b287b95b3552446 to your computer and use it in GitHub Desktop.
Save Ravenslofty/74ab917cc23d3b482b287b95b3552446 to your computer and use it in GitHub Desktop.
/* This is what I do at the moment for a knight. */
void GenKnightBitlist(struct Board * b, int tmp, int from)
{
int i, j, col;
i = from;
col = FILE (i);
j = i - 6;
if (j >= 0 && col < 6) {
b->bitlist[j] ^= tmp;
}
j = i - 10;
if (j >= 0 && col > 1) {
b->bitlist[j] ^= tmp;
}
j = i - 15;
if (j >= 0 && col < 7) {
b->bitlist[j] ^= tmp;
}
j = i - 17;
if (j >= 0 && col > 0) {
b->bitlist[j] ^= tmp;
}
j = i + 6;
if (j < 64 && col > 1) {
b->bitlist[j] ^= tmp;
}
j = i + 10;
if (j < 64 && col < 6) {
b->bitlist[j] ^= tmp;
}
j = i + 15;
if (j < 64 && col > 0) {
b->bitlist[j] ^= tmp;
}
j = i + 17;
if (j < 64 && col < 7) {
b->bitlist[j] ^= tmp;
}
}
/* This is what you're suggesting. */
char KnightArray[64][32][8]; /* 16KB */
void InitKnightArray()
{
int i, j, col, tmp;
for (i = 0; i < 64; i++) {
for (tmp = 0; tmp < 32; tmp++) {
col = FILE (i);
j = i - 6;
if (j >= 0 && col < 6) {
KnightArray[i][tmp][0] = j;
} else {
KnightArray[i][tmp][0] = INVALID;
}
/* Ad nauseam */
}
}
}
void GenKnightBitlist(struct Board * b, int tmp, int from)
{
int dest;
for (int i = 0; i < 8; i++) {
if ((dest = KnightArray[from][tmp][i]) != INVALID) {
b->bitlist[dest] ^= 1 << tmp;
}
}
}
/* Which will likely not be faster. */
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment