From 2b838ff44a96f6566e8414d8bd7d20b6d2725cc0 Mon Sep 17 00:00:00 2001 From: Albert Braimi Date: Wed, 18 Jun 2025 09:58:15 +0200 Subject: [PATCH] correction du bug de couleur du bg quand on change de piece --- main.c | 136 ++++++++++++++++++++++++++++++++++++++++++++++----------- 1 file changed, 110 insertions(+), 26 deletions(-) diff --git a/main.c b/main.c index 93baf98..f5a9194 100644 --- a/main.c +++ b/main.c @@ -69,16 +69,15 @@ int piece_color_to_int(Pieces piece) { } Pieces board[8][8] = { - {BLACK_ROOK, BLACK_KNIGHT, BLACK_BISHOP, BLACK_QUEEN, BLACK_KING, BLACK_BISHOP, BLACK_KNIGHT, BLACK_ROOK}, - {BLACK_PAWN, BLACK_PAWN, BLACK_PAWN, BLACK_PAWN, BLACK_PAWN, BLACK_PAWN, BLACK_PAWN, BLACK_PAWN}, - {EMPTY, EMPTY, EMPTY, EMPTY, EMPTY, EMPTY, EMPTY, EMPTY}, - {EMPTY, EMPTY, EMPTY, EMPTY, EMPTY, EMPTY, EMPTY, EMPTY}, - {EMPTY, EMPTY, EMPTY, EMPTY, EMPTY, EMPTY, EMPTY, EMPTY}, - {EMPTY, EMPTY, EMPTY, EMPTY, EMPTY, EMPTY, EMPTY, EMPTY}, - {WHITE_PAWN, WHITE_PAWN, WHITE_PAWN, WHITE_PAWN, WHITE_PAWN, WHITE_PAWN, WHITE_PAWN, WHITE_PAWN}, - {WHITE_ROOK, WHITE_KNIGHT, WHITE_BISHOP, WHITE_QUEEN, WHITE_KING, WHITE_BISHOP, WHITE_KNIGHT, WHITE_ROOK} + {BLACK_ROOK, EMPTY, BLACK_BISHOP, EMPTY, EMPTY, BLACK_ROOK, BLACK_KING, EMPTY}, + {BLACK_PAWN, BLACK_PAWN, BLACK_PAWN, EMPTY, BLACK_PAWN, BLACK_PAWN, BLACK_PAWN, BLACK_PAWN}, + {EMPTY, BLACK_KNIGHT, EMPTY, BLACK_PAWN, EMPTY, EMPTY, EMPTY, EMPTY}, + {EMPTY, EMPTY, EMPTY, EMPTY, WHITE_PAWN, BLACK_KNIGHT, EMPTY, EMPTY}, + {EMPTY, EMPTY, WHITE_BISHOP, WHITE_PAWN, EMPTY, EMPTY, EMPTY, EMPTY}, + {EMPTY, EMPTY, WHITE_KNIGHT, EMPTY, EMPTY, WHITE_KNIGHT, EMPTY, EMPTY}, + {WHITE_PAWN, WHITE_PAWN, WHITE_PAWN, EMPTY, EMPTY, WHITE_PAWN, WHITE_PAWN, WHITE_PAWN}, + {WHITE_ROOK, EMPTY, EMPTY, WHITE_QUEEN, EMPTY, WHITE_ROOK, WHITE_KING, EMPTY} }; - int piece_color[8][8]; int x = 0; int y = 7; @@ -88,7 +87,7 @@ bool player_track = true; ////////////////////////////////////////// -int print_tab() { +void print_tab() { for (int y = 0; y < 8; ++y) { for (int x = 0; x < 8; ++x) { if (y % 2 == 0) { @@ -108,16 +107,16 @@ int print_tab() { } } } + printf("\n"); } - printf("\x1b[39;49m"); - return 0; + printf("\x1b[0m"); } void tile_selection() { if (y % 2 == 1) { if (x % 2 == 0) { - printf(FG_DEFAULT_BG_WHITE"\x1b[%d;%dH%s", y - y_direction + 1 , (x - x_direction) * 2 + 1, piece_to_str(board[y - y_direction][x-x_direction])); + printf(FG_DEFAULT_BG_WHITE"\x1b[%d;%dH%s", y - y_direction + 1 , (x - x_direction) * 2 + 1, piece_to_str(board[y - y_direction][x - x_direction])); } if (x % 2 == 1) { printf(FG_DEFAULT_BG_136"\x1b[%d;%dH%s", y - y_direction + 1 , (x - x_direction) * 2 + 1, piece_to_str(board[y - y_direction][x - x_direction])); @@ -163,10 +162,71 @@ void controls() { switch (input) { case 104: // h + if (x > 0) { + for (int i = x; i > 0; --i) { + x--; + x_direction--; + if (piece_color[y][x] == 1 || piece_color[y][x] == 2) { + break; + } + } + } + else { + x = 7; + x_direction = 7; + tile_selection(); + x_direction = 0; + for (int i = x; i > 0; --i) { + if (piece_color[y][x] == 1 || piece_color[y][x] == 2) { + break; + } + x--; + x_direction--; + } + } + tile_selection(); + break; case 106: // j + if (y < 7) { + for (int i = y; i < 7; ++i) { + y++; + y_direction++; + if (piece_color[y][x] == 1 || piece_color[y][x] == 2) { + break; + } + } + } + else { + y = 0; + y_direction = -7; + tile_selection(); + y_direction = 0; + for (int i = y; i < 7; ++i) { + if (piece_color[y][x] == 1 || piece_color[y][x] == 2) { + break; + } + y++; + y_direction++; + } + } + tile_selection(); + break; case 107: // k - if (y >= 0) { - for (int i = y; i < 8; ++i) { + if (y > 0) { + for (int i = y; i > 0; --i) { + y--; + y_direction--; + if (piece_color[y][x] == 1 || piece_color[y][x] == 2) { + break; + } + } + } + else { + y = 7; + y_direction = 7; + tile_selection(); + y_direction = 0; + for (int i = y; i > 0; --i) { if (piece_color[y][x] == 1 || piece_color[y][x] == 2) { break; } @@ -174,12 +234,33 @@ void controls() { y_direction--; } } + tile_selection(); + break; + case 108: // l + if (x < 7) { + for (int i = x; i < 7; ++i) { + x++; + x_direction++; + if (piece_color[y][x] == 1 || piece_color[y][x] == 2) { + break; + } + } + } else { - y = 7; - y_direction = 1; + x = 0; + x_direction = -7; + tile_selection(); + x_direction = 0; + for (int i = x; i < 7; ++i) { + if (piece_color[y][x] == 1 || piece_color[y][x] == 2) { + break; + } + x++; + x_direction++; + } } tile_selection(); - case 108: // l + break; case 13: // enter @@ -190,16 +271,21 @@ void controls() { exit(0); default: break; } + fflush(stdout); } } int main() { - const struct termios old_termios; - tcsetattr(STDIN_FILENO, TCSANOW, &old_termios); + struct termios t_old, t_new; + + tcgetattr(STDIN_FILENO, &t_old); + t_new = t_old; + t_new.c_lflag &= ~(ICANON | ECHO); + tcsetattr(STDIN_FILENO, TCSANOW, &t_new); + setbuf(stdout, NULL); setlocale(LC_ALL, "en_US.UTF-8"); - printf("\n"); - printf("\x1b[H"); + printf("\x1b[H\x1b[J"); printf("\x1b[?25l"); for (int y = 0; y < 8; ++y) { @@ -213,11 +299,9 @@ int main() { controls(); - - - - getchar(); + printf("\x1b[0m"); printf("\x1b[10;0H"); + tcsetattr(STDIN_FILENO, TCSANOW, &t_old); return 0; }