このコミットが含まれているのは:
woosh 2023-11-13 14:09:40 +00:00
コミット fce493c0ca
1個のファイルの変更43行の追加29行の削除

72
uttt.c
ファイルの表示

@ -247,7 +247,7 @@ void ut_info(const char *str, int n)
info_line = y + 1;
}
int readfill(int fd, char *x, size_t l)
int ut_readfill(int fd, char *x, size_t l)
{
while(l > 0)
{
@ -262,6 +262,20 @@ int readfill(int fd, char *x, size_t l)
}
return 0;
}
int ut_writefill(int fd, const char *x, size_t l)
{
while(l > 0)
{
int r = write(fd, x, l);
if(r < 0)
{
if(errno == EAGAIN || errno == EWOULDBLOCK || errno == EINTR) {continue;}
else {return 1;}
}
x += r; l -= r;
}
return 0;
}
#define MAX_LINE 128
@ -273,7 +287,7 @@ int ut_dprintf(int fd, const char *restrict format, ...)
int n = vsnprintf(line, MAX_LINE, format, ap);
va_end(ap);
if(n < 0 || n > MAX_LINE - 1) {return -1;}
if(readfill(fd, line, n)) {return -1;}
if(ut_writefill(fd, line, n)) {return -1;}
return n;
}
int ut_cursprintf(const char *restrict format, ...)
@ -284,48 +298,48 @@ int ut_cursprintf(const char *restrict format, ...)
int n = vsnprintf(line, MAX_LINE, format, ap);
va_end(ap);
if(n < 0 || n > MAX_LINE - 1) {return -1;}
if(ut_info(line)) {return -1;}
ut_info(line, n);
return n;
}
void ut_show(const struct ut_state *state, FILE *file, bool as_comment) {
void ut_show(const struct ut_state *state, int fd, bool as_comment) {
#define tiles state->tiles
printf("Turn: %c\nPlay board: %d\n", (int)state->player, state->playBoard);
ut_dprintf(fd, "%sTurn: %c\nPlay board: %d\n", as_comment ? "| " : "", (int)state->player, state->playBoard);
int play_board_row = -1;
int play_board_col = -1;
if (state->playBoard != -1) {
play_board_row = state->playBoard / 3;
play_board_col = state->playBoard % 3;
}
fprintf(file, "%s 012 345 678\n", as_comment ? "| " : "");
ut_dprintf(fd, "%s 012 345 678\n", as_comment ? "| " : "");
for(int y = 0; y < 9; y++)
{
if (y == 3 || y == 6) {
fprintf(file, "%s ---+---+---\n", as_comment ? "| " : "");
ut_dprintf(fd, "%s ---+---+---\n", as_comment ? "| " : "");
}
fprintf(file, "%s%d ", as_comment ? "| " : "", y);
ut_dprintf(fd, "%s%d ", as_comment ? "| " : "", y);
for(int x = 0; x < 9; x++)
{
if (x == 3 || x == 6) {
fputc('|', file);
ut_writefill(fd, "|", 1);
}
fputc(tiles[y][x] ? tiles[y][x] : ' ', file);
ut_writefill(fd, tiles[y][x] ? &tiles[y][x] : " ", 1);
}
if (y / 3 == play_board_row || play_board_row == -1) {
fputc('<', file);
ut_writefill(fd, "<", 1);
}
fputc('\n', file);
ut_writefill(fd, "\n", 1);
}
if (play_board_col == -1) {
fprintf(file, "%s ^^^ ^^^ ^^^", as_comment ? "| " : "");
ut_dprintf(fd, "%s ^^^ ^^^ ^^^", as_comment ? "| " : "");
} else {
fprintf(file, "%s ", as_comment ? "| " : "");
ut_dprintf(fd, "%s ", as_comment ? "| " : "");
for (int i = 0; i < play_board_col; i++) {
fprintf(file, " ");
ut_dprintf(fd, " ");
}
fprintf(file, "^^^");
ut_dprintf(fd, "^^^");
}
fputc('\n', file);
ut_writefill(fd, "\n", 1);
#undef tiles
}
@ -369,7 +383,7 @@ bool getpos(const struct ut_state *state, int *x, int *y) {
return true;
}
void getpos_net(const struct ut_state *state, int conn, FILE* conn_file, int *x, int *y) {
void getpos_net(const struct ut_state *state, int conn, int *x, int *y) { // TODO
const int MAX_LINE_LEN = 120;
char byte;
int n;
@ -391,7 +405,7 @@ void getpos_net(const struct ut_state *state, int conn, FILE* conn_file, int *x,
} else {
if (byte == '\n') {
printf("Partner ended line before coords were complete - retrying\n");
fprintf(conn_file, "| Invalid coordinates. Try again: ");
ut_dprintf(conn_file, "| Invalid coordinates. Try again: ");
fflush(conn_file);
continue;
}
@ -400,7 +414,7 @@ void getpos_net(const struct ut_state *state, int conn, FILE* conn_file, int *x,
if (n == 0) {goto conn_closed;}
if (byte == '\n') {
printf("Partner ended line before coords were complete - retrying\n");
fprintf(conn_file, "| Invalid coordinates. Try again: ");
ut_dprintf(conn_file, "| Invalid coordinates. Try again: ");
fflush(conn_file);
continue;
}
@ -408,7 +422,7 @@ void getpos_net(const struct ut_state *state, int conn, FILE* conn_file, int *x,
if (n == 0) {goto conn_closed;}
if (byte == '\n') {
printf("Partner ended line before coords were complete - retrying\n");
fprintf(conn_file, "| Invalid coordinates. Try again: ");
ut_dprintf(conn_file, "| Invalid coordinates. Try again: ");
fflush(conn_file);
continue;
}
@ -417,7 +431,7 @@ void getpos_net(const struct ut_state *state, int conn, FILE* conn_file, int *x,
if (n == 0) {goto conn_closed;}
if (byte != '\n') {
printf("Partner sent extra data after coords were complete - ignoring and retrying\n");
fprintf(conn_file, "| Invalid coordinates. Try again: ");
ut_dprintf(conn_file, "| Invalid coordinates. Try again: ");
fflush(conn_file);
// ignore the rest of this line
for (int i = 0; i < MAX_LINE_LEN; i++) {
@ -516,7 +530,7 @@ void ut_local_game(struct ut_state *state) { // TODO replace with from ncurses m
}
}
void ut_network_game(struct ut_state *state, int conn, FILE* conn_file, char player) {
void ut_network_game(struct ut_state *state, int conn, FILE* conn_file, char player) { // TODO
int x, y;
printf("You play as %c.\n", player);
while (true) {
@ -524,20 +538,20 @@ void ut_network_game(struct ut_state *state, int conn, FILE* conn_file, char pla
ut_show_boards(state);
ut_show(state, conn_file, true);
if (state->player == player) {
fprintf(conn_file, "| Waiting for game partner ...\n");
ut_dprintf(conn_file, "| Waiting for game partner ...\n");
fflush(conn_file);
bool ok = getpos(state, &x, &y);
if (!ok) {continue;}
fprintf(conn_file, "%d,%d\n", x, y);
ut_dprintf(conn_file, "%d,%d\n", x, y);
} else {
printf("Waiting for game partner ...\n");
fprintf(conn_file, "| Place token %c in position x,y: ", ut_turn(player));
ut_dprintf(conn_file, "| Place token %c in position x,y: ", ut_turn(player));
fflush(conn_file);
getpos_net(state, conn, conn_file, &x, &y);
}
int err = ut_move(state, state, y, x);
if (err) {continue;}
fprintf(conn_file, "|\n");
ut_dprintf(conn_file, "|\n");
char w = ut_winner((char *)state->boards, 0, 3);
if(w)
{
@ -588,14 +602,14 @@ int main(int argc, char **argv) {
printf("error %d\n", errsv);
return 1;
}
FILE* conn_file = fdopen(conn, "w");
FILE* conn_file = fdopen(conn, "w"); // TODO
// decide X or O
int random = open("/dev/urandom", O_RDONLY);
int byte;
read(random, &byte, 1);
char player = byte % 2 == 0 ? 'X' : 'O';
// tell partner X or O
fprintf(conn_file, "%c\n", ut_turn(player));
ut_dprintf(conn_file, "%c\n", ut_turn(player)); // TODO
ut_network_game(&state, conn, conn_file, player);
} else if (strncmp(argv[1], arg_join, strlen(arg_join)) == 0) {
int sock = socket(AF_INET, SOCK_STREAM, 0);