Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

29 simple chessboard format #30

Merged
merged 5 commits into from
Feb 25, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 8 additions & 16 deletions chesslib/include/chessboard.h
Original file line number Diff line number Diff line change
Expand Up @@ -84,34 +84,26 @@
#define FIELD_G8 0x4000000000000000uLL
#define FIELD_H8 0x8000000000000000uLL

/*#define START_FORMATION (Bitboard[]){ FIELD_E1, FIELD_D1, 0x0000000000000081uLL, 0x0000000000000024uLL, 0x0000000000000042uLL, 0x000000000000FF00uLL, FIELD_E8, FIELD_D8, 0x8100000000000000uLL, 0x2400000000000000uLL, 0x4200000000000000uLL, 0x00FF000000000000uLL, START_POSITIONS }
#define BOARD_NULL (Bitboard[]){ 0x0uLL, 0x0uLL, 0x0uLL, 0x0uLL, 0x0uLL, 0x0uLL, 0x0uLL, 0x0uLL, 0x0uLL, 0x0uLL, 0x0uLL, 0x0uLL, 0x0uLL }*/

#define SIDE_OFFSET(color) (((uint8_t)(color)) * 6)
#define PIECE_OFFSET(piece) (((uint8_t)(piece)) - 1)
#define WHITE_MASK(color) (((Bitboard)(((int64_t)(color)) - 1)))
#define BLACK_MASK(color) (~WHITE_MASK((color)))

/* ====================================================
D E F I N E C O N S T A N T S
==================================================== */

/* global array representing an empty ChessBoard */
/*ChessBoard BOARD_NULL = (Bitboard[]){ 0x0uLL, 0x0uLL, 0x0uLL, 0x0uLL, 0x0uLL, 0x0uLL, 0x0uLL, 0x0uLL, 0x0uLL, 0x0uLL, 0x0uLL, 0x0uLL, 0x0uLL };*/
/*ChessBoard START_FORMATION = (Bitboard[]){ FIELD_E1, FIELD_D1, 0x0000000000000081uLL, 0x0000000000000024uLL, 0x0000000000000042uLL, 0x000000000000FF00uLL, FIELD_E8, FIELD_D8, 0x8100000000000000uLL, 0x2400000000000000uLL, 0x4200000000000000uLL, 0x00FF000000000000uLL, START_POSITIONS };*/

/* ====================================================
D E F I N E F U N C T I O N S
==================================================== */

ChessBoard create_board(const Bitboard bitboards[]);
ChessBoard create_board_from_piecesatpos(const ChessPieceAtPos pieces_at_pos[], size_t pieces_count);

Bitboard is_captured_at(ChessBoard board, ChessPosition pos);
ChessPiece get_piece_at(ChessBoard board, ChessPosition pos);
int was_piece_moved(ChessBoard board, ChessPosition pos);
Bitboard is_captured_at(const Bitboard board[], ChessPosition pos);
ChessPiece get_piece_at(const Bitboard board[], ChessPosition pos);
int was_piece_moved(const Bitboard board[], ChessPosition pos);

ChessBoard apply_draw(const Bitboard board[], ChessDraw draw);
void apply_draw_to_bitboards(ChessBoard bitboards, ChessDraw draw);

ChessBoard apply_draw(ChessBoard board, ChessDraw draw);
void apply_draw_to_bitboards(Bitboard* bitboards, ChessDraw draw);
ChessBoard from_simple_board(const ChessPiece simple_board[]);
SimpleChessBoard to_simple_board(const Bitboard board[]);

#endif
4 changes: 3 additions & 1 deletion chesslib/include/chessdraw.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,9 @@
D E F I N E F U N C T I O N S
==================================================== */

ChessDraw create_draw(ChessBoard board, ChessPosition oldPos, ChessPosition newPos, ChessPieceType peasantPromotionType);
ChessDraw create_draw(const Bitboard board[], ChessPosition oldPos, ChessPosition newPos, ChessPieceType peasantPromotionType);
ChessDraw from_compact_draw(const Bitboard board[], CompactChessDraw comp_draw);
CompactChessDraw to_compact_draw(ChessDraw draw);

int get_is_first_move(ChessDraw draw);
ChessDrawType get_draw_type(ChessDraw draw);
Expand Down
2 changes: 0 additions & 2 deletions chesslib/include/chesslibmodule.h
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,4 @@ static PyObject* chesslib_visualize_draw(PyObject* self, PyObject* args);

PyMODINIT_FUNC PyInit_chesslib(void);

/* TODO: add missing makros, constants, method stubs, etc. */

#endif
10 changes: 6 additions & 4 deletions chesslib/include/chesstypes.h
Original file line number Diff line number Diff line change
Expand Up @@ -95,10 +95,12 @@ typedef uint64_t Bitboard;
The boards holding information on chess pieces are ordered by the occurance of the piece in the ChessPieceType enum (King=0, Queens=1, Rooks=2, Bishops=3, Knights=4, Peasants=5).
All boards with indices 0-5 belong to the white side, the next 6 boards with indices 6-11 belong to the black side.
*/
/*typedef struct _CHESS_BITBOARD {
Bitboard bitboards[13];
} ChessBoard;*/

typedef Bitboard * ChessBoard;

/*
* The chess board represented as 64 bytes, each modelling the chess piece standing at the specific position of the board.
* In case there is no piece at the given position, it is assigned to CHESS_PIECE_NULL.
*/
typedef ChessPiece * SimpleChessBoard;

#endif
106 changes: 99 additions & 7 deletions chesslib/src/chessboard.c
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@

ChessBoard create_board(const Bitboard bitboards[])
{
/* TODO: check if memory allocation works */
size_t i;
ChessBoard board = (ChessBoard)malloc(13 * sizeof(Bitboard));
if (!board) { return NULL; }
Expand Down Expand Up @@ -68,7 +67,7 @@ ChessBoard create_board_from_piecesatpos(const ChessPieceAtPos pieces_at_pos[],
return board;
}

Bitboard is_captured_at(ChessBoard board, ChessPosition pos)
Bitboard is_captured_at(const Bitboard board[], ChessPosition pos)
{
Bitboard mask, all_pieces;

Expand All @@ -81,7 +80,7 @@ Bitboard is_captured_at(ChessBoard board, ChessPosition pos)
return (all_pieces & mask);
}

ChessPiece get_piece_at(ChessBoard board, ChessPosition pos)
ChessPiece get_piece_at(const Bitboard board[], ChessPosition pos)
{
int i;
ChessPiece piece = CHESS_PIECE_NULL;
Expand Down Expand Up @@ -111,12 +110,12 @@ ChessPiece get_piece_at(ChessBoard board, ChessPosition pos)
return piece;
}

int was_piece_moved(ChessBoard board, ChessPosition pos)
int was_piece_moved(const Bitboard board[], ChessPosition pos)
{
return ((~START_POSITIONS | board[12]) & (0x1uLL << pos)) > 0;
}

ChessBoard apply_draw(ChessBoard board, ChessDraw draw)
ChessBoard apply_draw(const Bitboard board[], ChessDraw draw)
{
uint8_t i;
ChessBoard new_board;
Expand All @@ -128,8 +127,6 @@ ChessBoard apply_draw(ChessBoard board, ChessDraw draw)

apply_draw_to_bitboards(new_board, draw);
return new_board;

/* TODO: check if the memory allocation actually works */
}

void apply_draw_to_bitboards(ChessBoard bitboards, ChessDraw draw)
Expand Down Expand Up @@ -198,3 +195,98 @@ void apply_draw_to_bitboards(ChessBoard bitboards, ChessDraw draw)
bitboards[promotion_board_index] ^= new_pos;
}
}

ChessBoard from_simple_board(const ChessPiece simple_board[])
{
uint8_t i, pos, white_pos, black_pos;
ChessPieceType piece_type;
ChessColor color;
Bitboard bitboard;
int set_bit;

/* assume pieces as already moved */
Bitboard was_moved = 0xFFFFFFFFFFFFFFFFuL;

/* allocate bitboards array */
ChessBoard bitboards = (ChessBoard)malloc(13 * sizeof(Bitboard));

/* loop through all bitboards */
for (i = 0; i < 12; i++)
{
/* determine the chess piece type and color of the iteration */
piece_type = (ChessPieceType)((i % 6) + 1);
color = (ChessColor)(i / 6);

/* init empty bitboard */
bitboard = 0;

/* loop through all positions */
for (pos = 0; pos < 64; pos++)
{
/* set piece bit if the position is captured */
set_bit = simple_board[pos] != CHESS_PIECE_NULL
&& get_piece_type(simple_board[pos]) == piece_type
&& get_piece_color(simple_board[pos]) == color;
bitboard |= set_bit ? 0x1uL << pos : 0x0uL;
}

/* apply converted bitboard */
bitboards[i] = bitboard;
}

/* init was moved bitboard */
for (i = 0; i < 16; i++)
{
white_pos = i;
black_pos = (uint8_t)(i + 48);

/* explicitly set was moved to 0 only for unmoved pieces */
was_moved ^= simple_board[white_pos] != CHESS_PIECE_NULL &&
!get_was_piece_moved(simple_board[white_pos]) ? 0x1uL << white_pos : 0x0uL;
was_moved ^= simple_board[black_pos] != CHESS_PIECE_NULL &&
!get_was_piece_moved(simple_board[black_pos]) ? 0x1uL << black_pos : 0x0uL;
}

/* apply converted bitboard */
bitboards[12] = was_moved;

return bitboards;
}

SimpleChessBoard to_simple_board(const Bitboard board[])
{
uint8_t i, pos;
ChessPieceType piece_type;
ChessColor color;
Bitboard bitboard;
SimpleChessBoard simple_board;

/* init pieces array with empty fields */
simple_board = (SimpleChessBoard)calloc(64, sizeof(ChessPiece));

// loop through all bitboards
for (i = 0; i < 12; i++)
{
// determine the chess piece type and color of the iteration
piece_type = (ChessPieceType)((i % 6) + 1);
color = (ChessColor)(i / 6);

// cache bitboard for shifting bitwise
bitboard = board[i];

// loop through all positions
for (pos = 0; pos < 64; pos++)
{
// write piece to array if there is one
simple_board[pos] = (bitboard & 0x1) > 0
? create_piece(piece_type, color, was_piece_moved(board, pos))
: simple_board[pos];

// shift bitboard
bitboard >>= 1;
}
}

// return a new chess board with the converted chess pieces
return simple_board;
}
14 changes: 10 additions & 4 deletions chesslib/src/chessdraw.c
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ ChessDraw create_draw_from_hash(uint32_t hash)
return (ChessDraw)hash;
}

ChessDrawType determine_draw_type(ChessBoard board, ChessPosition oldPos, ChessPosition newPos, ChessPieceType peasantPromotionType)
ChessDrawType determine_draw_type(const Bitboard board[], ChessPosition oldPos, ChessPosition newPos, ChessPieceType peasantPromotionType)
{
ChessDrawType type = Standard;
ChessPiece piece = get_piece_at(board, oldPos);
Expand All @@ -56,7 +56,7 @@ ChessDrawType determine_draw_type(ChessBoard board, ChessPosition oldPos, ChessP
return type;
}

ChessDraw create_draw(ChessBoard board, ChessPosition oldPos, ChessPosition newPos, ChessPieceType peasantPromotionType)
ChessDraw create_draw(const Bitboard board[], ChessPosition oldPos, ChessPosition newPos, ChessPieceType peasantPromotionType)
{
ChessPiece piece;
int is_first_move;
Expand All @@ -73,8 +73,8 @@ ChessDraw create_draw(ChessBoard board, ChessPosition oldPos, ChessPosition newP
draw_type = determine_draw_type(board, oldPos, newPos, peasantPromotionType);
drawing_side = get_piece_color(piece);
drawing_piece_type = get_piece_type(piece);
taken_piece_type =
(draw_type == EnPassant) ? Peasant : (is_captured_at(board, newPos) ? get_piece_type(get_piece_at(board, newPos)) : Invalid);
taken_piece_type = (draw_type == EnPassant) ? Peasant
: (is_captured_at(board, newPos) ? get_piece_type(get_piece_at(board, newPos)) : Invalid);

/* transform property values to a hash code */
draw = (ChessDraw)(
Expand All @@ -90,6 +90,12 @@ ChessDraw create_draw(ChessBoard board, ChessPosition oldPos, ChessPosition newP
return draw;
}

ChessDraw from_compact_draw(const Bitboard board[], CompactChessDraw comp_draw)
{
return create_draw(board, get_old_position(comp_draw),
get_new_position(comp_draw), get_peasant_promotion_piece_type(comp_draw));
}

CompactChessDraw to_compact_draw(ChessDraw draw)
{
return (CompactChessDraw)(draw & 0x7FFF);
Expand Down
Loading