diff options
author | eug-vs <eugene@eug-vs.xyz> | 2022-09-14 17:49:07 +0300 |
---|---|---|
committer | eug-vs <eugene@eug-vs.xyz> | 2022-09-14 17:50:55 +0300 |
commit | a881be4856c6d3dc7675059d5a635d34434b5797 (patch) | |
tree | de1551798279a2fa048e70c977b3c3f98f515d62 | |
parent | 71f28db1b8e26377d56f0eb0aef01f6c6c3afbd0 (diff) | |
download | j1chess-a881be4856c6d3dc7675059d5a635d34434b5797.tar.gz |
refactor: use lowerCamelCase for functions
-rw-r--r-- | src/bitboard.c | 8 | ||||
-rw-r--r-- | src/bitboard.h | 4 | ||||
-rw-r--r-- | src/board.c | 10 | ||||
-rw-r--r-- | src/board.h | 14 | ||||
-rw-r--r-- | src/main.c | 56 | ||||
-rw-r--r-- | src/unittest.c | 4 | ||||
-rw-r--r-- | src/unittest.h | 4 |
7 files changed, 50 insertions, 50 deletions
diff --git a/src/bitboard.c b/src/bitboard.c index cffe40f..c0abbf8 100644 --- a/src/bitboard.c +++ b/src/bitboard.c @@ -3,7 +3,7 @@ /* Print bitboard on screen in the same way squares appear in memory * (i.e the board is actually flipped along X) */ -void print_bitboard(Bitboard bb) { +void printBitboard(Bitboard bb) { for (U64 index = 0; index < 64; index++) { printf("%lu", (bb >> index) & 1); if ((index + 1) % 8 == 0) printf("\n"); @@ -12,9 +12,9 @@ void print_bitboard(Bitboard bb) { } /* Return bitboard cardinality, aka number of elements in the set */ -inline int pop_count(Bitboard bb){ +inline int popCount(Bitboard bb){ if (bb == 0) return 0; - return pop_count(bb >> 1) + (bb & 1); + return popCount(bb >> 1) + (bb & 1); } /* Return Bitboard with only Least Single Bit */ @@ -26,7 +26,7 @@ inline Bitboard ls1b(Bitboard bb) { * Only works for SINGLE Bitboards * Useful for calculating bit-index of LS1B */ inline int bitscan(Bitboard bb) { - return pop_count(ls1b(bb) - 1); + return popCount(ls1b(bb) - 1); } /* Bitscan forward with LS1B reset */ diff --git a/src/bitboard.h b/src/bitboard.h index ebac2f7..6b89c84 100644 --- a/src/bitboard.h +++ b/src/bitboard.h @@ -3,9 +3,9 @@ typedef U64 Bitboard; #define BIT (U64)1 -int pop_count(Bitboard bb); +int popCount(Bitboard bb); -void print_bitboard(Bitboard bb); +void printBitboard(Bitboard bb); Bitboard ls1b(Bitboard bb); diff --git a/src/board.c b/src/board.c index d88c1ee..c5c4eb2 100644 --- a/src/board.c +++ b/src/board.c @@ -2,7 +2,7 @@ #include <string.h> #include "board.h" -Board parse_FEN(char* FEN) { +Board parseFEN(char* FEN) { Board board; for (int i = 0; i < 12; i++) board.pieces[i] = 0; @@ -65,7 +65,7 @@ Board parse_FEN(char* FEN) { } -void print_board(Board board) { +void printBoard(Board board) { printf("\n"); for (int rank = 7; rank >= 0; rank--) { printf("%i|", rank + 1); @@ -89,7 +89,7 @@ void print_board(Board board) { printf(" a b c d e f g h\n"); } -void precompute_knight_attack_table(Bitboard attacks[64]) { +void precomputeKnightAttackTable(Bitboard attacks[64]) { for (int index = 0; index < 64; index++) { U64 position = BIT << index; @@ -105,7 +105,7 @@ void precompute_knight_attack_table(Bitboard attacks[64]) { } } -void precompute_king_attack_table(Bitboard attacks[64]) { +void precomputeKingAttackTable(Bitboard attacks[64]) { for (int index = 0; index < 64; index++) { U64 position = BIT << index; @@ -116,7 +116,7 @@ void precompute_king_attack_table(Bitboard attacks[64]) { } } -void precompute_pawn_attack_table(Bitboard attacks[64], BYTE color) { +void precomputePawnAttackTable(Bitboard attacks[64], BYTE color) { for (int index = 0; index < 64; index++) { U64 position = BIT << index; diff --git a/src/board.h b/src/board.h index e3309e2..e24a130 100644 --- a/src/board.h +++ b/src/board.h @@ -13,8 +13,8 @@ typedef struct { Bitboard pieces[12]; BYTE side; - BYTE castling_rights; - BYTE en_passant_square; + BYTE castlingRights; + BYTE enPassantSquare; } Board; enum enumSquare { @@ -53,10 +53,10 @@ static const char* pieces[] = { #define notGFile 0xBFBFBFBFBFBFBFBF #define notHFile 0x7F7F7F7F7F7F7F7F -Board parse_FEN(char* FEN); +Board parseFEN(char* FEN); -void print_board(Board board); +void printBoard(Board board); -void precompute_knight_attack_table(Bitboard table[64]); -void precompute_king_attack_table(Bitboard table[64]); -void precompute_pawn_attack_table(Bitboard table[64], BYTE color); +void precomputeKnightAttackTable(Bitboard table[64]); +void precomputeKingAttackTable(Bitboard table[64]); +void precomputePawnAttackTable(Bitboard table[64], BYTE color); @@ -3,75 +3,75 @@ #include "bitboard.h" int main() { - start_test_section("Bitboards"); { - unit_test(pop_count(0b01110) == 3, "Pop count of 01110 is 3"); + testSection("Bitboards"); { + unitTest(popCount(0b01110) == 3, "Pop count of 01110 is 3"); Bitboard bb = 0b1100; - unit_test(bitscanAndReset(&bb) == 2, "Bitscan of 0b1100 is 2"); - unit_test(bb == 0b1000, "After bitscan with reset the LS1B is flipped"); + unitTest(bitscanAndReset(&bb) == 2, "Bitscan of 0b1100 is 2"); + unitTest(bb == 0b1000, "After bitscan with reset the LS1B is flipped"); } - start_test_section("Default FEN string"); { - Board board = parse_FEN("rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1"); - unit_test(pop_count(board.pieces[PAWN] | board.pieces[PAWN | BLACK]) == 16, "There are 16 pawns total"); - unit_test(pop_count(board.pieces[ROOK]) == 2, "There are 2 white rooks"); + testSection("Default FEN string"); { + Board board = parseFEN("rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1"); + unitTest(popCount(board.pieces[PAWN] | board.pieces[PAWN | BLACK]) == 16, "There are 16 pawns total"); + unitTest(popCount(board.pieces[ROOK]) == 2, "There are 2 white rooks"); // TODO - unit_test(board.side == WHITE, "Side to move is white"); - unit_test(board.castling_rights == 0, "Both sides can castle"); - unit_test(board.en_passant_square == 0, "No en passant move is avaialble"); + unitTest(board.side == WHITE, "Side to move is white"); + unitTest(board.castlingRights == 0, "Both sides can castle"); + unitTest(board.enPassantSquare == 0, "No en passant move is avaialble"); } - start_test_section("Test knight attacks"); { + testSection("Test knight attacks"); { Bitboard attacks[64]; - precompute_knight_attack_table(attacks); + precomputeKnightAttackTable(attacks); int max_attacks = 0; for (int i = 0; i < 64; i++) { - int attack_count = pop_count(attacks[i]); + int attack_count = popCount(attacks[i]); if (attack_count > max_attacks) max_attacks = attack_count; } - unit_test(max_attacks == 8, "Max amount of knight attacks should be 8"); + unitTest(max_attacks == 8, "Max amount of knight attacks should be 8"); - unit_test( + unitTest( attacks[b7] == ((BIT << d8) | (BIT << d6) | (BIT << c5) | (BIT << a5)), "Knight on b7 attacks only d8, d6, c5, a5" ); } - start_test_section("Test king attacks"); { + testSection("Test king attacks"); { Bitboard attacks[64]; - precompute_king_attack_table(attacks); + precomputeKingAttackTable(attacks); int max_attacks = 0; for (int i = 0; i < 64; i++) { - int attack_count = pop_count(attacks[i]); + int attack_count = popCount(attacks[i]); if (attack_count > max_attacks) max_attacks = attack_count; } - unit_test(max_attacks == 8, "Max amount of king attacks should be 8"); + unitTest(max_attacks == 8, "Max amount of king attacks should be 8"); - unit_test( + unitTest( attacks[h2] == ((BIT << h1) | (BIT << g1) | (BIT << g2) | (BIT << g3) | (BIT << h3)), "King on h2 attacks only h1, g1, g2, g3, h3" ); } - start_test_section("Test pawn attacks"); { + testSection("Test pawn attacks"); { Bitboard white_attacks[64]; Bitboard black_attacks[64]; - precompute_pawn_attack_table(white_attacks, WHITE); - precompute_pawn_attack_table(black_attacks, BLACK); + precomputePawnAttackTable(white_attacks, WHITE); + precomputePawnAttackTable(black_attacks, BLACK); int is_same = 0; int max_attacks = 0; for (int i = 0; i < 64; i++) { - int attack_count = pop_count(white_attacks[i]); + int attack_count = popCount(white_attacks[i]); if (attack_count > max_attacks) max_attacks = attack_count; if (white_attacks[i] == black_attacks[i]) is_same = 1; } - unit_test(max_attacks == 2, "Max amount of pawn attacks should be 2"); - unit_test(is_same == 0, "Black and white pawns always move differently"); - unit_test(white_attacks[g2] == (BIT << h3 | BIT << f3), "White pawn on g2 attacks only h3 and f3"); + unitTest(max_attacks == 2, "Max amount of pawn attacks should be 2"); + unitTest(is_same == 0, "Black and white pawns always move differently"); + unitTest(white_attacks[g2] == (BIT << h3 | BIT << f3), "White pawn on g2 attacks only h3 and f3"); } report(); diff --git a/src/unittest.c b/src/unittest.c index af81cd9..137e899 100644 --- a/src/unittest.c +++ b/src/unittest.c @@ -16,14 +16,14 @@ bool assert(bool expression, const char* message) { return expression; } -void unit_test(bool expression, const char* subject) { +void unitTest(bool expression, const char* subject) { TOTAL++; SECTION_TOTAL++; printf( "%d.%d %s: %s\n", SECTION_NUMBER, SECTION_TOTAL, subject, expression? draw(GRN, PASS) : draw(RED, FAIL)); if (expression) SUCCESS++; } -void start_test_section(const char* name) { +void testSection(const char* name) { SECTION_TOTAL = 0; SECTION_NUMBER++; printf("%d.%s %s %s\n", SECTION_NUMBER, CYAN, name, RESET); diff --git a/src/unittest.h b/src/unittest.h index e32ee10..9a6d50e 100644 --- a/src/unittest.h +++ b/src/unittest.h @@ -14,8 +14,8 @@ bool assert(bool expression, const char* message); -void unit_test(bool expression, const char* subject); +void unitTest(bool expression, const char* subject); -void start_test_section(const char* name); +void testSection(const char* name); bool report(); |