aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorboa <boa@pm.me>2021-01-31 03:20:21 +0100
committerboa <boa@pm.me>2021-01-31 03:20:21 +0100
commit00d49540a61221a1da5dc58b7c782c22c88e8072 (patch)
tree242299df44e005d592314cf950cc8f29ee39ac68
parent7693bafd5e16ce14e89d53fb04948de98f13c0c8 (diff)
downloadajedrez-00d49540a61221a1da5dc58b7c782c22c88e8072.tar.gz
validation for some pieces
-rwxr-xr-xchessbin16800 -> 21968 bytes
-rw-r--r--chess.c271
2 files changed, 236 insertions, 35 deletions
diff --git a/chess b/chess
index 1f01f4a..3be73f9 100755
--- a/chess
+++ b/chess
Binary files differ
diff --git a/chess.c b/chess.c
index 3442c86..5f15fb6 100644
--- a/chess.c
+++ b/chess.c
@@ -11,7 +11,7 @@
dprintf(b_fd, (f_), ##__VA_ARGS__); \
} while (0)
-typedef enum color { c_w = 34, c_b = 31 } color;
+typedef enum color { c_w = 31, c_b = 34 } color;
typedef enum piece_type {
p_no,
@@ -29,61 +29,262 @@ typedef struct piece {
} piece;
piece table[8][8];
+int w_fd = -1, b_fd = -1;
+int *c_fd = &w_fd; // current fd
void print_table();
+void setup_table();
+void play_game();
void print_table() {
- printf(" BLACK\n |");
+ PRINT_ALL(" \x1B[31mWHITE\x1B[0m\n |");
for (int x = 0; x < 8; x++)
- printf("%c", 'a' + x);
- printf("|\n-+");
+ PRINT_ALL("%c", 'a' + x);
+ PRINT_ALL("|\n-+");
for (int x = 0; x < 8; x++)
- printf("-");
- printf("+-\n");
+ PRINT_ALL("-");
+ PRINT_ALL("+-\n");
for (int y = 0; y < 8; y++) {
- printf("%d|", y + 1);
+ PRINT_ALL("%d|", y + 1);
for (int x = 0; x < 8; x++)
if (table[x][y].p)
- printf("\x1B[%dm%c\x1B[0m", table[x][y].c, table[x][y].p);
+ PRINT_ALL("\x1B[%dm%c\x1B[0m", table[x][y].c, table[x][y].p);
else
- printf(".");
- printf("|%d\n", y + 1);
+ PRINT_ALL(".");
+ PRINT_ALL("|%d\n", y + 1);
}
- printf("-+");
+ PRINT_ALL("-+");
for (int x = 0; x < 8; x++)
- printf("-");
- printf("+-\n |");
+ PRINT_ALL("-");
+ PRINT_ALL("+-\n |");
for (int x = 0; x < 8; x++)
- printf("%c", 'a' + x);
- printf("|\n WHITE\n");
+ PRINT_ALL("%c", 'a' + x);
+ PRINT_ALL("|\n \x1B[34mBLACK\n\x1B[0m");
}
void setup_table() {
- table[0][0] = (piece){p_rook, c_b};
- table[1][0] = (piece){p_knight, c_b};
- table[2][0] = (piece){p_bishop, c_b};
- table[3][0] = (piece){p_king, c_b};
- table[4][0] = (piece){p_queen, c_b};
- table[5][0] = (piece){p_bishop, c_b};
- table[6][0] = (piece){p_knight, c_b};
- table[7][0] = (piece){p_rook, c_b};
+ table[0][0] = (piece){p_rook, c_w};
+ table[1][0] = (piece){p_knight, c_w};
+ table[2][0] = (piece){p_bishop, c_w};
+ table[3][0] = (piece){p_king, c_w};
+ table[4][0] = (piece){p_queen, c_w};
+ 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_b};
-
- table[0][7] = (piece){p_rook, c_w};
- table[1][7] = (piece){p_knight, c_w};
- table[2][7] = (piece){p_bishop, c_w};
- table[3][7] = (piece){p_king, c_w};
- table[4][7] = (piece){p_queen, c_w};
- table[5][7] = (piece){p_bishop, c_w};
- table[6][7] = (piece){p_knight, c_w};
- table[7][7] = (piece){p_rook, c_w};
+ 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[3][7] = (piece){p_king, 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_w};
+ table[x][6] = (piece){p_pawn, c_b};
+}
+
+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) {
+ // TODO: sáncolás
+ piece p = table[from_x][from_y];
+ int dx = to_x - from_x, dy = to_y - from_y;
+ int v_x, v_y;
+ switch (p.p) {
+ case p_pawn:
+ // TODO: refactor
+ if ((dx != 0 && (dx != 1 || dy != 1 || table[to_x][to_y].p == p_no)) ||
+ dy < -2 || dy > 2 || (dy == 1 && p.c != c_w) ||
+ (dy == 2 &&
+ (p.c != c_w || from_y != 1 || table[from_x][from_y + 1].p != p_no)) ||
+ (dy == -1 && p.c != c_b) ||
+ (dy == -2 &&
+ (p.c != c_b || from_y != 6 || table[from_x][from_y - 1].p != p_no)))
+ return 0;
+ break;
+ case p_rook:
+ if (dx * dy != 0)
+ return 0;
+ 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;
+ break;
+ case p_bishop:
+ if (abs(dx) != abs(dy))
+ return 0;
+ 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;
+ break;
+ case p_queen:
+ if (dx * dy != 0 && abs(dx) != abs(dy))
+ return 0;
+ 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;
+ break;
+ case p_king:
+ if (abs(dx) > 1 || abs(dy) > 1)
+ return 0;
+ break;
+ case p_knight:
+ if (!((abs(dx) == 2 && abs(dy) == 1) || (abs(dx) == 1 && abs(dy) == 2)))
+ return 0;
+ break;
+ case p_no:
+ return 0;
+ break;
+ }
+ return 1;
+}
+
+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...");
+ else
+ dprintf(w_fd, "fekete gondolkodik...");
+ char buf[20];
+ read(*c_fd, buf, 20);
+ int from_x = buf[0] - 'a';
+ int from_y = buf[1] - '1';
+ int to_x = buf[3] - 'a';
+ int to_y = buf[4] - '1';
+ if (!is_inside(from_x) || !is_inside(from_y) || !is_inside(to_x) ||
+ !is_inside(to_y) || table[from_x][from_y].p == p_no ||
+ table[from_x][from_y].c != c_c || table[to_x][to_y].c == c_c ||
+ !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;
+ } else {
+ table[to_x][to_y] = table[from_x][from_y];
+ table[from_x][from_y] = (piece){p_no, c_w};
+ }
+ if (c_fd == &w_fd) {
+ c_fd = &b_fd;
+ c_c = c_b;
+ } else {
+ c_fd = &w_fd;
+ c_c = c_w;
+ }
+ }
}
int main() {
setup_table();
- print_table();
+ // stolen from: https://git.sr.ht/~martijnbraam/among-sus
+ fd_set rfds, afds;
+ uint16_t port = 1234;
+ int listen_fd, listen6_fd, new_fd, i;
+ socklen_t client_size;
+ struct sockaddr_in listen_addr = {0}, client_addr = {0};
+ struct sockaddr_in6 listen6_addr = {0};
+
+ if ((listen_fd = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
+ perror("IPv4 socket");
+ exit(EXIT_FAILURE);
+ }
+ if ((listen6_fd = socket(AF_INET6, SOCK_STREAM, 0)) == -1) {
+ perror("IPv6 socket");
+ exit(EXIT_FAILURE);
+ }
+
+ i = 1;
+ if (setsockopt(listen_fd, SOL_SOCKET, SO_REUSEADDR, &i, sizeof(i))) {
+ perror("IPv4 setsockopt");
+ exit(EXIT_FAILURE);
+ }
+
+ if (setsockopt(listen6_fd, SOL_SOCKET, SO_REUSEADDR, &i, sizeof(i))) {
+ perror("IPv6 setsockopt");
+ exit(EXIT_FAILURE);
+ }
+
+ listen_addr.sin_family = AF_INET;
+ listen_addr.sin_addr.s_addr = htonl(INADDR_ANY);
+ listen_addr.sin_port = htons(port);
+
+ listen6_addr.sin6_family = AF_INET6;
+ listen6_addr.sin6_addr = in6addr_any;
+ listen6_addr.sin6_port = htons(port);
+ if (setsockopt(listen6_fd, IPPROTO_IPV6, IPV6_V6ONLY, &i, sizeof(i))) {
+ perror("setsockopt");
+ exit(EXIT_FAILURE);
+ }
+ if (setsockopt(listen6_fd, IPPROTO_IPV6, IPV6_V6ONLY, &i, sizeof(i))) {
+ perror("setsockopt");
+ exit(EXIT_FAILURE);
+ }
+
+ if (bind(listen_fd, (struct sockaddr *)&listen_addr, sizeof(listen_addr)) <
+ 0) {
+ perror("ipv4 bind");
+ return -1;
+ }
+ if (bind(listen6_fd, (struct sockaddr *)&listen6_addr, sizeof(listen6_addr)) <
+ 0) {
+ perror("ipv6 bind");
+ return -1;
+ }
+
+ listen(listen_fd, 5);
+ listen(listen6_fd, 5);
+ printf("Listening on :%d\n", port);
+
+ FD_ZERO(&afds);
+ FD_SET(listen_fd, &afds);
+ FD_SET(listen6_fd, &afds);
+
+ while (1) {
+ rfds = afds;
+ if (select(FD_SETSIZE, &rfds, NULL, NULL, NULL) < 0) {
+ perror("select");
+ exit(EXIT_FAILURE);
+ }
+
+ for (i = 0; i < FD_SETSIZE; ++i) {
+ if (FD_ISSET(i, &rfds)) {
+ if (i == listen_fd || i == listen6_fd) {
+ client_size = sizeof(client_addr);
+ new_fd = accept(i, (struct sockaddr *)&client_addr, &client_size);
+ if (new_fd < 0) {
+ perror("accept");
+ exit(EXIT_FAILURE);
+ }
+
+ printf("New connection from %s:%d\n", inet_ntoa(client_addr.sin_addr),
+ ntohs(client_addr.sin_port));
+ FD_SET(new_fd, &afds);
+ if (w_fd == -1) {
+ dprintf(new_fd, "te vagy az fehérrel\n");
+ w_fd = new_fd;
+ } else if (b_fd == -1) {
+ dprintf(new_fd, "te vagy a feketével\n");
+ dprintf(w_fd, "belépett a fekete is, kezdhetjük!\n");
+ b_fd = new_fd;
+ play_game();
+ return 0;
+ } else {
+ // write(new_fd, "nincs hely!\n", strlen("nincs hely!\n"));
+ }
+ } else {
+ close(i);
+ FD_CLR(i, &afds);
+ }
+ }
+ }
+ }
return 0;
}
Un proyecto texto-plano.xyz