Can print the board now

This commit is contained in:
cdricms
2023-12-28 00:23:45 +01:00
parent 5732856355
commit 3da9d6a0d1
4 changed files with 81 additions and 10 deletions

View File

@@ -7,6 +7,20 @@
#define QUEEN_MAXMOVES 27 #define QUEEN_MAXMOVES 27
#define KING_MAXMOVES 10 // Castling has to be taken into account #define KING_MAXMOVES 10 // Castling has to be taken into account
#define WHITE_PAWN "\u2659"
#define WHITE_KNIGHT "\u2658"
#define WHITE_BISHOP "\u2657"
#define WHITE_ROOK "\u2656"
#define WHITE_QUEEN "\u2655"
#define WHITE_KING "\u2654"
#define BLACK_PAWN "\u265F"
#define BLACK_KNIGHT "\u265E"
#define BLACK_BISHOP "\u265D"
#define BLACK_ROOK "\u265C"
#define BLACK_QUEEN "\u265B"
#define BLACK_KING "\u265A"
PieceColors piece_getColor(const Pieces piece) { return Black & piece; } PieceColors piece_getColor(const Pieces piece) { return Black & piece; }
Piece piece_getColorlessPiece(const Pieces piece) { Piece piece_getColorlessPiece(const Pieces piece) {
return piece_getColor(piece) ^ piece; return piece_getColor(piece) ^ piece;
@@ -165,3 +179,44 @@ bool board_movePiece(Board board, const Coordinates from,
b[from_index].piece = -1; b[from_index].piece = -1;
return true; return true;
} }
void board_print(const Board board) {
printf("┌───┬───┬───┬───┬───┬───┬───┬───┐\n");
uint i = 0;
while (i < 64) {
PieceColors piece_color = piece_getColor(board.squares[i].piece);
Pieces bare_piece = piece_getColorlessPiece(board.squares[i].piece);
char *unicode;
switch (bare_piece) {
case Pawn:
unicode = piece_color == White ? WHITE_PAWN : BLACK_PAWN;
break;
case Knight:
unicode = piece_color == White ? WHITE_KNIGHT : BLACK_KNIGHT;
break;
case Bishop:
unicode = piece_color == White ? WHITE_BISHOP : BLACK_BISHOP;
break;
case Rook:
unicode = piece_color == White ? WHITE_ROOK : BLACK_ROOK;
break;
case Queen:
unicode = piece_color == White ? WHITE_QUEEN : BLACK_QUEEN;
break;
case King:
unicode = piece_color == White ? WHITE_KING : BLACK_KING;
break;
}
printf("│ %s ", board.squares[i].piece == -1 ? " " : unicode);
if ((i + 1) % 8 == 0) {
const uint row = ((64 - i + 1) / 8) % 8 + 1;
printf("│ %d\n", row);
if (row != 1)
printf("├───┼───┼───┼───┼───┼───┼───┼───┤\n");
}
i++;
}
printf("└───┴───┴───┴───┴───┴───┴───┴───┘\n");
printf(" A B C D E F G H");
}

View File

@@ -28,5 +28,6 @@ Piece piece_getColorlessPiece(const Pieces piece);
bool board_movePiece(Board board, const Coordinates from, const Coordinates to); bool board_movePiece(Board board, const Coordinates from, const Coordinates to);
int board_getIndex(const Coordinates coords); int board_getIndex(const Coordinates coords);
Coordinates *piece_getMoves(const Pieces piece, const Coordinates piece_coords); Coordinates *piece_getMoves(const Pieces piece, const Coordinates piece_coords);
void board_print(const Board board);
#endif // !__BOARD_HEADER #endif // !__BOARD_HEADER

View File

@@ -2,7 +2,19 @@
#define __FEN_HEADER #define __FEN_HEADER
#include "board.h" #include "board.h"
typedef char FEN[75]; /*
<board> <turn> <castling> <en passant target> <halfmove clock> <fmove number>;
<board> represents the positions of pieces on the board.
<turn> indicates which player's turn it is.
<castling> specifies castling availability.
<en passant target> indicates the square to which an en passant capture is
possible.
<halfmove clock> is the number of halfmoves since the last capture or
pawn advance.
<fullmove number> is the number of the full move.
*/
typedef char FEN[89];
void fen_fromBoard(const Board board, FEN fen); void fen_fromBoard(const Board board, FEN fen);
void fen_toBoard(const FEN fen, Board *board); void fen_toBoard(const FEN fen, Board *board);

View File

@@ -1,21 +1,24 @@
#include "engine.h" #include "engine.h"
#include <locale.h>
void printFEN(void *value) { printf("%s", (char *)value); } void printFEN(void *value) { printf("%s", (char *)value); }
void printString(void *value) { printf("%s", *(char **)value); } void printString(void *value) { printf("%s", *(char **)value); }
int main() { int main() {
setlocale(LC_ALL, "");
Board board; Board board;
FEN fen = "rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR"; FEN fen = "rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR";
fen_toBoard(fen, &board); fen_toBoard(fen, &board);
board.history = linkedList_init(&fen); board_print(board);
Coordinates *coords = // board.history = linkedList_init(&fen);
piece_getMoves(board.squares[3].piece, board.squares[3].coords); // Coordinates *coords =
char a[] = "hello"; // piece_getMoves(board.squares[3].piece, board.squares[3].coords);
linkedList_append(board.history, &a); // char a[] = "hello";
printf("%d\n", linkedList_length(board.history)); // linkedList_append(board.history, &a);
linkedList_print(board.history, printFEN); // printf("%d\n", linkedList_length(board.history));
free(coords); // linkedList_print(board.history, printFEN);
linkedList_removeAll(board.history); // free(coords);
// linkedList_removeAll(board.history);
// char *a = "hello"; // char *a = "hello";
// LinkedList *head = linkedList_init(&a); // LinkedList *head = linkedList_init(&a);
// char *b = "world"; // char *b = "world";