I am working with a game state which is received as a string and needs to be converted into a BitBoard. I believe I have a function that accomplishes that now, but want to know how I can optimize it for faster execution? I originally started with a loop with the following idea:
for (i = 0; i < 23; ++i)
{
if (s.at(n) == 'x') set bit[2], // Start with x as there are more x's
else if(s.at(n) == 'W') set bit[0], // W next as W goes first
else set bit[1] // B last
}
but figured that I could just unroll the loop and skip the comparison and incrementation of 'i'. After doing that, I figured I could remove that last check for 'B', and just take the compliment of W | x and subtract 4286578688 from it to give me just 23 bits. That gave me the following code:
std::string board = "xBWxWxBWxWxxBxxxWxBxxBx"; // String to convert to bitboard
unsigned int bit; // Integer used for parsing values
unsigned int boards[3] {0, 0, 0}; // W in [0], B in [1], & x in [2]
if (board.at(0) == 'x') { boards[2] |= (1 << 22); } else if (board.at(0) == 'W') { boards[0] |= (1 << 22); }
⋅
⋅
⋅
if (board.at(22) == 'x') { boards[2] |= (1 << 0); } else if (board.at(22) == 'W') { boards[0] |= (1 << 0); }
boards[1] = ~(boards[0] | boards[2]) - 4286578688; // Take W's & x's compliment - 4286578688 to get 2163730
printf("%d | %d | %d\n",boards[0], boards[1], boards[2]); // Expected Output: "1351744 | 2163730 | 4873133"
Are there any other tricks to further optimize this process for speed? I'm not as concerned with file size.
Lastly, how would I go about converting the boards[W, B, x] back to a string? (E.g. Player 'W' added a piece to position 22, resulting in boards[] = {1351745, 2163730, 4873132}
. How to convert that to: board = xBWxWxBWxWxxBxxxWxBxxBW
?)
Edit: I got the function to revert back to the board with the following:
char state[23];
for (int i = 0, j = 22; i < 23; ++i, --j) {
if (boards[2] & (1 << j)) { state[i] = 'x'; } else if (boards[0] & (1 << j)) { state[i] = 'W'; } else { state[i] = 'B'; }
}