From 7c2792c406956718215ac60c63451b41ccc68689 Mon Sep 17 00:00:00 2001 From: ninya9k <> Date: Mon, 13 Nov 2023 12:09:56 +0000 Subject: [PATCH] pipe as comment character for network --- uttt.c | 32 ++++++++++++++++---------------- 1 file changed, 16 insertions(+), 16 deletions(-) diff --git a/uttt.c b/uttt.c index 725b7ae..379cfa4 100644 --- a/uttt.c +++ b/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) {