fixes ncurses stuck display, swapped coordinates on network. handles comment
line arriving before player token line.
このコミットが含まれているのは:
ninya9k 2023-11-15 14:24:14 +00:00
コミット a57d6c2c50
1個のファイルの変更21行の追加8行の削除

29
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);
}