Board without color flags

Discussion of chess software programming and technical issues.

Moderators: hgm, Harvey Williamson, bob

Forum rules
This textbox is used to restore diagrams posted with the [d] tag before the upgrade.
Post Reply
LoopList

Board without color flags

Post by LoopList » Sat Oct 31, 2009 3:35 pm

Hi!

Dose anyone has experiences with a chessboard without color flags?

It should't be a problem while generating moves, doing/undoing moves and evaluating withing the scope of a bitboard engine.

Many parts of the chess engine will get easier and the distinction between white and black is redundant.

?

Gerd Isenberg
Posts: 2113
Joined: Wed Mar 08, 2006 7:47 pm
Location: Hattingen, Germany

Re: Board without color flags

Post by Gerd Isenberg » Sat Oct 31, 2009 3:45 pm

LoopList wrote:Hi!

Dose anyone has experiences with a chessboard without color flags?

It should't be a problem while generating moves, doing/undoing moves and evaluating withing the scope of a bitboard engine.

Many parts of the chess engine will get easier and the distinction between white and black is redundant.

?
I actually color-flip the board vertically after each move and have white to move always. Works fine. One quad-bb,
hashkey[piece][color][sq] == byteswap(hashkey[piece][other(color)][sq^56]).

LoopList

Re: Board without color flags

Post by LoopList » Sat Oct 31, 2009 6:23 pm

What are the drawbacks of writing chessboard-related code for White and Black?

Pros:
Special code is easy to develop and to debug.
Special code is probably faster?!

Cons:
Redundant codes.
Changes and improvements have to be done twice.
Unintended assymetries are more likely.

The question arises, why many engine developers are using one general code for White and Black und no special code?

Michael Sherwin
Posts: 2975
Joined: Fri May 26, 2006 1:00 am
Location: WY, USA
Full name: Michael Sherwin

Re: Board without color flags

Post by Michael Sherwin » Mon Nov 02, 2009 12:43 am

LoopList wrote:What are the drawbacks of writing chessboard-related code for White and Black?

Pros:
Special code is easy to develop and to debug.
Special code is probably faster?!

Cons:
Redundant codes.
Changes and improvements have to be done twice.
Unintended assymetries are more likely.

The question arises, why many engine developers are using one general code for White and Black und no special code?
I do not know if I am qualified to answer this one. However, Gerd's approach interest me for one reason--moves would be generated identically no matter which color was on move from the moving side. This would allow a greater degree consistency of search and evaluation. Others are looking for evaluation symmetry, cash friendliness, or just clean abstraction of code.
I hate if statements. Pawns demand if statements. Therefore I hate pawns.

Gerd Isenberg
Posts: 2113
Joined: Wed Mar 08, 2006 7:47 pm
Location: Hattingen, Germany

Re: Board without color flags

Post by Gerd Isenberg » Mon Nov 02, 2009 9:48 pm

LoopList wrote:What are the drawbacks of writing chessboard-related code for White and Black?

Pros:
Special code is easy to develop and to debug.
Special code is probably faster?!

Cons:
Redundant codes.
Changes and improvements have to be done twice.
Unintended assymetries are more likely.

The question arises, why many engine developers are using one general code for White and Black und no special code?
One way to avoid duplicate, harder to maintain and more error prone color dependent code without a loss of its speed is to use color/side as compile time parameter, either as inlined C++ templates or as const parameters of inline functions.

bob
Posts: 20358
Joined: Mon Feb 27, 2006 6:30 pm
Location: Birmingham, AL

Re: Board without color flags

Post by bob » Mon Nov 02, 2009 10:09 pm

LoopList wrote:What are the drawbacks of writing chessboard-related code for White and Black?

Pros:
Special code is easy to develop and to debug.
Special code is probably faster?!

Cons:
Redundant codes.
Changes and improvements have to be done twice.
Unintended assymetries are more likely.

The question arises, why many engine developers are using one general code for White and Black und no special code?
Several reasons, but the most serious is that duplicated code is much harder to maintain. I can't count the number of times I found asymmetries in something caused by a difference in the way black and white were handled. I added special debug code in Crafty specifically to help catch this in the evaluation, since each piece type and color had specific code. Not the way to develop bug-free code. Current crafty has none of this stuff. It is much smaller, easier to maintain and easier to modify since a change only has to be done once.

LoopList

Re: Board without color flags

Post by LoopList » Tue Nov 03, 2009 7:03 am

Gerd Isenberg wrote:
LoopList wrote:What are the drawbacks of writing chessboard-related code for White and Black?

Pros:
Special code is easy to develop and to debug.
Special code is probably faster?!

Cons:
Redundant codes.
Changes and improvements have to be done twice.
Unintended assymetries are more likely.

The question arises, why many engine developers are using one general code for White and Black und no special code?
One way to avoid duplicate, harder to maintain and more error prone color dependent code without a loss of its speed is to use color/side as compile time parameter, either as inlined C++ templates or as const parameters of inline functions.
I don't really understand how to color-flip the board vertically after each move in an efficient way? Could you give me an example? And how many bitboards do you have? Do you need a board-vector, such as

Code: Select all

int board[64]
?

I am currently using the following code for board representation:

Code: Select all

  int king_sq[COLORS];

  int piece_ct[COLORS];
  int pawn_ct[COLORS];
  int knight_ct[COLORS];
  int bishop_ct[COLORS];
  int rook_ct[COLORS];
  int queen_ct[COLORS];

  bitboard_t piece_bb[COLORS];
  bitboard_t pawn_bb[COLORS];
  bitboard_t knight_bb[COLORS];
  bitboard_t bishop_bb[COLORS];
  bitboard_t rook_bb[COLORS];
  bitboard_t queen_bb[COLORS];
Thanks
Fritz

Gerd Isenberg
Posts: 2113
Joined: Wed Mar 08, 2006 7:47 pm
Location: Hattingen, Germany

Re: Board without color flags

Post by Gerd Isenberg » Tue Nov 03, 2009 7:20 am

LoopList wrote: I don't really understand how to color-flip the board vertically after each move in an efficient way? Could you give me an example? And how many bitboards do you have? Do you need a board-vector, such as

Code: Select all

int board[64]
?
Nope. I use a vector of four bitboards (quad-bb) as one and only board representation where a "vertical" nibble contains the piece code. One of those four bitboards represents the color of the pieces, the union of the three others the occupancy. To color-flip this board, four bswaps are needed, plus ones' complement and intersection with the occupancy of the color board. Incremental hashkey of the position is byte swapped as well.

Post Reply