コミットを比較

...

4 コミット

作成者 SHA1 メッセージ 日付
ninya9k 61cfc2db1c network coords parsing 2023-11-13 11:55:12 +00:00
ninya9k 012d16c55c send game layout over socket 2023-11-13 10:32:07 +00:00
ninya9k 00d635bea9 fix debug boards view 2023-11-13 10:29:13 +00:00
ninya9k 77fb778d0b can't play on completed boards 2023-11-13 10:22:19 +00:00
1個のファイルの変更113行の追加43行の削除

156
uttt.c
ファイルの表示

@ -89,6 +89,9 @@ int ut_move(struct ut_state *new_state, const struct ut_state *old_state, int ro
// bad move - tile is occupied
if(old_state->tiles[row][col] != '\0') {return 1;} // replace with ' ' maybe
// bad move - board is not playable
if(old_state->boards[row / 3][col / 3] != '\0') {return 1;} // replace with ' ' maybe
// copy old_state->{tiles,boards} to new_state->{tiles,boards}
memmove(new_state->tiles, old_state->tiles, sizeof(old_state->tiles));
memmove(new_state->boards, old_state->boards, sizeof(old_state->boards));
@ -104,51 +107,51 @@ 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) {
#define tiles state->tiles
printf("Turn: %c\nPlay board: %d\n", (int)state->player, state->playBoard);
void ut_show(const struct ut_state *state, FILE *file) {
#define tiles state->tiles
printf("Turn: %c\nPlay board: %d\n", (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;
}
printf(" 012 345 678\n");
for(int y = 0; y < 9; y++)
{
fprintf(file, " 012 345 678\n");
for(int y = 0; y < 9; y++)
{
if (y == 3 || y == 6) {
printf(" ---+---+---\n");
fprintf(file, " ---+---+---\n");
}
printf("%d ", y);
for(int x = 0; x < 9; x++)
{
fprintf(file, "%d ", y);
for(int x = 0; x < 9; x++)
{
if (x == 3 || x == 6) {
putchar('|');
fputc('|', file);
}
putchar(tiles[y][x] ? tiles[y][x] : ' ');
}
fputc(tiles[y][x] ? tiles[y][x] : ' ', file);
}
if (y / 3 == play_board_row || play_board_row == -1) {
printf("<");
fputc('<', file);
}
putchar('\n');
}
if (play_board_col == -1) {
printf(" ^^^ ^^^ ^^^");
} else {
printf(" ");
for (int i = 0; i < play_board_col; i++) {
printf(" ");
}
printf("^^^");
fputc('\n', file);
}
putchar('\n');
#undef tiles
if (play_board_col == -1) {
fprintf(file, " ^^^ ^^^ ^^^");
} else {
fprintf(file, " ");
for (int i = 0; i < play_board_col; i++) {
fprintf(file, " ");
}
fprintf(file, "^^^");
}
fputc('\n', file);
#undef tiles
}
void ut_show_boards(const struct ut_state *state) {
for (int j = 0; j < 3; j++) {
for (int i = 0; i < 3; i++) {
putchar(state->boards[j][i]);
putchar(state->boards[j][i] ? state->boards[j][i] : ' ');
}
putchar('\n');
}
@ -185,18 +188,78 @@ bool getpos(const struct ut_state *state, int *x, int *y) {
return true;
}
bool getpos_net(const struct ut_state *state, int conn, int *x, int *y) {
char buf[4];
int n = recv(conn, &buf, 4, 0); // lazy way but idc
printf("bytes received: %d\n", n);
printf("buf=%s\n", buf);
*x = buf[0] - 0x30;
*y = buf[2] - 0x30;
printf("x=%d y=%d\n", *x, *y);
if (buf[1] != ',' || buf[3] != '\n' || *x >= 9 || *y >= 9 || *x < 0 || *y < 0) {
printf("protocol not followed perfectly by partner - exiting\n");
exit(1);
void getpos_net(const struct ut_state *state, int conn, FILE* conn_file, int *x, int *y) {
const int MAX_LINE_LEN = 120;
char byte;
int n;
while (true) {
n = recv(conn, &byte, 1, 0);
if (n == 0) {goto conn_closed;}
if (byte == '|') {
// ignore this line
for (int i = 0; i < MAX_LINE_LEN; i++) {
n = recv(conn, &byte, 1, 0);
if (n == 0) {goto conn_closed;}
if (byte == '\n') {break;}
}
if (byte != '\n') {
printf("Partner sent too much data in one comment line - exiting\n");
close(conn);
exit(1);
}
} else {
if (byte == '\n') {
printf("Partner ended line before coords were complete - retrying\n");
fprintf(conn_file, "Invalid coordinates. Try again: ");
fflush(conn_file);
continue;
}
*x = byte - 0x30;
n = recv(conn, &byte, 1, 0);
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: ");
fflush(conn_file);
continue;
}
n = recv(conn, &byte, 1, 0);
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: ");
fflush(conn_file);
continue;
}
*y = byte - 0x30;
n = recv(conn, &byte, 1, 0);
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: ");
fflush(conn_file);
// ignore the rest of this line
for (int i = 0; i < MAX_LINE_LEN; i++) {
n = recv(conn, &byte, 1, 0);
if (n == 0) {goto conn_closed;}
if (byte == '\n') {break;}
}
if (byte != '\n') {
printf("Partner sent too much extra data - exiting\n");
close(conn);
exit(1);
}
continue;
}
break;
}
}
printf("x=%d y=%d\n", *x, *y);
return;
conn_closed:
printf("Connection closed.\n");
exit(1);
}
void finish(int sig)
@ -210,7 +273,7 @@ void finish(int sig)
void ut_local_game(struct ut_state *state) {
int x, y;
while (true) {
ut_show(state);
ut_show(state, stdout);
ut_show_boards(state);
bool ok = getpos(state, &x, &y);
if (!ok) {continue;}
@ -234,18 +297,25 @@ void ut_local_game(struct ut_state *state) {
void ut_host_game(struct ut_state *state, int conn, char host_player) {
int x, y;
printf("You play as %c.\n", host_player);
FILE* conn_file = fdopen(conn, "w");
while (true) {
ut_show(state);
ut_show(state, stdout);
ut_show_boards(state);
ut_show(state, conn_file);
if (state->player == host_player) {
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 parter ...\n");
getpos_net(state, conn, &x, &y);
printf("Waiting for game partner ...\n");
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, x, y);
int err = ut_move(state, state, y, x);
if (err) {continue;}
fputc('\n', conn_file);
char w = ut_winner((char *)state->boards, 0, 3);
if(w)
{