From d52937530d951a9c9a7c5fe00057837c7d9ce131 Mon Sep 17 00:00:00 2001 From: boa Date: Wed, 3 Feb 2021 12:36:09 +0100 Subject: fix castle --- sakkom | Bin 27128 -> 27112 bytes sakkom.c | 72 +++++++++++++++++++++++++++++++++++++++++++++++++-------------- 2 files changed, 56 insertions(+), 16 deletions(-) diff --git a/sakkom b/sakkom index 9fba27f..2c34001 100755 Binary files a/sakkom and b/sakkom differ diff --git a/sakkom.c b/sakkom.c index 47387af..e470efc 100644 --- a/sakkom.c +++ b/sakkom.c @@ -23,6 +23,8 @@ typedef enum color { c_w = 0, c_b = 1 } color; +typedef enum castle { castle_no, castle_long, castle_short } castle; + int colors[] = {39, 30}; int abs(int a) { @@ -98,10 +100,12 @@ bool is_move_inside(move m) { void print_table() { dprintf(w_fd, "\n \e[43m\x1B[30m\033[1m BLACK \x1B[0m\n\n "); dprintf(b_fd, "\n \e[43m\x1B[39m\033[1m WHITE \x1B[0m\n\n "); + for (int x = 0; x < 8; x++) { dprintf(w_fd, " %c ", 'A' + x); dprintf(b_fd, " %c ", 'A' + 7 - x); } + PRINT_ALL("\n \e[43m+-------------------------------+\x1B[0m\n"); for (int y = 7; y >= 0; y--) { if (y != 7) { @@ -117,6 +121,7 @@ void print_table() { dprintf(w_fd, " |"); dprintf(w_fd, "\x1B[0m %d\x1B[0m\n", y + 1); } + for (int y = 0; y < 8; y++) { if (y != 0) { dprintf(b_fd, "\x1B[0m " @@ -131,7 +136,9 @@ void print_table() { dprintf(b_fd, " |"); dprintf(b_fd, "\x1B[0m %d\n", y + 1); } + PRINT_ALL(" \e[43m+-------------------------------+\x1B[0m\n "); + for (int x = 0; x < 8; x++) { dprintf(b_fd, " %c ", 'A' + 7 - x); dprintf(w_fd, " %c ", 'A' + x); @@ -156,7 +163,6 @@ bool in_check(color c, int cx, int cy) { m.f.y = y; m.p = table[x][y]; if (table[x][y].c != c && is_move_legal(m.p.c, m)) { - printf("true check\n"); return true; } } @@ -294,6 +300,29 @@ move parse_move(char buf[50]) { return m; } +castle is_castle(color c, move m) { + int dx = m.t.x - m.f.x, dy = m.t.y - m.f.y; + if (m.p.p == p_king) { + if (!moved_king[c] && dy == 0) { + if (!moved_rook_l[c] && dx == -2 && table[m.f.x - 1][m.f.y].p == p_no && + table[m.f.x - 3][m.f.y].p == p_rook && + table[m.f.x - 3][m.f.y].c == c && !in_check(c, m.f.x - 1, m.f.y) && + !in_check(c, m.f.x, m.f.y)) { + return castle_short; + } else if (!moved_rook_r[c] && dx == 3 && + table[m.f.x + 1][m.f.y].p == p_no && + table[m.f.x + 2][m.f.y].p == p_no && + table[m.f.x + 4][m.f.y].p == p_rook && + table[m.f.x + 4][m.f.y].c == c && + !in_check(c, m.f.x + 1, m.f.y) && + !in_check(c, m.f.x + 2, m.f.y) && !in_check(c, m.f.x, m.f.y)) { + return castle_long; + } + } + } + return castle_no; +} + bool is_move_legal(color c, move m) { if (!is_move_inside(m) || is_empty(m.p) || is_enemy_of(c, m.p) || is_friend_of(c, m.e)) @@ -301,8 +330,6 @@ bool is_move_legal(color c, move m) { int dx = m.t.x - m.f.x, dy = m.t.y - m.f.y; int v_x, v_y, l; - if (in_check_sim(m)) - return false; switch (m.p.p) { case p_pawn: if ((dx != 0 && ((dx != 1 && dx != -1) || (dy != 1 && c == c_w) || @@ -325,10 +352,6 @@ bool is_move_legal(color c, move m) { for (int n = 1; n < abs(dx + dy); n++) if (!is_empty(table[m.f.x + n * v_x][m.f.y + n * v_y])) return false; - if ((m.f.x == 7 && m.f.y == 0) || (m.f.x == 7 && m.f.y == 7)) - moved_rook_r[c] = true; - if ((m.f.x == 0 && m.f.y == 0) || (m.f.x == 0 && m.f.y == 7)) - moved_rook_l[c] = true; break; case p_bishop: if (abs(dx) != abs(dy)) @@ -350,19 +373,18 @@ bool is_move_legal(color c, move m) { return false; break; case p_king: + printf("dx: %d dy: %d\n", dx, dy); if (abs(dx) > 3 || abs(dy) > 1) - return 0; + return false; if (in_check(c, m.t.x, m.t.y)) - return 0; + return false; if (abs(dx) > 1) { if (!moved_king[c] && dy == 0) { if (!moved_rook_l[c] && dx == -2 && table[m.f.x - 1][m.f.y].p == p_no && table[m.f.x - 3][m.f.y].p == p_rook && table[m.f.x - 3][m.f.y].c == c && !in_check(c, m.f.x - 1, m.f.y) && !in_check(c, m.f.x, m.f.y)) { - table[m.f.x - 1][m.f.y] = table[m.f.x - 3][m.f.y]; - table[m.f.x - 3][m.f.y].p = p_no; - break; + return true; } else if (!moved_rook_r[c] && dx == 3 && table[m.f.x + 1][m.f.y].p == p_no && table[m.f.x + 2][m.f.y].p == p_no && @@ -371,14 +393,11 @@ bool is_move_legal(color c, move m) { !in_check(c, m.f.x + 1, m.f.y) && !in_check(c, m.f.x + 2, m.f.y) && !in_check(c, m.f.x, m.f.y)) { - table[m.f.x + 2][m.f.y] = table[m.f.x + 4][m.f.y]; - table[m.f.x + 4][m.f.y].p = p_no; - break; + return true; } } return false; } - moved_king[c] = true; break; case p_knight: if (!((abs(dx) == 2 && abs(dy) == 1) || (abs(dx) == 1 && abs(dy) == 2))) @@ -388,6 +407,8 @@ bool is_move_legal(color c, move m) { return false; break; } + if (in_check_sim(m)) + return false; return true; } @@ -488,6 +509,24 @@ void play_game() { print_move(m); do_move(m); + castle ct = is_castle(c_c, m); + if (ct == castle_short) { + table[m.f.x - 1][m.f.y] = table[m.f.x - 3][m.f.y]; + table[m.f.x - 3][m.f.y].p = p_no; + } else if (ct == castle_long) { + table[m.f.x + 2][m.f.y] = table[m.f.x + 4][m.f.y]; + table[m.f.x + 4][m.f.y].p = p_no; + } + + if (m.p.p == p_rook && + ((m.f.x == 7 && m.f.y == 0) || (m.f.x == 7 && m.f.y == 7))) + moved_rook_r[c_c] = true; + else if (m.p.p == p_rook && + ((m.f.x == 0 && m.f.y == 0) || (m.f.x == 0 && m.f.y == 7))) + moved_rook_l[c_c] = true; + else if (m.p.p == p_king) + moved_king[c_c] = true; + // en passant if (enp.valid && is_same_pos(enp.p, m.t) && m.p.p == p_pawn) enp.to_remove->p = p_no; @@ -507,6 +546,7 @@ void play_game() { dprintf(*c_fd, "hiba\n"); goto no_print; } + skip: if (c_fd == &w_fd) { c_fd = &b_fd; -- cgit v1.2.3