Connect 4 Game - Calculating moves

DannyRyan

Member
Licensed User
Longtime User
Hi, Does anyone know how to change the following code to work in B4A?
I decided an 'easy' game to build would be a working human v CPU connect 4 game... which as I am finding out - is harder than I expected.
thanks

B4X:
The source code from the Fhourstones Benchmark from John Tromp uses a fascinating algorithm for testing a connect four game for a win. The algorithm uses following bitboard representation of the game:

.  .  .  .  .  .  .  TOP
5 12 19 26 33 40 47
4 11 18 25 32 39 46
3 10 17 24 31 38 45
2  9 16 23 30 37 44
1  8 15 22 29 36 43
0  7 14 21 28 35 42  BOTTOM
There is one bitboard for the red player and one for the yellow player. 0 represents a empty cell, 1 represents a filled cell. The bitboard is stored in an unsigned 64 bit integer variable. The bits 6, 13, 20, 27, 34, 41, >= 48 have to be 0.

The algorithm is:

// return whether 'board' includes a win
bool haswon(unsigned __int64 board)
{
    unsigned __int64 y = board & (board >> 6);
    if (y & (y >> 2 * 6))     // check \ diagonal
        return true;
    y = board & (board >> 7);
    if (y & (y >> 2 * 7))     // check horizontal
        return true;
    y = board & (board >> 8);
    if (y & (y >> 2 * 8))     // check / diagonal
        return true;
    y = board & (board >> 1);
    if (y & (y >> 2))         // check vertical
        return true;
    return false;
}
 

DannyRyan

Member
Licensed User
Longtime User
Where did the solution go?

Hi visited this post yesterday briefly to see if there was any responses. To which there was. 1 post from a kind person who rewrite the language above to B4A, also mentioned it took a few hours to put together a connect 4 game based on the code.. but i visit this page today and the reply is no longer here.. what happened?
thanks
danny:sign0148:

Found this in search also, when searching for "Connect 4"
Connect 4 Game - Calculating moves - DannyRyan (go to first post)
to build would be a working human v CPU connect 4 game... which as I am finding out - is harder than I... return true; return false; } That's a cool algorithm. With it I was able to get a connect 4... algorithm for testing a connect four game for a win. The algorithm uses following bitboard representation of the game: . . . . . . . TOP 5 12 19 26 33 40 47 4 11 18 25 32 39 46 3 10 17 24 31 38..., board / 2) If And64Bits(y, y / 4) <> 0 Then Return True For j = 6 To 8 y = And64Bits(board
 
Last edited:
Upvote 0

MLDev

Active Member
Licensed User
Longtime User
The code I posted didn't work correctly. That algorithm requires a 64 bit AND function. The function I posted didn't work for some very large numbers. I think I've fixed it.

B4X:
Sub haswon(board As Long) As Boolean
   Dim y As Long = And64Bits(board, board / 2)
   If And64Bits(y, y / 4) <> 0 Then Return True

   For j = 6 To 8
      y = And64Bits(board, board / Power(2, j))
      If And64Bits(y, y / Power(2, 2 * j)) <> 0 Then Return True
   Next
   
   Return False
End Sub

Sub And64Bits(a As Long, b As Long) As Long
   Dim c As Long = Bit.And(Bit.And(a / Power(2, 48), 65535), Bit.And(b / Power(2, 48), 65535))
   
   For j = 1 To 3
      a = a * 65536
      b = b * 65536
      c = c * 65536 + Bit.And(Bit.And(a / Power(2, 48), 65535), Bit.And(b / Power(2, 48), 65535))
   Next

   Return c
End Sub
 
Upvote 0

DannyRyan

Member
Licensed User
Longtime User
:wav:
Thanks MLDev, I do indeed owe you a pint!

Now, I have just got to figure out this bitboard - and how i interact with the bitboard.

Would i be correct in thinking i call the And64Bits sub and pass 2 bitboards as long. 1 bitboard for each player(human & computer).

thanks
 
Upvote 0
Top