From c9103840d936398902bff672df5dbfb542cced5b Mon Sep 17 00:00:00 2001 From: Ole Morud Date: Fri, 10 Jun 2022 14:15:49 +0200 Subject: [PATCH] Add board display feature --- chess.c | 66 +++++++++++++++++++++++++++++++++++++++++++++------------ 1 file changed, 52 insertions(+), 14 deletions(-) diff --git a/chess.c b/chess.c index 88d19e1..c00dc0f 100644 --- a/chess.c +++ b/chess.c @@ -1,4 +1,6 @@ +#include +#include #include #include #include @@ -14,37 +16,62 @@ #define WHITE 1 #define BLACK -1 -int main(){ - int *board; - init_board(board); - print_board(board); - - return 0; -} - - void print_board(int* board){ wchar_t piece; - + // The unicode value for a white chess king is 0x2654 // the following unicode 5 unicode characters are the // other white chess pieces. (matches macro definitions) // (https://en.wikipedia.org/wiki/Chess_symbols_in_Unicode) + for(int i=0; i<8; i++){ - for(int j=0; j<8; j++){ - piece = 0x2654 + board[i]; - putwchar(piece); + printf(" %i ", 8-i); // number coordinates + + for(int j=0; j<8; j++){ + int p = board[i*8+j]; + + // set tile color + if((i+j) % 2) + printf("\033[100;39m"); // "light black" + else + printf("\033[107;39m"); // "light white" + + if(p == E){ + putchar(' '); + }else{ + if(p > 0){ + printf("\033[37m"); // white foreground + }else{ + printf("\033[30m"); // black foreground + p *= -1; + } + + piece = 0x2659 + p; // unicode for black king is 0x267a (0x2659 + 1). + // The macro definitions of pieces align with + // unicode values to display the correct piece. + printf("%lc", piece); + } + + putchar(' '); } + + printf("\033[0m"); // reset text attributes putchar('\n'); } + + printf("\033[0m"); // reset text attributes + + // print letter coordinates + printf(" "); + for(int i=0; i<8; i++) printf(" %c", 'A'+i); } void init_board(int *board){ - // black pieces are prefixed by minus (-) + // black pieces are prefixed by a minus (-) const int start[] = { -R,-N,-B,-Q,-K,-B,-N,-R, -P,-P,-P,-P,-P,-P,-P,-P, @@ -58,3 +85,14 @@ void init_board(int *board){ memcpy(board, start, sizeof(start)); } + + +int main(){ + setlocale(LC_ALL, "C.UTF-8"); + + int *board; + init_board(board); + print_board(board); + + return 0; +}