From a57d6c2c50d1a0cea6e5a3ca6dccb519313ed8e5 Mon Sep 17 00:00:00 2001 From: ninya9k <> Date: Wed, 15 Nov 2023 14:24:14 +0000 Subject: [PATCH] werks. fixes ncurses stuck display, swapped coordinates on network. handles comment line arriving before player token line. --- uttt.c | 29 +++++++++++++++++++++-------- 1 file changed, 21 insertions(+), 8 deletions(-) diff --git a/uttt.c b/uttt.c index d4d7e22..8dd30b0 100644 --- a/uttt.c +++ b/uttt.c @@ -304,6 +304,7 @@ int ut_readfill(int fd, char *x, size_t l) int r = read(fd, x, l); if(r < 0) { + //printf("ut_readfill read:%d errno=%d\n", r, errno); if(errno == EAGAIN || errno == EWOULDBLOCK || errno == EINTR) {continue;} else {return 1;} } @@ -531,9 +532,10 @@ int ut_network_game(struct ut_state *state, int sock, char player, bool readable if (state->player == player) { if (readable) {ut_dprintf(sock, "Waiting for game partner ...\n");} if(ut_cursgetpos(state, &r, &c)) {continue;} - ut_dprintf(sock, "%d,%d\n", r, c); + ut_dprintf(sock, "%d,%d\n", c, r); } else { ut_cursprintf("Waiting for game partner ...\n"); + refresh(); if (readable) { // line feed aligns game board each turn ut_dprintf(sock, "\nPlace token %c in position x,y: ", ut_turn(player)); @@ -549,8 +551,8 @@ int ut_network_game(struct ut_state *state, int sock, char player, bool readable return 1; } } - if(ut_move(state, state, r, c)) {continue;} - ut_dprintf(sock, "\n"); + if (ut_move(state, state, r, c)) {continue;} + if (readable) {ut_dprintf(sock, "\n");} } return 0; } @@ -604,7 +606,7 @@ int ut_host_game(struct ut_state *state) { } bool readable = byte != '\0'; if (byte != '\n') { - int err = ut_ignore_line(sock); + int err = ut_ignore_line(conn); if (err == 1) { printf("Connection closed.\n"); return 1; @@ -646,10 +648,21 @@ int ut_join_game(struct ut_state *state) { // host decides X or O char player_buf[2]; - char *player = (char*)&player_buf; - int r = ut_readfill(sock, player_buf, 2); - if (r == 2) {printf("Connection closed.\n");} - if (r != 0) {return 1;} + char *player = player_buf; + for (int i = 0; i < 10; i++) { + int r = ut_readfill(sock, player_buf, 1); + ut_ignore_line(sock); + if (*player == '|') { + continue; + } + if (r == 2) {printf("Connection closed.\n");} + if (r != 0) {return 1;} + break; + } + if (*player != 'X' && *player != 'O') { + printf("Host is retarded.\n"); + return 1; + } return ut_network_game(state, sock, *player, false); }