There are only 16 pieces maximum on each team so you can number the pieces 0-15 and fit that in 4 bits.
Code: Select all
Piece #0 Pawn Piece #8 King's Castle
Piece #1 Pawn Piece #9 King's Knight
Piece #2 Pawn Piece #10 King's Bishop
Piece #3 Pawn Piece #11 King
Piece #4 Pawn Piece #12 Queen
Piece #5 Pawn Piece #13 Queen's Bishop
Piece #6 Pawn Piece #14 Queen's Knight
Piece #7 Pawn Piece #15 Queen's Castle
You don't need to determine Black or White since odd moves are always White and even moves are always Black, you just need to count the moves so far to resolve which colour moves next.
From any given position
Pawn has only 5 possible moves only 3 bits needs
Castle has only 14 possible moves only 4 bits needs
Bishop has only 14 possible moves only 4 bits needs
Knight has only 8 possible moves only 3 bits needs
King has only 8 possible moves only 3 bits needs
Queen has only 28 possible moves only 5 bits needs
So all moves except the Queen's can be fully specified in one byte with room to spare.
To fit the Queen's move in you can include 4 bits worth (16) of moves in the byte and use the spare space from the 3-bit pieces (Pawns, Knights and King to fill in for the remaining 12 moves.
You need to choose which move number corresponds with which movement of the piece.
e.g. for Pawn
Move 0 move forward 1 place
Move 1 move forward 2 places
Move 2 Take En Passant
Move 3 Take to front left
Move 4 Take to front right
Similarly a Castle could be specified as:
Move 0 move forward 1 square
Move 1 move forward 2 squares
..
Move 6 move forward 7 squares (wrap around if it's beyond the board)
Move 7 CASTLE-QUEEN
Move 8 move right 1 square
Move 9 move right 2 squares
..
Move 14 move right 7 squares (wrap around if it's beyond the board)
Move 15 CASTLE-KING
e.gs:
Hex
Byte Move
03 Pawn #0 does move 3
11 Pawn #1 does move 1
84 Castle 1 ( piece 8 ) does move 4.
etc.
For the queen you can use a special case like this
C9 Queen (piece #12 = C in hex) makes move 9
09 Pawn 0 makes move 9
07 Pawn 0 makes move 7
0F Pawn 0 makes move 15 ( = F in hex)
etc.
Now move 9 for a Pawn doesn't exist as a Pawn only has moves 0-4 so this is invalid for a Pawn. Since this situation can never exist, simply interpret an invalid move like move 9 as really being a move for the Queen so the Queen's move 0-14 are coded in the Queens move bits and the Queen's move 15-27 are coded in the invalid Pawn moves.
I'm sure you get the idea.
Paul.