diff --git a/main.c b/main.c index f5a9194..757876b 100644 --- a/main.c +++ b/main.c @@ -24,12 +24,6 @@ typedef enum { WHITE_KING } Pieces; -typedef enum { - WHITE, - BLACK -} States; - - char* piece_to_str(const Pieces piece) { switch (piece) { case EMPTY: return "\u2800\u2800"; @@ -113,23 +107,7 @@ void print_tab() { } 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])); - } - 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])); - } - } + print_tab(); printf("\x1b[%d;%dH\x1b[39;106m%s", y + 1 , x * 2 + 1, piece_to_str(board[y][x])); } @@ -144,16 +122,11 @@ void a() { printf("\x1b[%d;%dH\x1b[91;101m\u2800\u2800", y - 1 + 1 , x * 2 - 2 + 1); } } - - - - } void controls() { - tile_selection(); - while (1) { + tile_selection(); x_direction = 0; y_direction = 0; @@ -163,103 +136,139 @@ void controls() { switch (input) { case 104: // h if (x > 0) { - for (int i = x; i > 0; --i) { - x--; + for (x--; x > 0; x--) { x_direction--; - if (piece_color[y][x] == 1 || piece_color[y][x] == 2) { + if (piece_color_to_int(board[y][x]) == 1 || piece_color_to_int(board[y][x]) == 2) { break; } } } - else { + else if (x == 0) { 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) { + 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) { + 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--; x_direction--; } } - tile_selection(); break; case 106: // j if (y < 7) { - for (int i = y; i < 7; ++i) { - y++; + for (y++; y < 7; y++) { y_direction++; - if (piece_color[y][x] == 1 || piece_color[y][x] == 2) { + if (piece_color_to_int(board[y][x]) == 1 || piece_color_to_int(board[y][x]) == 2) { break; } } } - else { + else if (y == 7) { 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) { + 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) { + 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++; y_direction++; } } - tile_selection(); break; case 107: // k if (y > 0) { - for (int i = y; i > 0; --i) { - y--; + for (y--; y > 0; y--) { y_direction--; - if (piece_color[y][x] == 1 || piece_color[y][x] == 2) { + if (piece_color_to_int(board[y][x]) == 1 || piece_color_to_int(board[y][x]) == 2) { break; } } } - else { + else if (y == 0) { 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) { + 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--; y_direction--; } } - tile_selection(); break; case 108: // l if (x < 7) { - for (int i = x; i < 7; ++i) { - x++; + for (x++; x < 7; x++) { x_direction++; - if (piece_color[y][x] == 1 || piece_color[y][x] == 2) { + if (piece_color_to_int(board[y][x]) == 1 || piece_color_to_int(board[y][x]) == 2) { break; } } } - else { + else if (x == 7) { 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) { + 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++; x_direction++; } } - tile_selection(); break; @@ -271,7 +280,6 @@ void controls() { exit(0); default: break; } - fflush(stdout); } } @@ -294,13 +302,18 @@ int main() { } } - print_tab(board); + print_tab(); + printf("\x1b[?1049h"); + + + //printf("\x1b[?47l"); + + //printf("\x1b[0;0Haaaa"); controls(); - printf("\x1b[0m"); - printf("\x1b[10;0H"); + printf("\x1b[10;0H\x1b[0m\x1b[?25l"); tcsetattr(STDIN_FILENO, TCSANOW, &t_old); return 0; }