sequence points are real and indirection is bad

このコミットが含まれているのは:
woosh 2023-11-15 06:15:26 +00:00
コミット d50db74ddb
1個のファイルの変更16行の追加17行の削除

33
uttt.c
ファイルの表示

@ -31,7 +31,7 @@ const struct ut_state ut_initial = {
struct ut_socket_buffer {
int fd;
char *buf;
char buf[128];
size_t len;
size_t start;
size_t stop;
@ -408,7 +408,7 @@ int ut_cursgetpos(const struct ut_state *state, int *r, int *c)
#define P(r, c, d) ((state->playBoard == -1 || state->playBoard == 3 * (r / 3) + (c / 3)) && \
(state->boards[r / 3][c / 3] == '\0' || d % 3 == 1))
MEVENT event;
mvaddstr(21, 0, "Select move with arrow keys or mouse.");
ut_cursprintf("Select move with arrow keys or mouse.\n");
if(state->playBoard == -1)
{
*r = 3 * (*r / 3) + 1;
@ -526,7 +526,7 @@ int ut_cursgetpos(const struct ut_state *state, int *r, int *c)
return true;
}*/
int getpos_net(const struct ut_state *state, struct ut_socket_buffer *sockbuf, int *x, int *y) { // TODO
int ut_sockgetpos(const struct ut_state *state, struct ut_socket_buffer *sockbuf, int *x, int *y) { // TODO
const int MAX_LINE_LEN = 128;
char byte;
int n;
@ -600,7 +600,7 @@ int ut_network_game(struct ut_state *state, struct ut_socket_buffer *sockbuf, ch
ut_dprintf(sockbuf->fd, "|\n"); // aligns game board each turn
ut_dprintf(sockbuf->fd, "| Place token %c in position x,y: ", ut_turn(player));
}
int err = getpos_net(state, sockbuf, &x, &y);
int err = ut_sockgetpos(state, sockbuf, &x, &y);
if (err == -1) {
printf("Connection closed.\n");
return 1;
@ -648,8 +648,7 @@ char ut_random_player(void) {
int ut_host_game(struct ut_state *state) {
int sock = socket(AF_INET, SOCK_STREAM, 0);
if (sock == -1) {
int errsv = errno;
printf("error %d\n", errsv);
printf("error %d\n", errno);
return 1;
}
const struct sockaddr_in addr = {
@ -659,20 +658,17 @@ int ut_host_game(struct ut_state *state) {
};
setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &(int){1}, sizeof(int));
if (bind(sock, (struct sockaddr*)&addr, sizeof(addr)) == -1) {
int errsv = errno;
printf("error %d\n", errsv);
printf("error %d\n", errno);
return 1;
}
if (listen(sock, 128) == -1) {
int errsv = errno;
printf("error %d\n", errsv);
printf("error %d\n", errno);
return 1;
}
printf("Waiting for game partner at 127.0.0.1:6669 ...\n");
int conn = accept(sock, NULL, NULL);
if (conn == -1) {
int errsv = errno;
printf("error %d\n", errsv);
printf("error %d\n", errno);
return 1;
}
@ -684,7 +680,7 @@ int ut_host_game(struct ut_state *state) {
struct ut_socket_buffer connbuf = {
.fd = conn,
.buf = (char[128]){0},
.buf = {0},
.len = 128,
.start = 0,
.stop = 0,
@ -695,8 +691,7 @@ int ut_host_game(struct ut_state *state) {
int ut_join_game(struct ut_state *state) {
int sock = socket(AF_INET, SOCK_STREAM, 0);
if (sock == -1) {
int errsv = errno;
printf("error %d\n", errsv);
printf("error %d\n", errno);
return 1;
}
const struct sockaddr_in addr = {
@ -705,7 +700,11 @@ int ut_join_game(struct ut_state *state) {
.sin_addr = { .s_addr = htonl(0x7f000001) },
};
printf("Connecting to game host at 127.0.0.1:6669 ...\n");
connect(sock, (struct sockaddr*)&addr, sizeof(addr));
if(connect(sock, (struct sockaddr*)&addr, sizeof(addr)) == -1)
{
printf("error %d\n", errno);
return 1;
}
// host decides X or O
char player_buf[2];
@ -717,7 +716,7 @@ int ut_join_game(struct ut_state *state) {
struct ut_socket_buffer sockbuf = {
.fd = sock,
.buf = (char[128]){0},
.buf = {0},
.len = 128,
.start = 0,
.stop = 0,