readable iff game<->game session
このコミットが含まれているのは:
コミット
6fad23c17d
96
uttt.c
96
uttt.c
|
@ -454,30 +454,47 @@ int ut_cursgetpos(const struct ut_state *state, int *r, int *c)
|
|||
return true;
|
||||
}*/
|
||||
|
||||
int ut_sockgetpos(const struct ut_state *state, int sock, int *x, int *y) { // TODO
|
||||
const int MAX_LINE_LEN = 128;
|
||||
int ut_ignore_line(int sock) {
|
||||
char byte;
|
||||
for (int i = 0; i < 128; i++) {
|
||||
if (ut_readfill(sock, &byte, 1)) {
|
||||
return 1;
|
||||
}
|
||||
if (byte == '\n') {return 0;}
|
||||
}
|
||||
return 2;
|
||||
}
|
||||
|
||||
int ut_sockgetpos(const struct ut_state *state, int sock, int *x, int *y, bool readable) { // TODO
|
||||
int index = 0;
|
||||
char byte;
|
||||
int n;
|
||||
while (true) {
|
||||
char line[MAX_LINE_LEN];
|
||||
int len = ut_readline(sock, line, MAX_LINE_LEN);
|
||||
if (len < 0) {return len;}
|
||||
if (len == 0) {printf("sex\n");return -1;}
|
||||
line[len] = 0;
|
||||
printf("got >>>>>> line: '%s'\n", line);
|
||||
if (line[0] == '|') {continue;}
|
||||
if (len != 3) {return -3;}
|
||||
if (line[1] != ',') {return -3;}
|
||||
*x = line[0] - 0x30;
|
||||
*y = line[2] - 0x30;
|
||||
break;
|
||||
int r = ut_readfill(sock, &byte, 1);
|
||||
if (r < 0) {return r;}
|
||||
if (r == 0) {
|
||||
printf("Connection closed.\n");
|
||||
return -1;
|
||||
}
|
||||
if (index == 0) {
|
||||
*x = byte - 0x30;
|
||||
} else if (index == 2) {
|
||||
*y = byte - 0x30;
|
||||
} else if (index == 1 && byte != ',' || index == 3 && byte != '\n') {
|
||||
printf("Partner sent malformed coords - retrying\n");
|
||||
if (ut_ignore_line(sock)) {return -1;}
|
||||
if (readable) {
|
||||
ut_dprintf(sock, "Invalid coordinates. Try again: ");
|
||||
}
|
||||
index = 0;
|
||||
continue;
|
||||
}
|
||||
index++;
|
||||
if (index == 4) {break;}
|
||||
}
|
||||
printf("x=%d y=%d\n", *x, *y);
|
||||
return 0;
|
||||
|
||||
}
|
||||
|
||||
|
||||
int ut_local_game(struct ut_state *state) {
|
||||
int x, y;
|
||||
for(;;)
|
||||
|
@ -510,12 +527,12 @@ int ut_network_game(struct ut_state *state, int sock, char player, bool readable
|
|||
int x, y;
|
||||
//printf("You play as %c.\n", player);
|
||||
while (true) {
|
||||
ut_show(state, 1, false);
|
||||
ut_show_boards(state);
|
||||
ut_show(state, sock, true);
|
||||
//ut_show(state, 1, false);
|
||||
//ut_show_boards(state);
|
||||
//ut_show(state, sock, true);
|
||||
if (state->player == player) {
|
||||
if (readable) {ut_dprintf(sock, "Waiting for game partner ...\n");}
|
||||
bool ok = getpos(state, &x, &y);
|
||||
bool ok = ut_cursgetpos(state, &x, &y);
|
||||
if (!ok) {continue;}
|
||||
ut_dprintf(sock, "%d,%d\n", x, y);
|
||||
} else {
|
||||
|
@ -524,16 +541,10 @@ int ut_network_game(struct ut_state *state, int sock, char player, bool readable
|
|||
// line feed aligns game board each turn
|
||||
ut_dprintf(sock, "\nPlace token %c in position x,y: ", ut_turn(player));
|
||||
}
|
||||
int err = ut_sockgetpos(state, sock, &x, &y);
|
||||
int err = ut_sockgetpos(state, sock, &x, &y, readable);
|
||||
if (err == -1) {
|
||||
printf("Connection closed.\n");
|
||||
return 1;
|
||||
} else if (err == -2) {
|
||||
printf("Partner sent too much data - exiting\n");
|
||||
return 1;
|
||||
} else if (err == -3) {
|
||||
printf("Partner sent malformed data - retrying ...\n");
|
||||
continue;
|
||||
}
|
||||
}
|
||||
int err = ut_move(state, state, y, x);
|
||||
|
@ -603,9 +614,26 @@ int ut_host_game(struct ut_state *state) {
|
|||
// tell partner X or O
|
||||
ut_dprintf(conn, "%c\n", ut_turn(player));
|
||||
|
||||
// TODO check to determine readable
|
||||
// check to determine readable
|
||||
char byte;
|
||||
ut_dprintf(conn, "| Press enter to start: ");
|
||||
if (ut_readfill(conn, &byte, 1)) {
|
||||
printf("Connection closed.\n");
|
||||
return 1;
|
||||
}
|
||||
bool readable = byte != '\0';
|
||||
if (byte != '\n') {
|
||||
int err = ut_ignore_line(sock);
|
||||
if (err == 1) {
|
||||
printf("Connection closed.\n");
|
||||
return 1;
|
||||
} else if (err == 2) {
|
||||
printf("Partner sent too much data - exiting\n");
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
|
||||
return ut_network_game(state, conn, player);
|
||||
return ut_network_game(state, conn, player, readable);
|
||||
}
|
||||
|
||||
int ut_join_game(struct ut_state *state) {
|
||||
|
@ -626,17 +654,17 @@ int ut_join_game(struct ut_state *state) {
|
|||
return 1;
|
||||
}
|
||||
|
||||
// tell host to deactivate readable
|
||||
ut_dprintf(sock, "\0\n");
|
||||
|
||||
// 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;}
|
||||
state->host_player = ut_turn(*player);
|
||||
|
||||
// TODO send '\0' to deactivate readable
|
||||
|
||||
return ut_network_game(state, sock, *player);
|
||||
return ut_network_game(state, sock, *player, false);
|
||||
}
|
||||
|
||||
void finish(int sig)
|
||||
|
|
読み込み中…
新しいイシューから参照