mouvemenet presque fini

This commit is contained in:
Albert Braimi 2025-06-12 12:11:17 +02:00
parent fafb8aad4f
commit 36b962ece1
2 changed files with 75 additions and 38 deletions

BIN
README.md Normal file

Binary file not shown.

113
main.c
View File

@ -3,8 +3,6 @@
#include <unistd.h> #include <unistd.h>
#include <locale.h> #include <locale.h>
#define FG_WHITE_BG_WHITE "\x1b[38;5;255;48;5;255m"
#define FG_136_BG_136 "\x1b[38;5;136;48;5;136m"
#define FG_DEFAULT_BG_WHITE "\x1b[30;48;5;255m" #define FG_DEFAULT_BG_WHITE "\x1b[30;48;5;255m"
#define FG_DEFAULT_BG_136 "\x1b[30;48;5;136m" #define FG_DEFAULT_BG_136 "\x1b[30;48;5;136m"
@ -29,6 +27,7 @@ typedef enum {
BLACK BLACK
} States; } States;
char* piece_to_str(const Pieces piece) { char* piece_to_str(const Pieces piece) {
switch (piece) { switch (piece) {
case EMPTY: return "\u2800\u2800"; case EMPTY: return "\u2800\u2800";
@ -67,28 +66,43 @@ int piece_color_to_str(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}
};
int piece_color[8][8]; int piece_color[8][8];
int x = 1; int x = 0;
int y = 6; int y = 7;
int x_direction = 0;
int y_direction = 0;
int print_tab(Pieces board[8][8]) { //////////////////////////////////////////
int print_tab() {
for (int y = 0; y < 8; ++y) { for (int y = 0; y < 8; ++y) {
for (int x = 0; x < 8; ++x) { for (int x = 0; x < 8; ++x) {
if (y % 2 == 0) { if (y % 2 == 0) {
if (x % 2 == 0) { if (x % 2 == 0) {
printf(FG_DEFAULT_BG_WHITE"\x1b[%d;%dH%s"FG_WHITE_BG_WHITE, y + 1 , x * 2 + 1, piece_to_str(board[y][x])); printf(FG_DEFAULT_BG_WHITE"\x1b[%d;%dH%s", y + 1 , x * 2 + 1, piece_to_str(board[y][x]));
} }
if (x % 2 == 1) { if (x % 2 == 1) {
printf(FG_DEFAULT_BG_136"\x1b[%d;%dH%s"FG_136_BG_136, y + 1 , x * 2 + 1, piece_to_str(board[y][x])); printf(FG_DEFAULT_BG_136"\x1b[%d;%dH%s", y + 1 , x * 2 + 1, piece_to_str(board[y][x]));
} }
} }
if (y % 2 == 1) { if (y % 2 == 1) {
if (x % 2 == 1) { if (x % 2 == 1) {
printf(FG_DEFAULT_BG_WHITE"\x1b[%d;%dH%s"FG_WHITE_BG_WHITE, y + 1 , x * 2 + 1, piece_to_str(board[y][x])); printf(FG_DEFAULT_BG_WHITE"\x1b[%d;%dH%s", y + 1 , x * 2 + 1, piece_to_str(board[y][x]));
} }
if (x % 2 == 0) { if (x % 2 == 0) {
printf(FG_DEFAULT_BG_136"\x1b[%d;%dH%s"FG_136_BG_136, y + 1 , x * 2 + 1, piece_to_str(board[y][x])); printf(FG_DEFAULT_BG_136"\x1b[%d;%dH%s", y + 1 , x * 2 + 1, piece_to_str(board[y][x]));
} }
} }
} }
@ -97,82 +111,103 @@ int print_tab(Pieces board[8][8]) {
return 0; return 0;
} }
int piece_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]));
}
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]));
}
}
if (y % 2 == 0) {
if (x % 2 == 1) {
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 == 0) {
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]));
}
}
printf("\x1b[%d;%dH\x1b[39;49ma", y + 1 , x * 2 + 1);
}
int controls() { int controls() {
piece_selection();
while (1) { while (1) {
x_direction = 0;
y_direction = 0;
int input = getchar(); int input = getchar();
switch (input) { switch (input) {
case 104: // h case 104: // h
if (x > 0 && (piece_color[y][x - 1] == 1 || piece_color[y][x - 1] == 2)) { if (x > 0 && (piece_color[y][x - 1] == 1 || piece_color[y][x - 1] == 2)) {
x--; x--;
printf("%d", x); x_direction--;
piece_selection();
} }
else { else {
x = 7; x = 7;
printf("%d", x); x_direction = 7;
piece_selection();
} }
break; break;
case 106: // j case 106: // j
if (y < 7 && (piece_color[y][x - 1] == 1 || piece_color[y][x - 1] == 2)) { if (y < 7 && (piece_color[y][x - 1] == 1 || piece_color[y][x - 1] == 2)) {
y++; y++;
printf("%d", y); y_direction++;
piece_selection();
} }
else { else {
y = 0; y = 6;
printf("%d", y); y_direction--;
piece_selection();
} }
break; break;
case 107: // k case 107: // k
if (y > 0 && (piece_color[y][x - 1] == 1 || piece_color[y][x - 1] == 2)) { if (y > 6 && (piece_color[y][x - 1] == 1 || piece_color[y][x - 1] == 2)) {
y--; y--;
printf("%d", y); y_direction = -1;
piece_selection();
} }
else { else {
y = 7; y = 7;
printf("%d", y); y_direction = 1;
piece_selection();
} }
break; break;
case 108: // l case 108: // l
if (x < 7 && (piece_color[y][x - 1] == 1 || piece_color[y][x - 1] == 2)) { if (x < 7 && (piece_color[y][x - 1] == 1 || piece_color[y][x - 1] == 2)) {
x++; x++;
printf("%d", x); x_direction++;
piece_selection();
} }
else { else {
x = 0; x = 0;
printf("%d", x); x_direction = -7;
piece_selection();
} }
break; break;
default: printf("sigma "); break; default: break;
} }
} }
return 0; return 0;
} }
int piece_selecton() {
printf("\x1b[%d;%dh\x1b[39;49m", y, x);
}
int main() { int main() {
const struct termios old_termios; const struct termios old_termios;
tcsetattr(STDIN_FILENO, TCSANOW, &old_termios); tcsetattr(STDIN_FILENO, TCSANOW, &old_termios);
setlocale(LC_ALL, "en_US.UTF-8"); setlocale(LC_ALL, "en_US.UTF-8");
printf("\n"); printf("\n");
printf("\x1b[H"); printf("\x1b[H");
printf("\x1b[2J"); printf("\x1b[?25l");
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}
};
for (int y = 0; y < 8; ++y) { for (int y = 0; y < 8; ++y) {
for (int x = 0; x < 8; ++x) { for (int x = 0; x < 8; ++x) {
@ -182,7 +217,9 @@ int main() {
print_tab(board); print_tab(board);
a(); controls();