checc/main.c
2025-06-18 15:45:30 +02:00

354 lines
12 KiB
C

#include <stdio.h>
#include <termios.h>
#include <unistd.h>
#include <locale.h>
#include <stdlib.h>
#include <stdbool.h>
#define FG_DEFAULT_BG_WHITE "\x1b[30;48;5;255m"
#define FG_DEFAULT_BG_136 "\x1b[30;48;5;136m"
#define FG_RED_BG_RED "\x1b[31;41m"
#define FG_CYAN_BG_CYAN "\x1b[36;46m"
typedef enum {
EMPTY,
BLACK_PAWN,
BLACK_ROOK,
BLACK_KNIGHT,
BLACK_BISHOP,
BLACK_QUEEN,
BLACK_KING,
WHITE_PAWN,
WHITE_ROOK,
WHITE_KNIGHT,
WHITE_BISHOP,
WHITE_QUEEN,
WHITE_KING
} Pieces;
char* piece_to_str(const Pieces piece) {
switch (piece) {
case EMPTY: return "\u2800\u2800";
case BLACK_PAWN: return "\u265f\u2800";
case BLACK_ROOK: return "\u265c\u2800";
case BLACK_KNIGHT: return "\u265e\u2800";
case BLACK_BISHOP: return "\u265d\u2800";
case BLACK_QUEEN: return "\u265b\u2800";
case BLACK_KING: return "\u265a\u2800";
case WHITE_PAWN: return "\u2659\u2800";
case WHITE_ROOK: return "\u2656\u2800";
case WHITE_KNIGHT: return "\u2658\u2800";
case WHITE_BISHOP: return "\u2657\u2800";
case WHITE_QUEEN: return "\u2655\u2800";
case WHITE_KING: return "\u2654\u2800";
default: return 0;
}
}
int piece_color_to_int(Pieces piece) {
switch (piece) {
case EMPTY: return 3;
case BLACK_PAWN:
case BLACK_ROOK:
case BLACK_KNIGHT:
case BLACK_BISHOP:
case BLACK_QUEEN:
case BLACK_KING: return 2;
case WHITE_PAWN:
case WHITE_ROOK:
case WHITE_KNIGHT:
case WHITE_BISHOP:
case WHITE_QUEEN:
case WHITE_KING: return 1;
default: return 3;
}
}
Pieces board[8][8] = {
{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;
int x_direction = 0;
int y_direction = 0;
bool player_track = true;
//////////////////////////////////////////
void print_tab() {
for (int y = 0; y < 8; ++y) {
for (int x = 0; x < 8; ++x) {
if (y % 2 == 0) {
if (x % 2 == 0) {
printf(FG_DEFAULT_BG_WHITE"\x1b[%d;%dH%s", y + 1 , x * 2 + 1, piece_to_str(board[y][x]));
}
if (x % 2 == 1) {
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 (x % 2 == 1) {
printf(FG_DEFAULT_BG_WHITE"\x1b[%d;%dH%s", y + 1 , x * 2 + 1, piece_to_str(board[y][x]));
}
if (x % 2 == 0) {
printf(FG_DEFAULT_BG_136"\x1b[%d;%dH%s", y + 1 , x * 2 + 1, piece_to_str(board[y][x]));
}
}
}
printf("\n");
}
printf("\x1b[0m");
}
void tile_selection() {
print_tab();
printf("\x1b[%d;%dH\x1b[39;106m%s", y + 1 , x * 2 + 1, piece_to_str(board[y][x]));
printf("\x1b[0m");
}
void a() {
if (board[y][x] == BLACK_PAWN) {
if (piece_color_to_int(board[y + 1][x]) == 3) {
printf("\x1b[%d;%dH\x1b[96;106m\u2800\u2800", y + 1 + 1 , x * 2 + 1);
if (piece_color_to_int(board[y + 1][x]) == 3) {
printf("\x1b[%d;%dH\x1b[96;106m\u2800\u2800", y + 2 + 1 , x * 2 + 1);
}
}
if (piece_color_to_int(board[y + 1][x + 1]) == 1) {
printf(FG_CYAN_BG_CYAN"\x1b[%d;%dH\u2800\u2800", y - 1 + 1 , x * 2 + 2 + 1);
}
if (piece_color_to_int(board[y + 1][x - 1]) == 1) {
printf(FG_CYAN_BG_CYAN"\x1b[%d;%dH\u2800\u2800", y - 1 + 1 , x * 2 - 2 + 1);
}
}
if (board[y][x] == WHITE_PAWN) {
if (piece_color_to_int(board[y - 1][x]) == 3) {
printf("\x1b[%d;%dH\x1b[96;106m\u2800\u2800", y - 1 + 1 , x * 2 + 1);
if (piece_color_to_int(board[y - 1][x]) == 3) {
printf("\x1b[%d;%dH\x1b[96;106m\u2800\u2800", y - 2 + 1 , x * 2 + 1);
}
}
if (piece_color_to_int(board[y - 1][x + 1]) == 2) {
printf(FG_CYAN_BG_CYAN"\x1b[%d;%dH\u2800\u2800", y - 1 + 1 , x * 2 + 2 + 1);
}
if (piece_color_to_int(board[y - 1][x - 1]) == 2) {
printf(FG_CYAN_BG_CYAN"\x1b[%d;%dH\u2800\u2800", y - 1 + 1 , x * 2 - 2 + 1);
}
}
else {
printf("\x1b[10;0H");
}
while (1){}
}
void controls() {
while (1) {
tile_selection();
x_direction = 0;
y_direction = 0;
int input = getchar();
switch (input) {
case 104: // h
if (x == 0 && y == 0) {
x = 7;
y = 7;
x_direction = 7;
y_direction = 7;
}
if (x > 0) {
for (x--; x > 0; x--) {
x_direction--;
if (piece_color_to_int(board[y][x]) == 1 || piece_color_to_int(board[y][x]) == 2) {
break;
}
}
}
else if (x == 0) {
y--;
x = 7;
x_direction = 7;
tile_selection();
x_direction = 0;
for (; x > 0; x--) {
if (piece_color_to_int(board[y][x]) == 1 || piece_color_to_int(board[y][x]) == 2) {
break;
}
x_direction--;
}
}
if (x == 0 && piece_color_to_int(board[y][x]) == 3) {
y--;
x = 7;
x_direction = 7;
tile_selection();
x_direction = 0;
for (; x > 0; x--) {
if (piece_color_to_int(board[y][x]) == 1 || piece_color_to_int(board[y][x]) == 2) {
break;
}
x_direction--;
}
}
break;
case 106: // j
if (x == 0 && y == 7) {
x = 7;
y = 0;
x_direction = 7;
y_direction = -7;
}
if (y < 7) {
for (y++; y < 7; y++) {
y_direction++;
if (piece_color_to_int(board[y][x]) == 1 || piece_color_to_int(board[y][x]) == 2) {
break;
}
}
}
else if (y == 7) {
x--;
y = 0;
y_direction = -7;
tile_selection();
y_direction = 0;
for (; y < 7; y++) {
if (piece_color_to_int(board[y][x]) == 1 || piece_color_to_int(board[y][x]) == 2) {
break;
}
y_direction++;
}
}
if (y == 7 && piece_color_to_int(board[y][x]) == 3) {
x--;
y = 0;
y_direction = -7;
tile_selection();
y_direction = 0;
for (; y < 7; y++) {
if (piece_color_to_int(board[y][x]) == 1 || piece_color_to_int(board[y][x]) == 2) {
break;
}
y_direction++;
}
}
break;
case 107: // k
if (x == 0 && y == 0) {
x = 7;
y = 7;
x_direction = 7;
y_direction = 7;
}
if (y > 0) {
for (y--; y > 0; y--) {
y_direction--;
if (piece_color_to_int(board[y][x]) == 1 || piece_color_to_int(board[y][x]) == 2) {
break;
}
}
}
else if (y == 0) {
x
y = 7;
y_direction = 7;
tile_selection();
y_direction = 0;
for (; y > 0; y--) {
if (piece_color_to_int(board[y][x]) == 1 || piece_color_to_int(board[y][x]) == 2) {
break;
}
y_direction--;
}
}
if (y == 0 && piece_color_to_int(board[y][x]) == 3) {
y = 7;
y_direction = 7;
tile_selection();
y_direction = 0;
for (; y > 0; y--) {
if (piece_color_to_int(board[y][x]) == 1 || piece_color_to_int(board[y][x]) == 2) {
break;
}
y_direction--;
}
}
break;
case 108: // l
if (x < 7) {
for (x++; x < 7; x++) {
x_direction++;
if (piece_color_to_int(board[y][x]) == 1 || piece_color_to_int(board[y][x]) == 2) {
break;
}
}
}
else if (x == 7) {
x = 0;
x_direction = -7;
tile_selection();
x_direction = 0;
for (; x < 7; x++) {
if (piece_color_to_int(board[y][x]) == 1 || piece_color_to_int(board[y][x]) == 2) {
break;
}
x_direction++;
}
}
if (x == 7 && piece_color_to_int(board[y][x]) == 3) {
x = 0;
x_direction = -7;
tile_selection();
x_direction = 0;
for (; x < 7; x++) {
if (piece_color_to_int(board[y][x]) == 1 || piece_color_to_int(board[y][x]) == 2) {
break;
}
x_direction++;
}
}
break;
case 10: // enter
a();
break;
case 113: // q
return;
default: break;
}
}
}
int main() {
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("\x1b[H\x1b[J");
printf("\x1b[?25l");
print_tab();
controls();
printf("\x1b[10;0H\x1b[0m\x1b[?25h");
tcsetattr(STDIN_FILENO, TCSANOW, &t_old);
return 0;
}