2023-11-12 19:34:40 +09:00
|
|
|
#include <stddef.h>
|
|
|
|
#include <stdbool.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
2023-11-12 19:57:13 +09:00
|
|
|
#include <curses.h>
|
2023-11-12 20:43:33 +09:00
|
|
|
#include <signal.h>
|
2023-11-12 22:31:24 +09:00
|
|
|
#include <sys/socket.h>
|
|
|
|
#include <netinet/in.h>
|
|
|
|
#include <arpa/inet.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <errno.h>
|
2023-11-13 00:05:26 +09:00
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <unistd.h>
|
2023-11-12 19:34:40 +09:00
|
|
|
|
2023-11-12 19:57:13 +09:00
|
|
|
struct ut_state
|
2023-11-12 19:34:40 +09:00
|
|
|
{
|
2023-11-12 19:57:13 +09:00
|
|
|
char tiles[9][9];
|
|
|
|
char boards[3][3];
|
|
|
|
int playBoard;
|
|
|
|
char player;
|
2023-11-12 19:34:40 +09:00
|
|
|
};
|
2023-11-12 20:43:33 +09:00
|
|
|
const struct ut_state ut_initial = {
|
|
|
|
.boards = {0},
|
|
|
|
.tiles = {0},
|
|
|
|
.playBoard = -1,
|
|
|
|
.player = 'X'
|
|
|
|
};
|
2023-11-12 19:34:40 +09:00
|
|
|
|
2023-11-12 22:31:24 +09:00
|
|
|
const char *arg_host = "--host";
|
|
|
|
const char *arg_join = "--join";
|
2023-11-12 19:34:40 +09:00
|
|
|
|
2023-11-12 19:57:13 +09:00
|
|
|
char ut_turn(char player)
|
2023-11-12 19:34:40 +09:00
|
|
|
{
|
2023-11-12 19:57:13 +09:00
|
|
|
switch(player)
|
|
|
|
{
|
|
|
|
case 'X': return 'O';
|
|
|
|
case 'O': return 'X';
|
|
|
|
default: return '\0';
|
|
|
|
}
|
2023-11-12 19:34:40 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
#define T(r, c) (tiles[offset + stride * r + c])
|
2023-11-12 19:57:13 +09:00
|
|
|
char ut_winner(char *tiles, int offset, int stride) {
|
2023-11-12 19:34:40 +09:00
|
|
|
|
2023-11-12 19:57:13 +09:00
|
|
|
// whoreizontal wins
|
|
|
|
for (int y = 0; y < 3; y++)
|
|
|
|
{
|
|
|
|
char tile = T(y, 0);
|
|
|
|
if (tile == '\0') {continue;}
|
|
|
|
if (tile == T(y, 1) && tile == T(y, 2)) {return tile;}
|
|
|
|
}
|
|
|
|
|
|
|
|
// vertical wins
|
|
|
|
for (int x = 0; x < 3; x++)
|
|
|
|
{
|
|
|
|
char tile = T(0, x);
|
|
|
|
if (tile == '\0') {continue;}
|
|
|
|
if (tile == T(1, x) && tile == T(2, x)) {return tile;}
|
|
|
|
}
|
|
|
|
|
|
|
|
// diagonalz
|
|
|
|
char tile = T(1, 1);
|
2023-11-12 20:43:33 +09:00
|
|
|
if (tile != '\0')
|
|
|
|
{
|
|
|
|
if (tile == T(0, 0) && tile == T(2, 2)) {return tile;}
|
|
|
|
if (tile == T(2, 0) && tile == T(0, 2)) {return tile;}
|
2023-11-12 20:39:43 +09:00
|
|
|
}
|
|
|
|
|
2023-11-12 20:43:33 +09:00
|
|
|
for(int y = 0; y < 3; y++)
|
|
|
|
{
|
|
|
|
for(int x = 0; x < 3; x++)
|
|
|
|
{
|
|
|
|
if(T(y, x) == '\0') {return '\0';} // in progress
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return ' '; // draw
|
2023-11-12 19:34:40 +09:00
|
|
|
}
|
|
|
|
|
2023-11-12 19:57:13 +09:00
|
|
|
int ut_move(struct ut_state *new_state, const struct ut_state *old_state, int row, int col)
|
2023-11-12 19:34:40 +09:00
|
|
|
{
|
2023-11-12 19:57:13 +09:00
|
|
|
// bad move - out of bounds
|
|
|
|
if(row < 0 || row >= 9 || col < 0 || col >= 9) {return 1;}
|
2023-11-12 19:34:40 +09:00
|
|
|
|
2023-11-12 19:57:13 +09:00
|
|
|
// state->playBoard == -1 means all boards playable; otherwise only one board is playable
|
|
|
|
if (old_state->playBoard != -1 && old_state->playBoard != 3 * (row / 3) + (col / 3)) {return 1;}
|
2023-11-12 19:34:40 +09:00
|
|
|
|
2023-11-12 19:57:13 +09:00
|
|
|
// bad move - tile is occupied
|
|
|
|
if(old_state->tiles[row][col] != '\0') {return 1;} // replace with ' ' maybe
|
2023-11-12 19:34:40 +09:00
|
|
|
|
2023-11-13 19:22:19 +09:00
|
|
|
// bad move - board is not playable
|
|
|
|
if(old_state->boards[row / 3][col / 3] != '\0') {return 1;} // replace with ' ' maybe
|
|
|
|
|
2023-11-12 19:57:13 +09:00
|
|
|
// 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));
|
2023-11-12 19:34:40 +09:00
|
|
|
|
2023-11-12 19:57:13 +09:00
|
|
|
// do move
|
|
|
|
new_state->tiles[row][col] = old_state->player;
|
2023-11-12 20:58:58 +09:00
|
|
|
new_state->boards[row / 3][col / 3] = ut_winner((char *)new_state->tiles, 27 * (row / 3) + 3 * (col / 3), 9);
|
2023-11-12 19:57:13 +09:00
|
|
|
|
|
|
|
// next play board - if next play board is not playable, play any board
|
|
|
|
new_state->playBoard = new_state->boards[row % 3][col % 3] == '\0' ? 3 * (row % 3) + (col % 3) : -1;
|
2023-11-12 19:34:40 +09:00
|
|
|
|
2023-11-12 19:57:13 +09:00
|
|
|
new_state->player = ut_turn(old_state->player);
|
|
|
|
return 0;
|
2023-11-12 19:34:40 +09:00
|
|
|
}
|
|
|
|
|
2023-11-13 21:09:56 +09:00
|
|
|
void ut_show(const struct ut_state *state, FILE *file, bool as_comment) {
|
2023-11-13 19:32:07 +09:00
|
|
|
#define tiles state->tiles
|
|
|
|
printf("Turn: %c\nPlay board: %d\n", (int)state->player, state->playBoard);
|
2023-11-12 19:34:40 +09:00
|
|
|
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;
|
|
|
|
}
|
2023-11-13 21:09:56 +09:00
|
|
|
fprintf(file, "%s 012 345 678\n", as_comment ? "| " : "");
|
2023-11-13 19:32:07 +09:00
|
|
|
for(int y = 0; y < 9; y++)
|
|
|
|
{
|
2023-11-12 19:34:40 +09:00
|
|
|
if (y == 3 || y == 6) {
|
2023-11-13 21:09:56 +09:00
|
|
|
fprintf(file, "%s ---+---+---\n", as_comment ? "| " : "");
|
2023-11-12 19:34:40 +09:00
|
|
|
}
|
2023-11-13 21:09:56 +09:00
|
|
|
fprintf(file, "%s%d ", as_comment ? "| " : "", y);
|
2023-11-13 19:32:07 +09:00
|
|
|
for(int x = 0; x < 9; x++)
|
|
|
|
{
|
2023-11-12 19:34:40 +09:00
|
|
|
if (x == 3 || x == 6) {
|
2023-11-13 19:32:07 +09:00
|
|
|
fputc('|', file);
|
2023-11-12 19:34:40 +09:00
|
|
|
}
|
2023-11-13 19:32:07 +09:00
|
|
|
fputc(tiles[y][x] ? tiles[y][x] : ' ', file);
|
|
|
|
}
|
2023-11-12 21:05:57 +09:00
|
|
|
if (y / 3 == play_board_row || play_board_row == -1) {
|
2023-11-13 19:32:07 +09:00
|
|
|
fputc('<', file);
|
2023-11-12 19:34:40 +09:00
|
|
|
}
|
2023-11-13 19:32:07 +09:00
|
|
|
fputc('\n', file);
|
|
|
|
}
|
2023-11-12 21:05:57 +09:00
|
|
|
if (play_board_col == -1) {
|
2023-11-13 21:09:56 +09:00
|
|
|
fprintf(file, "%s ^^^ ^^^ ^^^", as_comment ? "| " : "");
|
2023-11-12 21:05:57 +09:00
|
|
|
} else {
|
2023-11-13 21:09:56 +09:00
|
|
|
fprintf(file, "%s ", as_comment ? "| " : "");
|
2023-11-12 19:34:40 +09:00
|
|
|
for (int i = 0; i < play_board_col; i++) {
|
2023-11-13 19:32:07 +09:00
|
|
|
fprintf(file, " ");
|
2023-11-12 19:34:40 +09:00
|
|
|
}
|
2023-11-13 19:32:07 +09:00
|
|
|
fprintf(file, "^^^");
|
2023-11-12 19:34:40 +09:00
|
|
|
}
|
2023-11-13 19:32:07 +09:00
|
|
|
fputc('\n', file);
|
|
|
|
#undef tiles
|
2023-11-12 19:34:40 +09:00
|
|
|
}
|
|
|
|
|
2023-11-13 00:05:26 +09:00
|
|
|
void ut_show_boards(const struct ut_state *state) {
|
|
|
|
for (int j = 0; j < 3; j++) {
|
|
|
|
for (int i = 0; i < 3; i++) {
|
2023-11-13 19:29:13 +09:00
|
|
|
putchar(state->boards[j][i] ? state->boards[j][i] : ' ');
|
2023-11-13 00:05:26 +09:00
|
|
|
}
|
|
|
|
putchar('\n');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-11-12 19:57:13 +09:00
|
|
|
bool getpos(const struct ut_state *state, int *x, int *y) {
|
|
|
|
while (true) {
|
|
|
|
printf("Place token %c at position x,y: ", state->player);
|
|
|
|
char line[5] = {0};
|
|
|
|
if (fgets(line, 5, stdin) == NULL)
|
|
|
|
return false;
|
|
|
|
// line was too short
|
|
|
|
if (line[3] == '\0')
|
|
|
|
continue;
|
|
|
|
// line was too long
|
|
|
|
if (line[3] != '\n') {
|
|
|
|
// consume rest of line
|
|
|
|
while (true) {
|
|
|
|
int garbage = getchar();
|
|
|
|
if (garbage == EOF)
|
|
|
|
return false;
|
|
|
|
if (garbage == '\n')
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
int sscanf_result = sscanf(line, "%d,%d", x, y);
|
|
|
|
if (sscanf_result == EOF)
|
|
|
|
return false;
|
|
|
|
if (sscanf_result != 2)
|
|
|
|
continue;
|
2023-11-12 20:24:27 +09:00
|
|
|
break;
|
2023-11-12 19:57:13 +09:00
|
|
|
}
|
|
|
|
return true;
|
2023-11-12 19:34:40 +09:00
|
|
|
}
|
|
|
|
|
2023-11-13 20:55:12 +09:00
|
|
|
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");
|
2023-11-13 21:09:56 +09:00
|
|
|
fprintf(conn_file, "| Invalid coordinates. Try again: ");
|
2023-11-13 20:55:12 +09:00
|
|
|
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");
|
2023-11-13 21:09:56 +09:00
|
|
|
fprintf(conn_file, "| Invalid coordinates. Try again: ");
|
2023-11-13 20:55:12 +09:00
|
|
|
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");
|
2023-11-13 21:09:56 +09:00
|
|
|
fprintf(conn_file, "| Invalid coordinates. Try again: ");
|
2023-11-13 20:55:12 +09:00
|
|
|
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");
|
2023-11-13 21:09:56 +09:00
|
|
|
fprintf(conn_file, "| Invalid coordinates. Try again: ");
|
2023-11-13 20:55:12 +09:00
|
|
|
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;
|
|
|
|
}
|
2023-11-13 00:42:24 +09:00
|
|
|
}
|
2023-11-13 20:55:12 +09:00
|
|
|
printf("x=%d y=%d\n", *x, *y);
|
|
|
|
return;
|
|
|
|
|
|
|
|
conn_closed:
|
|
|
|
printf("Connection closed.\n");
|
|
|
|
exit(1);
|
2023-11-13 00:05:26 +09:00
|
|
|
}
|
|
|
|
|
2023-11-12 20:43:33 +09:00
|
|
|
void finish(int sig)
|
|
|
|
{
|
2023-11-12 22:39:12 +09:00
|
|
|
putchar('\n');
|
2023-11-12 20:43:33 +09:00
|
|
|
endwin();
|
|
|
|
// other cleanup
|
|
|
|
exit(0);
|
|
|
|
}
|
|
|
|
|
2023-11-12 22:31:24 +09:00
|
|
|
void ut_local_game(struct ut_state *state) {
|
2023-11-13 00:05:26 +09:00
|
|
|
int x, y;
|
2023-11-12 19:57:13 +09:00
|
|
|
while (true) {
|
2023-11-13 21:09:56 +09:00
|
|
|
ut_show(state, stdout, false);
|
2023-11-13 00:05:26 +09:00
|
|
|
ut_show_boards(state);
|
|
|
|
bool ok = getpos(state, &x, &y);
|
2023-11-12 19:57:13 +09:00
|
|
|
if (!ok) {continue;}
|
2023-11-12 22:31:24 +09:00
|
|
|
int err = ut_move(state, state, x, y);
|
2023-11-12 19:57:13 +09:00
|
|
|
if (err) {continue;}
|
2023-11-12 22:31:24 +09:00
|
|
|
char w = ut_winner((char *)state->boards, 0, 3);
|
2023-11-12 19:57:13 +09:00
|
|
|
if(w)
|
|
|
|
{
|
2023-11-12 20:43:33 +09:00
|
|
|
if(w == ' ')
|
|
|
|
{
|
|
|
|
printf("\nDraw!\n");
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
printf("\n%c wins!\n", (int)w);
|
|
|
|
}
|
2023-11-12 19:57:13 +09:00
|
|
|
}
|
|
|
|
}
|
2023-11-12 22:31:24 +09:00
|
|
|
}
|
|
|
|
|
2023-11-13 00:05:26 +09:00
|
|
|
void ut_host_game(struct ut_state *state, int conn, char host_player) {
|
|
|
|
int x, y;
|
|
|
|
printf("You play as %c.\n", host_player);
|
2023-11-13 19:32:07 +09:00
|
|
|
FILE* conn_file = fdopen(conn, "w");
|
2023-11-13 00:05:26 +09:00
|
|
|
while (true) {
|
2023-11-13 21:09:56 +09:00
|
|
|
ut_show(state, stdout, false);
|
2023-11-13 00:05:26 +09:00
|
|
|
ut_show_boards(state);
|
2023-11-13 21:09:56 +09:00
|
|
|
ut_show(state, conn_file, true);
|
2023-11-13 00:05:26 +09:00
|
|
|
if (state->player == host_player) {
|
2023-11-13 21:09:56 +09:00
|
|
|
fprintf(conn_file, "| Waiting for game partner ...\n");
|
2023-11-13 19:32:07 +09:00
|
|
|
fflush(conn_file);
|
2023-11-13 00:05:26 +09:00
|
|
|
bool ok = getpos(state, &x, &y);
|
|
|
|
if (!ok) {continue;}
|
|
|
|
} else {
|
2023-11-13 19:32:07 +09:00
|
|
|
printf("Waiting for game partner ...\n");
|
2023-11-13 21:09:56 +09:00
|
|
|
fprintf(conn_file, "| Place token %c in position x,y: ", ut_turn(host_player));
|
2023-11-13 19:32:07 +09:00
|
|
|
fflush(conn_file);
|
2023-11-13 20:55:12 +09:00
|
|
|
getpos_net(state, conn, conn_file, &x, &y);
|
2023-11-13 00:05:26 +09:00
|
|
|
}
|
2023-11-13 19:32:07 +09:00
|
|
|
int err = ut_move(state, state, y, x);
|
2023-11-13 00:42:24 +09:00
|
|
|
if (err) {continue;}
|
2023-11-13 21:09:56 +09:00
|
|
|
fprintf(conn_file, "|\n");
|
2023-11-13 00:05:26 +09:00
|
|
|
char w = ut_winner((char *)state->boards, 0, 3);
|
|
|
|
if(w)
|
|
|
|
{
|
|
|
|
if(w == ' ')
|
|
|
|
{
|
|
|
|
printf("\nDraw!\n");
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
printf("\n%c wins!\n", (int)w);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2023-11-12 22:31:24 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
int main(int argc, char **argv) {
|
|
|
|
signal(SIGINT, finish);
|
|
|
|
struct ut_state state = ut_initial;
|
|
|
|
if (argc < 2) {
|
|
|
|
ut_local_game(&state);
|
|
|
|
} else if (strncmp(argv[1], arg_host, strlen(arg_host)) == 0) {
|
|
|
|
int sock = socket(AF_INET, SOCK_STREAM, 0);
|
|
|
|
if (sock == -1) {
|
|
|
|
int errsv = errno;
|
|
|
|
printf("error %d\n", errsv);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
const struct sockaddr_in addr = {
|
|
|
|
.sin_family = AF_INET,
|
|
|
|
.sin_port = htons(6669),
|
|
|
|
.sin_addr = { .s_addr = htonl(0x7f000001) },
|
|
|
|
};
|
2023-11-13 00:05:26 +09:00
|
|
|
setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &(int){1}, sizeof(int));
|
2023-11-12 22:31:24 +09:00
|
|
|
if (bind(sock, (struct sockaddr*)&addr, sizeof(addr)) == -1) {
|
|
|
|
int errsv = errno;
|
|
|
|
printf("error %d\n", errsv);
|
|
|
|
return 1;
|
|
|
|
}
|
2023-11-13 00:05:26 +09:00
|
|
|
if (listen(sock, 128) == -1) {
|
|
|
|
int errsv = errno;
|
|
|
|
printf("error %d\n", errsv);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
printf("Waiting for game partner at 127.0.0.1:6669 ...\n");
|
|
|
|
int conn = accept(sock, NULL, NULL);
|
|
|
|
if (conn == -1) {
|
|
|
|
int errsv = errno;
|
|
|
|
printf("error %d\n", errsv);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
int random = open("/dev/urandom", O_RDONLY);
|
|
|
|
int byte;
|
|
|
|
read(random, &byte, 1);
|
|
|
|
//printf("%d\n", byte);
|
|
|
|
char host_player = byte % 2 == 0 ? 'X' : 'O';
|
|
|
|
//printf("%c\n", host_player);
|
|
|
|
ut_host_game(&state, conn, host_player);
|
2023-11-12 22:31:24 +09:00
|
|
|
} else if (strncmp(argv[1], arg_join, strlen(arg_join)) == 0) {
|
|
|
|
printf("no ;)\n");
|
|
|
|
} else {
|
|
|
|
printf("%s\n", argv[1]);
|
|
|
|
}
|
2023-11-12 19:57:13 +09:00
|
|
|
return 0;
|
2023-11-13 00:05:26 +09:00
|
|
|
|
2023-11-12 19:34:40 +09:00
|
|
|
}
|