• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
ashish123

Move Generation using Bit boards (Connect-4)

13 posts in this topic

Hi all,
I am facing some speed(performance) issues while generating moves for connect 4.

Perviously I wrote simple nested for-loops to generate the moves now I tried to convert it into bit boards so
I found all the empty squares and anded it with column bits. (eg column1=(1L<<1|1L<<10...)
This gave me the empty bits which are in a particular column.
Now I found the MSB by right-shifting this till the number was 0( trick to find MSB when its power of 2).
it gave me correct answer, but then surprisingly this was slower as compared to nested for (nested for loops took 238 ms where as bitboards took 1349 ms).

So then I tried another method, the folding trick as mentioned here.
[source lang="csharp"] x |= (x >> 1);
x |= (x >> 2);
x |= (x >> 4);
x |= (x >> 8);
x |= (x >> 16);
x |= (x >> 32);

for (int n = 53; n >=0; n--)
if (((1L << n) & (x & ~(x >> 1))) != 0)
return n;[/source]
This too gave me slow results.
What am I doing wrong as I am sure bitboards will be certainly faster then nested loops.
How can I achieve this without nested loops, something like debrujin sequence for MSB (64 bit number).

-Thank you.
0

Share this post


Link to post
Share on other sites
I would use consecutive bits to represent columns. Following the same convention as [url="http://homepages.cwi.nl/~tromp/c4/fhour.html"]Fhourstones[/url]:

[code]. . . . . . .
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
[/code]

You can then generate moves as
[code]u64 generate_moves() {
u64 occupied = pieces[0] | pieces[1];
return BOARD_MASK & (occupied >> 1) & ~occupied;
}[/code]

When you need to loop over the moves, you do something like this:
[code] for (u64 moves = generate_moves(); moves; moves &= moves-1) {
u64 move = moves & -moves;
// `move' now has a bitboard with a single 1 in the position where you can move.
// You can use the De Bruijn sequence trick if you want to convert it to an index.
}[/code] Edited by alvaro
0

Share this post


Link to post
Share on other sites
Fhourstones representation is nice, it uses lesser number of bits as compared to mine (with borders).
However I will choose this type of representation in second version (to compare with my own implementation)

I found the bug that was causing delay, it considered a move on index 0, which lies on the border. (so approximately 7 times more ouch!)

I used Nalimov representation from chess-programming wiki.
Generally what I do is, take up a 2d-array, store all moves in form of moves[depth,move] and then access according to depth. I also keep another array which helps me to count number of moves for particular depth, which is used to traverse.

The array representation helped me to sort the moves based on killer moves heuristics. But I also noticed that using arrays for storing moves seems to be slow (I may be wrong on this one, kindly correct if I am.) But I am unable to see a way to sort killer moves first with using only bit boards.

Here is what I did with my genMoves method.
[source lang="csharp"]public static void genMoves()
{
long empty = ((~(xBits | yBits)) & bitBoard);

int moveIndex = 0;

moveIndex = findIndex((ulong)(empty & column1));

if (moveIndex != 0)
moves[depth, nPly[depth]++] = moveIndex;

moveIndex = findIndex((ulong)(empty & column2));
if (moveIndex != 0)
moves[depth, nPly[depth]++] = moveIndex;

moveIndex = findIndex((ulong)(empty & column3));
if (moveIndex != 0)
moves[depth, nPly[depth]++] = moveIndex;

moveIndex = findIndex((ulong)(empty & column4));

if (moveIndex != 0)
moves[depth, nPly[depth]++] = moveIndex;

moveIndex = findIndex((ulong)(empty & column5));

if (moveIndex != 0)
moves[depth, nPly[depth]++] = moveIndex;

moveIndex = findIndex((ulong)(empty & column6));

if (moveIndex != 0)
moves[depth, nPly[depth]++] = moveIndex;

moveIndex = findIndex((ulong)(empty & column7));

if (moveIndex != 0)
moves[depth, nPly[depth]++] = moveIndex;


}

public static int findIndex(ulong bb)
{
int result = 0;
if (bb > 0xFFFFFFFF)
{
bb >>= 32;
result = 32;
}
if (bb > 0xFFFF)
{
bb >>= 16;
result += 16;
}
if (bb > 0xFF)
{
bb >>= 8;
result += 8;
}
return result + ms1bTable[(int)bb];
[/source]
Theres negligible improvement of jus one second over the for loops.
With arrays its simpler to order the moves, but with bits its quicker.
Can you show me a way to order the moves using bit approach. Edited by ashish123
0

Share this post


Link to post
Share on other sites
I can't write code for you following your square-to-bit convention, because I don't know what it is. However, my code shows you how you don't have to consider each column individually to find all the valid moves: Just compute `empties & shift_north(occupied)'. Then extract all the bits that are set, using a loop like the one I showed you.

I don't know of any way to sort moves other than putting them in an array first, but that shouldn't be slow at all.
0

Share this post


Link to post
Share on other sites
[quote name='alvaro' timestamp='1341054536' post='4954278']
Just compute `empties & shift_north(occupied)'. Then extract all the bits that are set, using a loop like the one I showed you.
[/quote]

On an empty board, occupied will be 0, so according to the pseudo-code, only available move is 0.
am I missing something? I adopted the Fhourstones structure for a while.
0

Share this post


Link to post
Share on other sites
[quote name='ashish123' timestamp='1341161955' post='4954572']
[quote name='alvaro' timestamp='1341054536' post='4954278']
Just compute `empties & shift_north(occupied)'. Then extract all the bits that are set, using a loop like the one I showed you.
[/quote]

On an empty board, occupied will be 0, so according to the pseudo-code, only available move is 0.
am I missing something? I adopted the Fhourstones structure for a while.
[/quote]

Ooops! You are right. It's easily fixed, though: empties & (shift_north(occupied) | FIRST_ROW)
0

Share this post


Link to post
Share on other sites
Hi again,
I was occupied with few things so had to keep this coding away.
@alvaro: Apology for late reply, but your trick did its job and its working nicely.

Thinking about this further, I think I can reduce the number of moves when there is winning threat present.
say I have three in a row and computer has 6 different moves, its not practical to search every single move as next I am going to play on that.
Firstly can you please classify if this approach is correct. I have added some piece of code in my make move method which will help me do that.
its buggy currently(of which I do not concentrate as of now,concentrating on concept), but thing is I am focussing on discarding of search nodes as much as possible before adding more knowledge to the eval as it would slow down.
Currently its getting about 400knps.
Please help me with this.
[CODE]
long occupied = (xBits | yBits);
long empty = ~occupied;
long bitMoves = 0L;
bitMoves = bitBoard & empty & ((occupied >> 9) | lastrow);
//Find the forced moves
long xThreats = 0L;
long yThreats = 0L;

yThreats |= ((yBits << 1) & (yBits << 2) & (yBits << 3) & empty & bitBoard);//XXX_
yThreats |= ((yBits >> 2) & (yBits << 1) & (yBits >> 1) & empty & bitBoard);//X_XX
yThreats |= ((yBits << 2) & (yBits << 1) & (yBits >> 1) & empty & bitBoard);//XX_X
yThreats |= ((yBits >> 1) & (yBits >> 2) & (yBits >> 3) & empty & bitBoard);//_XXX

yThreats |= ((yBits << 10) & (yBits << 20) & (yBits << 30) & empty & bitBoard);//XXX_
yThreats |= ((yBits >> 20) & (yBits << 10) & (yBits >> 10) & empty & bitBoard);//X_XX
yThreats |= ((yBits << 20) & (yBits << 10) & (yBits >> 10) & empty & bitBoard);//XX_X
yThreats |= ((yBits >> 10) & (yBits >> 20) & (yBits >> 30) & empty & bitBoard);//_XXX
yThreats |= ((yBits << 8) & (yBits << 16) & (yBits << 24) & empty & bitBoard);//XXX_
yThreats |= ((yBits >> 16) & (yBits << 8) & (yBits >> 8) & empty & bitBoard);//X_XX
yThreats |= ((yBits << 16) & (yBits << 8) & (yBits >> 8) & empty & bitBoard);//XX_X
yThreats |= ((yBits >> 8) & (yBits >> 16) & (yBits >> 24) & empty & bitBoard);//_XXX

xThreats |= ((xBits << 1) & (xBits << 2) & (xBits << 3) & empty & bitBoard);//XXX_
xThreats |= ((xBits >> 2) & (xBits << 1) & (xBits >> 1) & empty & bitBoard);//X_XX
xThreats |= ((xBits << 2) & (xBits << 1) & (xBits >> 1) & empty & bitBoard);//XX_X
xThreats |= ((xBits >> 1) & (xBits >> 2) & (xBits >> 3) & empty & bitBoard);//_XXX
xThreats |= ((xBits << 10) & (xBits << 20) & (xBits << 30) & empty & bitBoard);//XXX_
xThreats |= ((xBits >> 20) & (xBits << 10) & (xBits >> 10) & empty & bitBoard);//X_XX
xThreats |= ((xBits << 20) & (xBits << 10) & (xBits >> 10) & empty & bitBoard);//XX_X
xThreats |= ((xBits >> 10) & (xBits >> 20) & (xBits >> 30) & empty & bitBoard);//XXX_

xThreats |= ((xBits << 8) & (xBits << 16) & (xBits << 24) & empty & bitBoard);//XXX_
xThreats |= ((xBits >> 16) & (xBits << 8) & (xBits >> 8) & empty & bitBoard);//X_XX
xThreats |= ((xBits << 16) & (xBits << 8) & (xBits >> 8) & empty & bitBoard);//XX_X
xThreats |= ((xBits >> 8) & (xBits >> 16) & (xBits >> 24) & empty & bitBoard);//_XXX


if((((yThreats|xThreats)&bitMoves)!=0))
{
bitMoves = bitMoves&(yThreats|xThreats);// play on the threatend empty square only.
}
[/CODE]

my board structure is
00 | 01 02 03 04 05 06 07 | 08
09 | 10 11 12 13 14 15 16 | 17
18 | 19 20 21 22 23 24 25 | 26
27 | 28 29 30 31 32 33 34 | 35
36 | 37 38 39 40 41 42 43 | 44
45 | 46 47 48 49 50 51 52 | 53
where | is the border. Edited by ashish123
0

Share this post


Link to post
Share on other sites
You don't need two columns for padding, but that doesn't really matter.

I spent a lot of time in the early 90s writing a connect 4 program. What I did at the time was making the move generator smart enough to only allow you to win if a win is present, and only allows you to block an opponent's threat if one is present. My friends and I were playing a lot of connect 4 at the time, and we actually used rules similar to chess, where it is considered illegal to expose your king. So (during the search) my move generator also didn't let a player play right under an opponent's threat, because that results in immediate victory for the opponent.

Oh, I also extended the depth for forced moves (where only one move is legal, with the definition above). This makes the program stronger in tactics, but nowadays I would prefer to test this potential improvement more scientifically, actually playing thousands of games to see if it really helps.

A matter of style:
[code] long bitMoves = 0L;
bitMoves = bitBoard & empty & ((occupied >> 9) | lastrow);[/code]

Why is the code above not simply this?
[code] long bitMoves = bitBoard & empty & ((occupied >> 9) | lastrow);[/code]

Actually, I think my compiler (gcc) would complain about initializing a variable to a value that is never used.
0

Share this post


Link to post
Share on other sites
@alvaro, Thanks for the reply,
From your reply, I took that I was on right track.
Now for a trial position after 8 plies, my program solves the game in about 5 mins.
Later on I shall implement iterative deepening and would post the updated progress.
0

Share this post


Link to post
Share on other sites
Hi again,
I implemented the iterative deepening, with History heuristics, but then some how I find that the time taken for Iterative deepening is far too more than normal
I think this happened because of high search depth (20+) as there were lot of sorting for each move. I also read few papers which said that performance History heuristics decreases with higher depths where depth is above 7 or 8.
0

Share this post


Link to post
Share on other sites
Ok, I implemented History heuristics with iterative deepening and it further delayed but quiet a measurable extent.
I think that the major cause of this is due to poor evaluation function (Search for WIN-LOSS only).
Can anyone guide me here, how to speed up, I want to keep knowledge as low as possible(I know this is bad, but I want to prune the tree on a higher extent by forcing moves).
Is there any extension of algorithm which can speed up.

I have implemented killer move heuristics(works great), with hashtables.
I also try the move from hashtables first, but then too the performance doesnt really speed up.

Can anyone shine some light on my observation and query.

Thanks.
0

Share this post


Link to post
Share on other sites
Writing a strong program that only uses win-draw-loss is hard. Consider the following situation:
[code]_ _ _ _ _ _ _
_ _ _ _ _ _ _
_ _ _ _ _ _ _
_ _ X 1 Y Z _
_ _ 1 2 1 _ _
_ _ 2 1 2 _ _[/code]
It is 2's turn to move, and if it plays at X, player 1 can play at Y, which leads to a straight-forward victory (fill up the rightmost column, stay out of trouble elsewhere and win at Z). Unless you incorporate some knowledge in your program, it will have to search to depth 30 or so before it realizes it. Because I have some knowledge of the game, I can see it with essentially no search.

John Tromp wrote a connect-4 program that can search the whole tree from that position in at most a few seconds. His program Fhourstones searches the starting position in about 5 minutes on my desktop machine. He even has a Java applet in [url="http://homepages.cwi.nl/~tromp/c4/c4.html"]this page[/url] that plays perfectly (using a database for positions after 8 moves). So what you are trying to do is certainly possible, but you need to be a very good programmer to do it.

John insists that some variant of history heuristic is what allows his programs to be so fast. The code to Fhourstones is available, so perhaps you can just read it to find what the exact details, but his description was basically having one byte per square on the board, which are used to sort the moves (descending). They are initially all 0, and if a beta-cut happens after exploring the third child of a node, the first two children get their scores reduced by one and the score of the third child is increased by 2 (so the sum of all the scores is always 0). The score is capped to avoid overflow (and to stay nimble and adapt to changing circumstances when exploring different parts of the tree). I imagine he keeps a separate table for each player. Edited by alvaro
0

Share this post


Link to post
Share on other sites
@alvaro: Thanks for the reply, and a good example which used zugswang to force up a win. Is there a method by which I can find which player controls the zugswang.
I tried to read Victor Allis thesis and the rules which are described in them can be summed up in bitboard fashion pretty decently.
However I fail to understand how to determine which player holds the zugswang.

I also read the explanation given by John Tromp, he has used some heavy stuff.
Will try to implement the logic.

Another question off the topic, how can I be a better programmer? I am coding from past 2 years and now I think I am not growing.
How can I be better. Are there any assignments? Some way of logic building or some classes/books on algorithms I should read? Or is this a phase in every programmers life?

Thanks.
0

Share this post


Link to post
Share on other sites
[url="http://www.amazon.com/The-Complete-Book-CONNECT-Strategy/dp/1402756216"]This book on Connect 4[/url] is a great place to learn about strategy for this game, which is primarily about what you call "holding the zugswang".

It's not a short thing to explain, although the very basic idea is that, if you imagine the whole board fills up except the square below the threat and above, you can determine whose turn it will be at that point by looking at the parity of the number of empty squares left. So, if we label top row as even, player 1 wins with a threat on an odd row and player 2 wins with a threat on an even row, if there is nothing else going on on the board. Player 2 can win with 2 odd threats. Now an interesting observation is that an odd threat of player 1 beats any number of even threats of player 2. The exact classification of columns and how they combine is covered in the book.
0

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0