I am working on a gomoku engine. I am using 15 16-bit words,one for each row (15x15 board). The board looks like this if it was an array: (starting from the bottom and up,right to left)
...29 28 27 26 25 24 23 22 21 20 19 18 17 16 15
14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
etc.
In Gomoku, A win must be 5 consecutive marks and not more, more than 5 is called an "overline" and is not considered as a win.
So I am trying to find an algorithm that check if there is a win.
The best idea i found so far is doing a bit masking on each 7 (or 6 on the edges) bit-sequence with this bit pattern that represents a win: 0111110
this should be done for the whole row,columns and diagonals that relevant to a certain move (I always have a reference to the last move that was played).
But The problem with this approach, is that for the columns and the diagonals, I need to "construct" a temporary Bit-set that is similar to how a row looks like but only with the bits of the column /diagonal instead. something like:
And after I did that, i can start do the win pattern recognizing to see if that cartain column contains a win:Code:short y = 0; int col = lastMove%15; short mask = 1; for(int i = 0; i < 15; i++){ y |= (board[color][i] >> col & mask) << i; }
Any one got a better idea? cause mine is really slow and clumsyCode:short overline = 127; short win = 62; for(int i = 0; i <= 9; i++){ y >>= i; if((y & overline) == win){ return color+1; } }