checc/main.c

167 lines
4.5 KiB
C

#include <stdio.h>
#include <termios.h>
#include <unistd.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_136 "\x1b[30;48;5;136m"
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;
typedef enum {
WHITE,
BLACK
} States;
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_str(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;
}
}
int piece_color[8][8];
int print_tab(Pieces board[8][8]) {
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"FG_WHITE_BG_WHITE, y + 1 , x * 2 + 1, piece_to_str(board[y][x]));
}
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]));
}
}
if (y % 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]));
}
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("\x1b[39;49m");
return 0;
}
int a() {
int x = 0;
int y = 7;
int input = getchar();
while (1) {
switch (input) {
case 104: // h
if (x != 0 || piece_color[y][x] == 1 || piece_color[y][x] == 2)
x--;
break;
case 106: // j
break;
case 107: // k
break;
case 108: // l
if (x != 7 || piece_color[y][x] == 1 || piece_color[y][x] == 2)
x++;
break;
}
}
printf("%d", x);
return 0;
}
int main() {
const struct termios old_termios;
tcsetattr(STDIN_FILENO, TCSANOW, &old_termios);
setlocale(LC_ALL, "en_US.UTF-8");
printf("\n");
printf("\x1b[H");
printf("\x1b[2J");
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 x = 0; x < 8; ++x) {
piece_color[y][x] = piece_color_to_str(board[y][x]);
}
}
print_tab(board);
a();
getchar();
printf("\x1b[10;0H");
return 0;
}