I am using 0x88 in C# to represent my board. I chose a 16x8 byte array. I am able to iterate through the array, row by row, and assign 0-127 to each square. Then, using a bitwise AND operation I am able to find which squares are on the board and which that are not.
In my next step, I want to setup the starting position with the pieces, but I am unsure how to proceed.
Where and how do I store the piece info for each square of this Byte array? I have a Pieces struct:
private void SetupPieces(byte[,] newBoard)
{
Pieces pieces = new Pieces();
pieces.emptySquare = 0;
pieces.whitePawn = 1;
pieces.blackPawn = 2;
pieces.whiteKnight = 3;
pieces.blackKnight = 4;
pieces.whiteBishop = 5;
pieces.blackBishop = 6;
pieces.whiteRook = 7;
pieces.blackRook = 8;
pieces.whiteQueen = 9;
pieces.blackQueen = 10;
pieces.whiteKing = 11;
pieces.blackKing = 12;
}
0x88 board representation : basic question from beginner
Moderators: hgm, Rebel, chrisw
Re: 0x88 board representation : basic question from beginner
See the other post (http://www.talkchess.com/forum/viewtopic.php?t=24441) about the enums. You are not supposed to initialize constants.
Then if you want to initialize your board then you should have something like :
board[A1] = whiteRook;
board[A2] = whitePawn;
etc.
But you will need something to initialize your board from a FEN string so you can try something else than just the start position.
For efficiency you should also use a piece list (http://www.talkchess.com/forum/viewtopi ... =&start=13)
HJ.
Then if you want to initialize your board then you should have something like :
board[A1] = whiteRook;
board[A2] = whitePawn;
etc.
But you will need something to initialize your board from a FEN string so you can try something else than just the start position.
For efficiency you should also use a piece list (http://www.talkchess.com/forum/viewtopi ... =&start=13)
HJ.
-
- Posts: 670
- Joined: Mon Dec 03, 2007 3:01 pm
- Location: Barcelona, Spain
Re: 0x88 board representation : basic question from beginner
Firstly, it makes sense to enumerate your pieces so that you can easily access the color of the piece.
Eg:
whitePawn = 0
blackPawn = whitePawn + 8
whiteKnight = 1
blackKnight = whiteKnight + 8
...
then if you want the color of a piece you just write
board[Square] & 8
Secondly, engines should be able to read fen-strings
eg:rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1
so you have to build some algorithm to loop through the string.
here is an example:
http://chessprogramming.wikispaces.com/ ... %280x88%29
see under "int board_loadFromFen(char * fen)"
regards
Edmund
Eg:
whitePawn = 0
blackPawn = whitePawn + 8
whiteKnight = 1
blackKnight = whiteKnight + 8
...
then if you want the color of a piece you just write
board[Square] & 8
Secondly, engines should be able to read fen-strings
eg:rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1
so you have to build some algorithm to loop through the string.
here is an example:
http://chessprogramming.wikispaces.com/ ... %280x88%29
see under "int board_loadFromFen(char * fen)"
regards
Edmund
-
- Posts: 1922
- Joined: Thu Mar 09, 2006 12:51 am
- Location: Earth
Re: 0x88 board representation : basic question from beginner
If you're using 0x88, you use a one dimensional array, not a two dimensional array (byte[] board = new byte[128] As an aside, you should also have some sort of enum of squares (A1=0, B1=1, C1=2, etc.). I think the best way for initializing the board is by creating a FEN reader as mentioned, and then just storing the fen for the start position.
Re: 0x88 board representation : basic question from beginner
Thank you Harald, Edmund and Zach! I'm working on your suggestions now. I appreciate it!
-
- Posts: 1922
- Joined: Thu Mar 09, 2006 12:51 am
- Location: Earth
Re: 0x88 board representation : basic question from beginner
Oops, I didn't look at my post after I actually posted it. That should be (byte[] board = new byte[128]; ) as you could probably tell.
Also, as for Edmund's suggestion about the piece enumeration, I think his original numbering was just as good (you can get the color with piece & 1). Depending on the engine either one might be better. I'd prefer his actually, so that the colors are in the range 0-1, and if you do lookups based on color (which I do a lot) you don't need an additional shift (or a 9-element array).
Also, as for Edmund's suggestion about the piece enumeration, I think his original numbering was just as good (you can get the color with piece & 1). Depending on the engine either one might be better. I'd prefer his actually, so that the colors are in the range 0-1, and if you do lookups based on color (which I do a lot) you don't need an additional shift (or a 9-element array).
-
- Posts: 147
- Joined: Wed Jun 06, 2007 10:01 am
- Location: United States
- Full name: Mike Leany
Re: 0x88 board representation : basic question from beginner
His implementation would work. You just change the 8 above to a 1, which gives you:Codeman wrote:Firstly, it makes sense to enumerate your pieces so that you can easily access the color of the piece.
Eg:
whitePawn = 0
blackPawn = whitePawn + 8
whiteKnight = 1
blackKnight = whiteKnight + 8
...
then if you want the color of a piece you just write
board[Square] & 8
board[Square] & 1
But I haven't played with mailbox board representations myself, so I don't know if there are any other drawbacks.
Secondly, engines should be able to read fen-strings
eg:rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1
so you have to build some algorithm to loop through the string.
here is an example:
http://chessprogramming.wikispaces.com/ ... %280x88%29
see under "int board_loadFromFen(char * fen)"
regards
Edmund
-
- Posts: 27851
- Joined: Fri Mar 10, 2006 10:06 am
- Location: Amsterdam
- Full name: H G Muller
Re: 0x88 board representation : basic question from beginner
It is often more convenient to use one bit to indicate black pieces, and another bit to indicate white pieces (rather than just relying on the first bit to be zero). E.g. if white = 8 and black = 16, you can do
whiteKnight = knight + 8;
blackKnight = knight + 16;
and then test for pieces of a certain color by
if(piece & color) ...
and get the piece type by
pieceType = piece & 7;
If empty squares have neither bit set, thi owuld automatically skip empty squares. In addition it would be also possible to make pieces that test true for both colors, which you could use as edge guards around the board. (So that whoever has the move, squares outside the board are recognized as own pieces, so you cannot capture them.) Micro-Max uses this representation.
This whole system can still be used if you store piece numbers on the board, in stead of piece types. You then don't even have to trip off the color bit to get the piece number; you can simply assign the numbers such that pieces of white have one bit set (e.g. take them in the range 16-31 to have the 16 bit set) and black pieces another (32-47 for the 32 bit). I use this representation in qperft. (Except that I use the 32 bit for white and the 64 bit for black there, to allow me to recognize Pawns by the 16 bit, and still have enough numbers left with the 16 bit cleared, for indicating promotion pieces.)
whiteKnight = knight + 8;
blackKnight = knight + 16;
and then test for pieces of a certain color by
if(piece & color) ...
and get the piece type by
pieceType = piece & 7;
If empty squares have neither bit set, thi owuld automatically skip empty squares. In addition it would be also possible to make pieces that test true for both colors, which you could use as edge guards around the board. (So that whoever has the move, squares outside the board are recognized as own pieces, so you cannot capture them.) Micro-Max uses this representation.
This whole system can still be used if you store piece numbers on the board, in stead of piece types. You then don't even have to trip off the color bit to get the piece number; you can simply assign the numbers such that pieces of white have one bit set (e.g. take them in the range 16-31 to have the 16 bit set) and black pieces another (32-47 for the 32 bit). I use this representation in qperft. (Except that I use the 32 bit for white and the 64 bit for black there, to allow me to recognize Pawns by the 16 bit, and still have enough numbers left with the 16 bit cleared, for indicating promotion pieces.)
-
- Posts: 4675
- Joined: Mon Mar 13, 2006 7:43 pm
Enumeration types
I strongly suggest that coders, particularly beginner coders, use enumeration types with the default sequential integer value assignments. This makes indexing fast and simple as there are no holes in the index ranges. Also, a decent debugger can output the enumeration symbols instead of integer literals. (Maybe do input as well.) Furthermore, any boolean property of such an enumeration value can be determined via a quick shift and mask:
or the possibly faster alternative:
Never forget the priority list:
1) Validity first
2) Efficiency second
In any case, property value and scalar value extraction and insertion should be compartmentalized into inline functions. These are easier to validate and are also easier to change if needed.
Code: Select all
inline bool IsSomeProperty(ManEnumType chessman)
{
return SomePropertyBitMaskConstant & (1 << chessman);
}
Code: Select all
inline bool IsSomeProperty(ManEnumType chessman)
{
return (SomePropertyBitMaskConstant >> chessman) & 1;
}
1) Validity first
2) Efficiency second
In any case, property value and scalar value extraction and insertion should be compartmentalized into inline functions. These are easier to validate and are also easier to change if needed.