aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorboa <boa@pm.me>2021-01-31 18:23:04 +0100
committerboa <boa@pm.me>2021-01-31 18:23:04 +0100
commit983978ecd478ee13be2b9c423d7a3fd2f9457f24 (patch)
tree3a4073e21c69765ef7446d8b292afd28273021a0
parentdc3f15b4d3a48233c639c657464d3307c8d4b58a (diff)
downloadajedrez-983978ecd478ee13be2b9c423d7a3fd2f9457f24.tar.gz
some stuff I don't remember
-rwxr-xr-xchessbin22000 -> 22064 bytes
-rw-r--r--chess.c116
2 files changed, 97 insertions, 19 deletions
diff --git a/chess b/chess
index e42a533..1ba1e4d 100755
--- a/chess
+++ b/chess
Binary files differ
diff --git a/chess.c b/chess.c
index fdaadc3..0cd766d 100644
--- a/chess.c
+++ b/chess.c
@@ -1,4 +1,5 @@
#include <arpa/inet.h>
+#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
@@ -38,8 +39,9 @@ typedef struct piece {
piece table[8][8];
int w_fd = -1, b_fd = -1;
int *c_fd = &w_fd; // current fd
-bool w_moved_king = false, b_moved_king = false;
+bool moved_king[2] = {false, false};
+bool is_valid();
void print_table();
void setup_table();
void play_game();
@@ -92,6 +94,17 @@ void print_table() {
"\n\n \e[43m\x1B[30m\033[1m BLACK \x1B[0m\n\n\x1B[0m");
}
+bool in_check(color c, int cx, int cy) {
+ for (int y = 0; y < 8; y++) {
+ for (int x = 0; x < 8; x++) {
+ if (table[x][y].c != c && is_valid(x, y, cx, cy)) {
+ return true;
+ }
+ }
+ }
+ return false;
+}
+
void setup_table() {
table[0][0] = (piece){p_rook, c_w};
table[1][0] = (piece){p_knight, c_w};
@@ -118,7 +131,7 @@ void setup_table() {
int is_inside(int i) { return i >= 0 && i < 8; }
-int is_valid(int from_x, int from_y, int to_x, int to_y) {
+bool is_valid(int from_x, int from_y, int to_x, int to_y) {
// TODO: sáncolás
piece p = table[from_x][from_y];
int dx = to_x - from_x, dy = to_y - from_y;
@@ -136,29 +149,29 @@ int is_valid(int from_x, int from_y, int to_x, int to_y) {
(dy == -2 &&
(p.c != c_b || from_y != 6 || table[from_x][from_y - 1].p != p_no)) ||
(dx == 0 && table[to_x][to_y].p != p_no))
- return 0;
+ return false;
break;
case p_rook:
if (dx * dy != 0)
- return 0;
+ return false;
v_x = (dx != 0) ? ((dx > 0) ? 1 : -1) : 0;
v_y = (dy != 0) ? ((dy > 0) ? 1 : -1) : 0;
for (int n = 1; n < abs(dx + dy); n++)
if (table[from_x + n * v_x][from_y + n * v_y].p != p_no)
- return 0;
+ return false;
break;
case p_bishop:
if (abs(dx) != abs(dy))
- return 0;
+ return false;
v_x = dx ? ((dx > 0) ? 1 : -1) : 0;
v_y = dy ? ((dy > 0) ? 1 : -1) : 0;
for (int n = 1; n < abs(dx); n++)
if (table[from_x + n * v_x][from_y + n * v_y].p != p_no)
- return 0;
+ return false;
break;
case p_queen:
if (dx * dy != 0 && abs(dx) != abs(dy))
- return 0;
+ return false;
v_x = dx ? ((dx > 0) ? 1 : -1) : 0;
v_y = dy ? ((dy > 0) ? 1 : -1) : 0;
l = (dx * dy == 0) ? abs(dx + dy) : abs(dx);
@@ -167,32 +180,64 @@ int is_valid(int from_x, int from_y, int to_x, int to_y) {
return 0;
break;
case p_king:
- printf("%d %d\n", dx, dy);
- if (abs(dx) > 1 || abs(dy) > 1)
+ if (in_check(p.c, to_x, to_y))
return 0;
+ if (abs(dx) > 1 || abs(dy) > 1) {
+ if (!moved_king[p.c] && dy == 0) {
+ if (dx == -2 && table[from_x - 1][from_y].p == p_no &&
+ table[from_x - 3][from_y].p == p_rook &&
+ table[from_x - 3][from_y].c == p.c &&
+ !in_check(p.c, from_x - 1, from_y) &&
+ !in_check(p.c, from_x, from_y)) {
+ table[from_x - 1][from_y] = table[from_x - 3][from_y];
+ table[from_x - 3][from_y].p = p_no;
+ break;
+ } else if (dx == 3 && table[from_x + 1][from_y].p == p_no &&
+ table[from_x + 2][from_y].p == p_no &&
+ table[from_x + 4][from_y].p == p_rook &&
+ table[from_x + 4][from_y].c == p.c &&
+ !in_check(p.c, from_x + 1, from_y) &&
+ !in_check(p.c, from_x + 2, from_y) &&
+ !in_check(p.c, from_x, from_y)) {
+ table[from_x + 2][from_y] = table[from_x + 4][from_y];
+ table[from_x + 4][from_y].p = p_no;
+ break;
+ }
+ }
+ return false;
+ }
+ moved_king[p.c] = true;
break;
case p_knight:
if (!((abs(dx) == 2 && abs(dy) == 1) || (abs(dx) == 1 && abs(dy) == 2)))
- return 0;
+ return false;
break;
case p_no:
- return 0;
+ return false;
break;
}
- return 1;
+ return true;
}
void play_game() {
color c_c = c_w;
for (;;) {
print_table();
- dprintf(*c_fd, "> ");
if (c_c == c_w)
- dprintf(b_fd, "fehér gondolkodik...");
+ dprintf(b_fd, "fehér gondolkodik...\n");
else
- dprintf(w_fd, "fekete gondolkodik...");
- char buf[20];
+ dprintf(w_fd, "fekete gondolkodik...\n");
+ no_print:
+ dprintf(*c_fd, "> ");
+ char buf[50] = {0};
read(*c_fd, buf, 20);
+ if (buf[0] == ':') {
+ if (c_c == c_w)
+ dprintf(b_fd, buf);
+ else
+ dprintf(w_fd, buf);
+ goto no_print;
+ }
int from_x = buf[0] - 'a';
int from_y = buf[1] - '1';
int to_x = buf[3] - 'a';
@@ -203,8 +248,13 @@ void play_game() {
(table[to_x][to_y].c == c_c && table[to_x][to_y].p != p_no) ||
!is_valid(from_x, from_y, to_x, to_y)) {
dprintf(*c_fd, "hiba: %d %d %d %d\n", from_x, from_y, to_x, to_y);
- continue;
+ goto no_print;
} else {
+ printf("%s", buf);
+ if (c_c == c_w)
+ dprintf(b_fd, buf);
+ else
+ dprintf(w_fd, buf);
if (table[to_x][to_y].p == p_king) {
if (c_c == c_w)
PRINT_ALL("a fehér nyert!");
@@ -214,6 +264,35 @@ void play_game() {
}
table[to_x][to_y] = table[from_x][from_y];
table[from_x][from_y] = (piece){p_no, c_w};
+ if ((to_y == 0 || to_y == 7) && table[to_x][to_y].p == p_pawn) {
+ char cbuf[10] = {0};
+ retry:
+ dprintf(*c_fd, "mit kérsz?\n");
+ dprintf(*c_fd, "> ");
+ read(*c_fd, cbuf, 10);
+ switch (cbuf[0]) {
+ case 'P':
+ table[to_x][to_y].p = p_pawn;
+ break;
+ case 'R':
+ table[to_x][to_y].p = p_rook;
+ break;
+ case 'N':
+ table[to_x][to_y].p = p_knight;
+ break;
+ case 'B':
+ table[to_x][to_y].p = p_bishop;
+ break;
+ case 'Q':
+ table[to_x][to_y].p = p_queen;
+ break;
+ case 'K':
+ table[to_x][to_y].p = p_king;
+ break;
+ default:
+ goto retry;
+ }
+ }
}
if (c_fd == &w_fd) {
c_fd = &b_fd;
@@ -227,7 +306,6 @@ void play_game() {
int main() {
setup_table();
- print_table();
// stolen from: https://git.sr.ht/~martijnbraam/among-sus
fd_set rfds, afds;
uint16_t port = 1234;
Un proyecto texto-plano.xyz