pipe as comment character for network

このコミットが含まれているのは:
ninya9k 2023-11-13 12:09:56 +00:00
コミット 7c2792c406
1個のファイルの変更16行の追加16行の削除

32
uttt.c
ファイルの表示

@ -107,7 +107,7 @@ int ut_move(struct ut_state *new_state, const struct ut_state *old_state, int ro
return 0;
}
void ut_show(const struct ut_state *state, FILE *file) {
void ut_show(const struct ut_state *state, FILE *file, bool as_comment) {
#define tiles state->tiles
printf("Turn: %c\nPlay board: %d\n", (int)state->player, state->playBoard);
int play_board_row = -1;
@ -116,13 +116,13 @@ void ut_show(const struct ut_state *state, FILE *file) {
play_board_row = state->playBoard / 3;
play_board_col = state->playBoard % 3;
}
fprintf(file, " 012 345 678\n");
fprintf(file, "%s 012 345 678\n", as_comment ? "| " : "");
for(int y = 0; y < 9; y++)
{
if (y == 3 || y == 6) {
fprintf(file, " ---+---+---\n");
fprintf(file, "%s ---+---+---\n", as_comment ? "| " : "");
}
fprintf(file, "%d ", y);
fprintf(file, "%s%d ", as_comment ? "| " : "", y);
for(int x = 0; x < 9; x++)
{
if (x == 3 || x == 6) {
@ -136,9 +136,9 @@ void ut_show(const struct ut_state *state, FILE *file) {
fputc('\n', file);
}
if (play_board_col == -1) {
fprintf(file, " ^^^ ^^^ ^^^");
fprintf(file, "%s ^^^ ^^^ ^^^", as_comment ? "| " : "");
} else {
fprintf(file, " ");
fprintf(file, "%s ", as_comment ? "| " : "");
for (int i = 0; i < play_board_col; i++) {
fprintf(file, " ");
}
@ -210,7 +210,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: ");
fprintf(conn_file, "| Invalid coordinates. Try again: ");
fflush(conn_file);
continue;
}
@ -219,7 +219,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: ");
fprintf(conn_file, "| Invalid coordinates. Try again: ");
fflush(conn_file);
continue;
}
@ -227,7 +227,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: ");
fprintf(conn_file, "| Invalid coordinates. Try again: ");
fflush(conn_file);
continue;
}
@ -236,7 +236,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: ");
fprintf(conn_file, "| Invalid coordinates. Try again: ");
fflush(conn_file);
// ignore the rest of this line
for (int i = 0; i < MAX_LINE_LEN; i++) {
@ -273,7 +273,7 @@ void finish(int sig)
void ut_local_game(struct ut_state *state) {
int x, y;
while (true) {
ut_show(state, stdout);
ut_show(state, stdout, false);
ut_show_boards(state);
bool ok = getpos(state, &x, &y);
if (!ok) {continue;}
@ -299,23 +299,23 @@ void ut_host_game(struct ut_state *state, int conn, char host_player) {
printf("You play as %c.\n", host_player);
FILE* conn_file = fdopen(conn, "w");
while (true) {
ut_show(state, stdout);
ut_show(state, stdout, false);
ut_show_boards(state);
ut_show(state, conn_file);
ut_show(state, conn_file, true);
if (state->player == host_player) {
fprintf(conn_file, "Waiting for game partner ...\n");
fprintf(conn_file, "| Waiting for game partner ...\n");
fflush(conn_file);
bool ok = getpos(state, &x, &y);
if (!ok) {continue;}
} else {
printf("Waiting for game partner ...\n");
fprintf(conn_file, "Place token %c in position x,y: ", ut_turn(host_player));
fprintf(conn_file, "| Place token %c in position x,y: ", ut_turn(host_player));
fflush(conn_file);
getpos_net(state, conn, conn_file, &x, &y);
}
int err = ut_move(state, state, y, x);
if (err) {continue;}
fputc('\n', conn_file);
fprintf(conn_file, "|\n");
char w = ut_winner((char *)state->boards, 0, 3);
if(w)
{