pipe as comment character for network
このコミットが含まれているのは:
コミット
7c2792c406
32
uttt.c
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;
|
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
|
#define tiles state->tiles
|
||||||
printf("Turn: %c\nPlay board: %d\n", (int)state->player, state->playBoard);
|
printf("Turn: %c\nPlay board: %d\n", (int)state->player, state->playBoard);
|
||||||
int play_board_row = -1;
|
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_row = state->playBoard / 3;
|
||||||
play_board_col = 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++)
|
for(int y = 0; y < 9; y++)
|
||||||
{
|
{
|
||||||
if (y == 3 || y == 6) {
|
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++)
|
for(int x = 0; x < 9; x++)
|
||||||
{
|
{
|
||||||
if (x == 3 || x == 6) {
|
if (x == 3 || x == 6) {
|
||||||
|
@ -136,9 +136,9 @@ void ut_show(const struct ut_state *state, FILE *file) {
|
||||||
fputc('\n', file);
|
fputc('\n', file);
|
||||||
}
|
}
|
||||||
if (play_board_col == -1) {
|
if (play_board_col == -1) {
|
||||||
fprintf(file, " ^^^ ^^^ ^^^");
|
fprintf(file, "%s ^^^ ^^^ ^^^", as_comment ? "| " : "");
|
||||||
} else {
|
} else {
|
||||||
fprintf(file, " ");
|
fprintf(file, "%s ", as_comment ? "| " : "");
|
||||||
for (int i = 0; i < play_board_col; i++) {
|
for (int i = 0; i < play_board_col; i++) {
|
||||||
fprintf(file, " ");
|
fprintf(file, " ");
|
||||||
}
|
}
|
||||||
|
@ -210,7 +210,7 @@ void getpos_net(const struct ut_state *state, int conn, FILE* conn_file, int *x,
|
||||||
} else {
|
} else {
|
||||||
if (byte == '\n') {
|
if (byte == '\n') {
|
||||||
printf("Partner ended line before coords were complete - retrying\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);
|
fflush(conn_file);
|
||||||
continue;
|
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 (n == 0) {goto conn_closed;}
|
||||||
if (byte == '\n') {
|
if (byte == '\n') {
|
||||||
printf("Partner ended line before coords were complete - retrying\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);
|
fflush(conn_file);
|
||||||
continue;
|
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 (n == 0) {goto conn_closed;}
|
||||||
if (byte == '\n') {
|
if (byte == '\n') {
|
||||||
printf("Partner ended line before coords were complete - retrying\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);
|
fflush(conn_file);
|
||||||
continue;
|
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 (n == 0) {goto conn_closed;}
|
||||||
if (byte != '\n') {
|
if (byte != '\n') {
|
||||||
printf("Partner sent extra data after coords were complete - ignoring and retrying\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);
|
fflush(conn_file);
|
||||||
// ignore the rest of this line
|
// ignore the rest of this line
|
||||||
for (int i = 0; i < MAX_LINE_LEN; i++) {
|
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) {
|
void ut_local_game(struct ut_state *state) {
|
||||||
int x, y;
|
int x, y;
|
||||||
while (true) {
|
while (true) {
|
||||||
ut_show(state, stdout);
|
ut_show(state, stdout, false);
|
||||||
ut_show_boards(state);
|
ut_show_boards(state);
|
||||||
bool ok = getpos(state, &x, &y);
|
bool ok = getpos(state, &x, &y);
|
||||||
if (!ok) {continue;}
|
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);
|
printf("You play as %c.\n", host_player);
|
||||||
FILE* conn_file = fdopen(conn, "w");
|
FILE* conn_file = fdopen(conn, "w");
|
||||||
while (true) {
|
while (true) {
|
||||||
ut_show(state, stdout);
|
ut_show(state, stdout, false);
|
||||||
ut_show_boards(state);
|
ut_show_boards(state);
|
||||||
ut_show(state, conn_file);
|
ut_show(state, conn_file, true);
|
||||||
if (state->player == host_player) {
|
if (state->player == host_player) {
|
||||||
fprintf(conn_file, "Waiting for game partner ...\n");
|
fprintf(conn_file, "| Waiting for game partner ...\n");
|
||||||
fflush(conn_file);
|
fflush(conn_file);
|
||||||
bool ok = getpos(state, &x, &y);
|
bool ok = getpos(state, &x, &y);
|
||||||
if (!ok) {continue;}
|
if (!ok) {continue;}
|
||||||
} else {
|
} else {
|
||||||
printf("Waiting for game partner ...\n");
|
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);
|
fflush(conn_file);
|
||||||
getpos_net(state, conn, conn_file, &x, &y);
|
getpos_net(state, conn, conn_file, &x, &y);
|
||||||
}
|
}
|
||||||
int err = ut_move(state, state, y, x);
|
int err = ut_move(state, state, y, x);
|
||||||
if (err) {continue;}
|
if (err) {continue;}
|
||||||
fputc('\n', conn_file);
|
fprintf(conn_file, "|\n");
|
||||||
char w = ut_winner((char *)state->boards, 0, 3);
|
char w = ut_winner((char *)state->boards, 0, 3);
|
||||||
if(w)
|
if(w)
|
||||||
{
|
{
|
||||||
|
|
読み込み中…
新しいイシューから参照