aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorboa <boa@pm.me>2021-02-01 16:56:29 +0100
committerboa <boa@pm.me>2021-02-01 16:56:29 +0100
commit17d017813400bd94cb14df96320745da1b6a041c (patch)
treeacf67ddd5d02be75ba5b3cf539a9d5c9ca3a6b97
parentf4adaa2c550382ae2149078ccfe70d4941fee6c0 (diff)
downloadajedrez-17d017813400bd94cb14df96320745da1b6a041c.tar.gz
refactoring, debugging
-rwxr-xr-xchessbin37712 -> 37576 bytes
-rw-r--r--sakkom.c72
2 files changed, 35 insertions, 37 deletions
diff --git a/chess b/chess
index 243e72d..57f0fbc 100755
--- a/chess
+++ b/chess
Binary files differ
diff --git a/sakkom.c b/sakkom.c
index 3e8e8c3..15ee325 100644
--- a/sakkom.c
+++ b/sakkom.c
@@ -1,7 +1,7 @@
// SAKKOM: sakk a konzolban király! online multiplayer
// chess.com alternatíva, ami netcatet használ mint kliens
//
-// TODO: refactoring, debugging and stalemate
+// TODO: stalemate
//
// do what you want
// boa
@@ -140,7 +140,6 @@ void print_table() {
}
bool in_check(color c, int cx, int cy) {
- printf("c: %d, %d, %d\n", c, cx, cy);
move m;
m.t.x = cx;
m.t.y = cy;
@@ -167,7 +166,7 @@ bool in_check_sim(move m) {
for (int y = 0; y < 8; y++) {
for (int x = 0; x < 8; x++) {
if (table[x][y].p == p_king && table[x][y].c == m.p.c) {
- res = in_check(table[x][y].c, x, y);
+ res = in_check(m.p.c, x, y);
goto out;
}
}
@@ -187,19 +186,19 @@ void setup_table() {
table[5][0] = (piece){p_bishop, c_w};
table[6][0] = (piece){p_knight, c_w};
table[7][0] = (piece){p_rook, c_w};
- // for (int x = 0; x < 8; x++)
- // table[x][1] = (piece){p_pawn, c_w};
+ for (int x = 0; x < 8; x++)
+ table[x][1] = (piece){p_pawn, c_w};
- // table[0][7] = (piece){p_rook, c_b};
- // table[1][7] = (piece){p_knight, c_b};
- // table[2][7] = (piece){p_bishop, c_b};
+ table[0][7] = (piece){p_rook, c_b};
+ table[1][7] = (piece){p_knight, c_b};
+ table[2][7] = (piece){p_bishop, c_b};
table[3][7] = (piece){p_king, c_b};
- // table[4][7] = (piece){p_queen, c_b};
+ table[4][7] = (piece){p_queen, c_b};
table[5][7] = (piece){p_bishop, c_b};
- // table[6][7] = (piece){p_knight, c_b};
- // table[7][7] = (piece){p_rook, c_b};
- // for (int x = 0; x < 8; x++)
- // table[x][6] = (piece){p_pawn, c_b};
+ table[6][7] = (piece){p_knight, c_b};
+ table[7][7] = (piece){p_rook, c_b};
+ for (int x = 0; x < 8; x++)
+ table[x][6] = (piece){p_pawn, c_b};
}
result is_checkmate(color c) {
@@ -239,31 +238,28 @@ out:
}
}
}
- color ic = c == c_w ? c_b : c_w;
- m.t.x = kx;
- m.t.y = ky;
- m.e = table[kx][ky];
+
+ // probably the most simple and least efficient way to solve this problem
for (int y = 0; y < 8; y++) {
for (int x = 0; x < 8; x++) {
- m.f.x = x;
- m.f.y = y;
- m.p = table[x][y];
- if (table[x][y].c != c && is_move_legal(c, m)) {
- int dx = kx - x, dy = ky - y;
- if (table[x][y].p == p_rook || table[x][y].p == p_queen ||
- table[x][y].p == p_bishop) {
- int v_x = dx ? ((dx > 0) ? 1 : -1) : 0;
- int v_y = dy ? ((dy > 0) ? 1 : -1) : 0;
- bool possible = false;
- for (int n = 0; n < abs(dx); n++)
- if (table[x + n * v_x][y + n * v_y].p == p_no)
- if (in_check(ic, x + n * v_x, y + n * v_y))
- possible = true;
- if (!possible) {
- return r_win;
+ if (table[x][y].c == c) {
+ m.f.x = x;
+ m.f.y = y;
+ m.p = table[x][y];
+ for (int ty = 0; ty < 8; ty++) {
+ for (int tx = 0; tx < 8; tx++) {
+ if (tx == x && ty == y)
+ continue;
+ if (tx == kx && ty == ky)
+ continue;
+ m.t.x = tx;
+ m.t.y = ty;
+ m.e = table[tx][ty];
+ if (is_move_legal(c, m) && !in_check_sim(m)) {
+ return r_no;
+ }
}
}
- return r_no;
}
}
}
@@ -275,12 +271,12 @@ void print_move(move m) {
int n = 0;
buf[n++] = table[m.f.x][m.f.y].p;
buf[n++] = m.f.x + 'a';
- buf[n++] = m.f.y + '0';
+ buf[n++] = m.f.y + '1';
buf[n++] = ' ';
if (table[m.t.x][m.t.y].p != p_no)
buf[n++] = table[m.t.x][m.t.y].p;
buf[n++] = m.t.x + 'a';
- buf[n++] = m.t.y + '0';
+ buf[n++] = m.t.y + '1';
buf[n++] = '\n';
buf[n++] = '\0';
printf("%s", buf);
@@ -376,9 +372,11 @@ bool is_move_legal(color c, move m) {
return false;
break;
case p_king:
+ if (abs(dx) > 3 || abs(dy) > 1)
+ return 0;
if (in_check(c, m.t.x, m.t.y))
return 0;
- if (abs(dx) > 1 || abs(dy) > 1) {
+ 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 &&
Un proyecto texto-plano.xyz