aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorboa <boa@pm.me>2021-02-01 18:33:58 +0100
committerboa <boa@pm.me>2021-02-01 18:33:58 +0100
commit6a1977cdc695996fd4c2e1af3baa0757136fdf98 (patch)
tree04b33a423b7bf0efca705fd9a8c8631bd9e07d4d
parent17d017813400bd94cb14df96320745da1b6a041c (diff)
downloadajedrez-6a1977cdc695996fd4c2e1af3baa0757136fdf98.tar.gz
stalemate, refactor
-rwxr-xr-xchessbin37576 -> 37432 bytes
-rw-r--r--sakkom.c120
2 files changed, 50 insertions, 70 deletions
diff --git a/chess b/chess
index 57f0fbc..7d2f063 100755
--- a/chess
+++ b/chess
Binary files differ
diff --git a/sakkom.c b/sakkom.c
index 15ee325..4de8f77 100644
--- a/sakkom.c
+++ b/sakkom.c
@@ -1,8 +1,6 @@
// SAKKOM: sakk a konzolban király! online multiplayer
// chess.com alternatíva, ami netcatet használ mint kliens
//
-// TODO: stalemate
-//
// do what you want
// boa
@@ -201,69 +199,59 @@ void setup_table() {
table[x][6] = (piece){p_pawn, c_b};
}
-result is_checkmate(color c) {
- int kx = -1, ky = -1;
- 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 == c) {
- kx = x;
- ky = y;
- goto out;
- }
- }
- }
-out:
- printf("k: %d %d\n", kx, ky);
- if (kx == -1 || ky == -1)
- return r_no;
- if (!in_check(c, kx, ky)) {
- printf("k not in check\n");
- return r_no;
- }
- printf("k in check\n");
+pos find_king(color c) {
+ 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 == c)
+ return (pos){x, y};
+ return (pos){-1, -1};
+}
+
+bool is_stalemate(color c) {
+ pos kp = find_king(c);
+ if (in_check(c, kp.x, kp.y))
+ return false;
+
move m;
- m.f.x = kx;
- m.f.y = ky;
- m.p = table[kx][ky];
- for (int y = -1; y <= 1; y++) {
- for (int x = -1; x <= 1; x++) {
- if (x == 0 && y == 0)
- continue;
- m.t.x = kx + x;
- m.t.y = ky + y;
- m.e = table[kx + x][ky + y];
- if (is_move_legal(c, m) && is_inside(ky + y) && is_inside(kx + x) &&
- !in_check(c, kx + x, ky + y)) {
- return r_no;
+ for (m.f.y = 0; m.f.y < 8; m.f.y++) {
+ for (m.f.x = 0; m.f.x < 8; m.f.x++) {
+ if (table[m.f.x][m.f.y].c == c) {
+ m.p = table[m.f.x][m.f.y];
+ for (m.t.y = 0; m.t.y < 8; m.t.y++) {
+ for (m.t.x = 0; m.t.x < 8; m.t.x++) {
+ if (m.t.x == m.f.x && m.t.y == m.f.y)
+ continue;
+ m.e = table[m.t.x][m.t.y];
+ if (is_move_legal(c, m)) {
+ return false;
+ }
+ }
+ }
}
}
}
+ return true;
+}
+bool is_checkmate(color c) {
// 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++) {
- 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;
+ move m;
+ for (m.f.y = 0; m.f.y < 8; m.f.y++) {
+ for (m.f.x = 0; m.f.x < 8; m.f.x++) {
+ if (table[m.f.x][m.f.y].c == c) {
+ m.p = table[m.f.x][m.f.y];
+ for (m.t.y = 0; m.t.y < 8; m.t.y++) {
+ for (m.t.x = 0; m.t.x < 8; m.t.x++) {
+ m.e = table[m.t.x][m.t.y];
+ if (is_move_legal(c, m)) {
+ return false;
}
}
}
}
}
}
- return r_win;
+ return true;
}
void print_move(move m) {
@@ -286,22 +274,6 @@ void print_move(move m) {
dprintf(w_fd, buf);
}
-bool print_result(color c) {
- result r;
- if ((r = is_checkmate(c)) != r_no) {
- if (r == r_stalemate) {
- PRINT_ALL("\ndöntetlen\n");
- } else {
- if (c == c_w)
- PRINT_ALL("\na fekete nyert!\n");
- else
- PRINT_ALL("\na fehér nyert!\n");
- }
- return true;
- }
- return false;
-}
-
move parse_move(char buf[50]) {
move m;
@@ -451,7 +423,15 @@ void play_game() {
dprintf(w_fd, "fekete gondolkodik...\n");
no_print:
dprintf(*c_fd, "> ");
- if (print_result(c_c)) {
+
+ if (is_checkmate(c_c)) {
+ if (c_c == c_w)
+ PRINT_ALL("\na fekete nyert!\n");
+ else
+ PRINT_ALL("\na fehér nyert!\n");
+ break;
+ } else if (is_stalemate(c_c)) {
+ PRINT_ALL("\ndöntetlen\n");
break;
}
Un proyecto texto-plano.xyz