コミットを比較

...

35 コミット

作成者 SHA1 メッセージ 日付
ninya9k 79be4652d3 readable play board names 2023-11-15 15:23:55 +00:00
ninya9k aa1f7fc20f remove deterministic starting player test 2023-11-15 14:57:22 +00:00
ninya9k b40eb333ff allow \r\n 2023-11-15 14:53:31 +00:00
ninya9k 39f29df835 fluff 2023-11-15 14:25:09 +00:00
ninya9k a57d6c2c50 werks.
fixes ncurses stuck display, swapped coordinates on network. handles comment
line arriving before player token line.
2023-11-15 14:24:16 +00:00
ninya9k 9369bad57c oOOooooOo game<->game mystery crash oOOooOOooooOoo 2023-11-15 12:36:57 +00:00
woosh e943629371 last move indicator and board line start detection 2023-11-15 12:12:38 +00:00
ninya9k 031c466236 make curses + network work
still kinda broken. messages for host are not printed and host must wait until
opponent makes a move until host sees their move
2023-11-15 11:06:25 +00:00
woosh 58d97e34b5 colors! 2023-11-15 09:54:04 +00:00
woosh 7c6495a72a better board win indication 2023-11-15 08:50:35 +00:00
ninya9k 01f4e1e021 use ut_sockdraw 2023-11-15 08:34:51 +00:00
ninya9k 26be1ff137 btw ignore commit message 6fad23c1
should be: readable iff not game<->game session
2023-11-15 08:32:45 +00:00
woosh 6314045855 Merge remote-tracking branch 'refs/remotes/origin/cleaner' into cleaner 2023-11-15 08:17:59 +00:00
woosh d5b5220c8b fixed initial r,c 2023-11-15 08:16:45 +00:00
ninya9k a98459b46e lol 2023-11-15 08:09:16 +00:00
ninya9k 194825db5a Merge remote-tracking branch 'origin/cleaner' into cleaner 2023-11-15 08:07:35 +00:00
ninya9k 771b1e9ba8 readable socket niceties 2023-11-15 08:05:54 +00:00
woosh 8c83ddcba4 fixing input and curses board - still broken 2023-11-15 08:05:29 +00:00
ninya9k 573ae23c48 send board over socket 2023-11-15 08:04:45 +00:00
ninya9k 6a669af1cd ignore | lines 2023-11-15 07:48:05 +00:00
ninya9k 6fad23c17d readable iff game<->game session 2023-11-15 07:40:24 +00:00
ninya9k 17bb8103ac fix oopsie 2023-11-15 07:04:36 +00:00
ninya9k d7f2f88615 delete pipe character 2023-11-15 07:03:20 +00:00
ninya9k 8cb22b7dbb not using socket buffers 2023-11-15 06:57:54 +00:00
woosh 046feddf0b wait for key before exiting ncurses and more 2023-11-15 06:49:07 +00:00
woosh d50db74ddb sequence points are real and indirection is bad 2023-11-14 22:15:26 -08:00
woosh 4409c01211 fixing and host_player TODO 2023-11-14 21:26:20 -08:00
woosh f5c31f72a8 dont always initialize ncurses 2023-11-14 20:54:53 -08:00
woosh 5b976d0c8b more moving fixing 2023-11-14 20:43:23 -08:00
woosh 430f5a31c6 moving stuff around 2023-11-14 20:25:47 -08:00
woosh bc7c229792 unifying board drawing WIP 2023-11-14 20:07:38 -08:00
ninya9k 5e772aecb5 close connection at game end i guess 2023-11-15 02:49:30 +00:00
ninya9k 7a84bf052a use socket buffer everywhere 2023-11-15 02:47:05 +00:00
ninya9k dd32ab8251 fix ring buffer 2023-11-15 02:46:07 +00:00
ninya9k f5ffa6ab26 socket ring buffer and readline 2023-11-14 14:10:49 +00:00
1個のファイルの変更389行の追加336行の削除

725
uttt.c
ファイルの表示

@ -21,17 +21,15 @@ struct ut_state
char boards[3][3];
int playBoard;
char player;
char host_player;
};
const struct ut_state ut_initial = {
.boards = {0},
.tiles = {0},
.playBoard = -1,
.player = 'X',
.host_player = '\0',
};
};
const char *HELP_TEXT = "\
const char HELP_TEXT[] = "\
Usage: uttt (--local | --host | --join)\n\
\n\
--local play non-network game\n\
@ -39,9 +37,16 @@ Usage: uttt (--local | --host | --join)\n\
--join join game at 127.0.0.1:6669\n\
";
const char *arg_local = "--local";
const char *arg_host = "--host";
const char *arg_join = "--join";
const char arg_local[] = "--local";
const char arg_host[] = "--host";
const char arg_join[] = "--join";
const char PLAY_BOARDS[][14] = {
"top left", "top middle", "top right",
"middle left", "middle", "middle right",
"bottom left", "bottom middle", "bottom right",
"all boards",
};
char ut_turn(char player)
{
@ -53,13 +58,6 @@ char ut_turn(char player)
}
}
int ut_random_player(char *player) {
int random = open("/dev/urandom", O_RDONLY);
if (read(random, player, 1) < 0) {return errno;}
*player = *player % 2 == 0 ? 'X' : 'O';
return 0;
}
#define T(r, c) (tiles[offset + stride * r + c])
char ut_winner(const char *tiles, int offset, int stride) {
@ -126,9 +124,61 @@ int ut_move(struct ut_state *new_state, const struct ut_state *old_state, int ro
return 0;
}
void ut_drawBoard(const char *tiles, int offset, int stride, int iy, int ix, char winner, bool highlight)
int curs_line;
void ut_curserase(void)
{
// 5x5 board display
curs_line = 0;
erase();
}
void ut_cursprint(const char *str, int n)
{
mvaddnstr(curs_line, 0, str, n);
int y, x;
getyx(stdscr, y, x);
curs_line = y;
}
int ut_writefill(int fd, const char *x, size_t l)
{
while(l > 0)
{
int r = write(fd, x, l);
if(r < 0)
{
if(errno == EAGAIN || errno == EWOULDBLOCK || errno == EINTR) {continue;}
else {return 1;}
}
x += r; l -= r;
}
return 0;
}
#define MAX_LINE 128
int ut_cursprintf(const char *restrict format, ...)
{
char line[MAX_LINE];
va_list ap;
va_start(ap, format);
int n = vsnprintf(line, MAX_LINE, format, ap);
va_end(ap);
if(n < 0 || n > MAX_LINE - 1) {return -1;}
ut_cursprint(line, n);
return n;
}
int ut_dprintf(int fd, const char *restrict format, ...)
{
char line[MAX_LINE];
va_list ap;
va_start(ap, format);
int n = vsnprintf(line, MAX_LINE, format, ap);
va_end(ap);
if(n < 0 || n > MAX_LINE - 1) {return -1;}
if(ut_writefill(fd, line, n)) {return -1;}
return n;
}
void ut_drawBoard(void (*mvch)(void*, int, int, char), void *arg, const char *tiles, int offset, int stride, int iy, int ix, char winner, bool highlight)
{
// 7x7 board display
/*
* X|X|X
* -+-+-
@ -136,40 +186,145 @@ void ut_drawBoard(const char *tiles, int offset, int stride, int iy, int ix, cha
* -+-+-
* X|X|X
*/
for(int r = 0; r < 3; r++)
for(int r = 0; r < 3; r++) // tiles
for(int c = 0; c < 3; c++)
mvaddch(iy + 2 * r, ix + 2 * c, T(r, c) != '\0' ? T(r, c) : ' ');
mvch(arg, iy + 2 * r + 1, ix + 2 * c + 1, T(r, c) != '\0' ? T(r, c) : ' ');
//char info = winner != '\0' ? winner : highlight ? '*' : ' ';
for(int r = 0; r < 3; r++)
for(int r = 0; r < 3; r++) // |
for(int c = 0; c < 2; c++)
mvaddch(iy + 2 * r, ix + 2 * c + 1, winner != '\0' ? winner : '|');
for(int r = 0; r < 2; r++)
mvch(arg, iy + 2 * r + 1, ix + 2 * c + 1 + 1, '|');
for(int r = 0; r < 2; r++) // -
for(int c = 0; c < 3; c++)
mvaddch(iy + 2 * r + 1, ix + 2 * c, winner != '\0' ? winner : '-');
for(int r = 0; r < 2; r++)
mvch(arg, iy + 2 * r + 1 + 1, ix + 2 * c + 1, '-');
for(int r = 0; r < 2; r++) // +
for(int c = 0; c < 2; c++)
mvaddch(iy + 2 * r + 1, ix + 2 * c + 1, winner != '\0' ? winner : highlight ? '*' : '+');
mvch(arg, iy + 2 * r + 1 + 1, ix + 2 * c + 1 + 1, highlight ? '*' : '+');
for(int r = 0; r < 7; r++) // | boundary
for(int c = 0; c < 2; c++)
mvch(arg, iy + r, ix + 6 * c, winner != '\0' ? winner : ' ');
for(int r = 0; r < 2; r++) // - boundary
for(int c = 0; c < 7; c++)
mvch(arg, iy + 6 * r, ix + c, winner != '\0' ? winner : ' ');
}
void ut_draw(const struct ut_state *state)
#define DTILES_Y 21
#define DTILES_X 21
void ut_drawTiles(void (*mvch)(void*, int, int, char), void *arg, const struct ut_state *state, bool numbers, int lr, int lc)
{
for(int r = 0; r < 3; r++)
for(int c = 0; c < 3; c++)
ut_drawBoard((char *)state->tiles,
27 * r + 3 * c, 9,
7 * r + 1, 7 * c + 1,
state->boards[r][c],
state->playBoard == -1 || state->playBoard == 3 * r + c);
//refresh();
ut_drawBoard(mvch, arg, (char *)state->tiles,
27 * r + 3 * c, 9,
7 * r, 7 * c,
state->boards[r][c],
state->boards[r][c] == '\0' && (state->playBoard == -1 || state->playBoard == 3 * r + c));
if(numbers)
{
for(int b = 0; b < 3; b++)
for(int c = 0; c < 3; c++)
mvch(arg, 0, 7 * b + 2 * c + 1, '0' + 3 * b + c);
for(int b = 0; b < 3; b++)
for(int r = 0; r < 3; r++)
mvch(arg, 7 * b + 2 * r + 1, 0, '0' + 3 * b + r);
}
if(lr >= 0 && lr < 9 && lc >= 0 && lc < 9)
mvch(arg, 7 * (lr / 3) + 2 * (lr % 3) + 1,
7 * (lc / 3) + 2 * (lc % 3) + 1,
state->tiles[lr][lc] == 'X' ? '%' : '0');
}
#define DBOARDS_Y 7
#define DBOARDS_X 7
void ut_drawBoards(void (*mvch)(void*, int, int, char), void *arg, const struct ut_state *state)
{
ut_drawBoard(mvch, arg, (char *)state->boards,
0, 3,
0, 0,
'\0', false);
}
int ut_click(const struct ut_state *state, int *r, int *c)
void ut_cursmvchc(void *arg, int y, int x, char c)
{
bool colorized;
if(colorized = has_colors())
{
color_set((c == 'X' || c == '%') ? 1 :
(c == 'O' || c == '0') ? 2 : 0, NULL);
}
bool bold;
if(c == '%' || c == '0')
{
bold = true;
attr_on(A_BOLD, NULL);
}
mvaddch(curs_line + y, x, c);
if(bold) {attr_off(A_BOLD, NULL);}
if(colorized) {color_set(0, NULL);}
}
void ut_tmvch(void *arg, int y, int x, char c)
{
((char (*)[DTILES_X])arg)[y][x] = c;
}
void ut_bmvch(void *arg, int y, int x, char c)
{
((char (*)[DBOARDS_X])arg)[y][x] = c;
}
int dtiles_line;
void ut_cursdraw(const struct ut_state *state)
{
ut_cursprintf("Turn: %c Play board: %s\n", (int)state->player, PLAY_BOARDS[(state->playBoard + 10) % 10]);
dtiles_line = curs_line;
ut_drawTiles(ut_cursmvchc, NULL, state, false, -1, -1); // TODO some kind of move history to know the last move
curs_line += DTILES_Y;
//ut_drawBoards(ut_cursmvchc, NULL, state);
}
void ut_sockdraw(const struct ut_state *state, int fd)
{
ut_dprintf(fd, "Turn: %c\nPlay board: %s\n", (int)state->player, PLAY_BOARDS[(state->playBoard + 10) % 10]);
char dtiles[DTILES_Y][DTILES_X];
ut_drawTiles(ut_tmvch, dtiles, state, true, -1, -1);
for(int r = 0; r < DTILES_Y; r++)
{
ut_dprintf(fd, "%.*s\n", DTILES_X, dtiles[r]);
}
}
/*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] ? state->boards[j][i] : ' ');
}
putchar('\n');
}
}*/
int ut_readfill(int fd, char *x, size_t l)
{
while(l > 0)
{
int r = read(fd, x, l);
if(r < 0)
{
//printf("ut_readfill read:%d errno=%d\n", r, errno);
if(errno == EAGAIN || errno == EWOULDBLOCK || errno == EINTR) {continue;}
else {return 1;}
}
else if(r == 0) {return 2;}
x += r; l -= r;
}
return 0;
}
int ut_cursgetpos(const struct ut_state *state, int *r, int *c)
{
#define P(r, c, d) ((state->playBoard == -1 || state->playBoard == 3 * (r / 3) + (c / 3)) && \
(state->boards[r / 3][c / 3] == '\0' || d % 3 == 1))
MEVENT event;
mvaddstr(21, 0, "Select move with arrow keys or mouse.");
if(state->playBoard == -1)
ut_cursprintf("Select move with arrow keys or mouse.\n");
if(*r < 0 || *r >= 9 || *c < 0 || *c >= 9)
{
*r = 4; *c = 4;
}
else if(state->playBoard == -1)
{
*r = 3 * (*r / 3) + 1;
*c = 3 * (*c / 3) + 1;
@ -186,7 +341,7 @@ int ut_click(const struct ut_state *state, int *r, int *c)
*r = 3 * (*r / 3) + 1;
*c = 3 * (*c / 3) + 1;
}*/
move((7 * (*r / 3) + 1) + (2 * (*r % 3)),
move(dtiles_line + (7 * (*r / 3) + 1) + (2 * (*r % 3)),
(7 * (*c / 3) + 1) + (2 * (*c % 3)));
refresh();
switch(getch())
@ -194,6 +349,7 @@ int ut_click(const struct ut_state *state, int *r, int *c)
case KEY_MOUSE:
if(getmouse(&event) == OK && (event.bstate & BUTTON1_CLICKED))
{
event.y -= dtiles_line;
if(event.y < 0 || event.y >= 21) {break;}
if(event.x < 0 || event.x >= 21) {break;}
*r = (event.y % 7 - 1) / 2 + 3 * (event.y / 7);
@ -255,122 +411,7 @@ int ut_click(const struct ut_state *state, int *r, int *c)
#undef P
}
int info_line; // set this to 21 and reset it on erase TODO
void ut_info(const char *str, int n)
{
mvaddnstr(info_line, 0, str, n);
// info_line++; // depends on str not containing newlines
int y, x;
getyx(stdscr, y, x);
info_line = y + 1;
}
int ut_readfill(int fd, char *x, size_t l)
{
while(l > 0)
{
int r = read(fd, x, l);
if(r < 0)
{
if(errno == EAGAIN || errno == EWOULDBLOCK || errno == EINTR) {continue;}
else {return 1;}
}
else if(r == 0) {return 2;}
x += r; l -= r;
}
return 0;
}
int ut_writefill(int fd, const char *x, size_t l)
{
while(l > 0)
{
int r = write(fd, x, l);
if(r < 0)
{
if(errno == EAGAIN || errno == EWOULDBLOCK || errno == EINTR) {continue;}
else {return 1;}
}
x += r; l -= r;
}
return 0;
}
#define MAX_LINE 128
int ut_dprintf(int fd, const char *restrict format, ...)
{
char line[MAX_LINE];
va_list ap;
va_start(ap, format);
int n = vsnprintf(line, MAX_LINE, format, ap);
va_end(ap);
if(n < 0 || n > MAX_LINE - 1) {return -1;}
if(ut_writefill(fd, line, n)) {return -1;}
return n;
}
int ut_cursprintf(const char *restrict format, ...)
{
char line[MAX_LINE];
va_list ap;
va_start(ap, format);
int n = vsnprintf(line, MAX_LINE, format, ap);
va_end(ap);
if(n < 0 || n > MAX_LINE - 1) {return -1;}
ut_info(line, n);
return n;
}
void ut_show(const struct ut_state *state, int fd, bool as_comment) {
#define tiles state->tiles
ut_dprintf(fd, "%sTurn: %c\n%sPlay board: %d\n", as_comment ? "| " : "", (int)state->player, as_comment ? "| " : "", 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;
}
ut_dprintf(fd, "%s 012 345 678\n", as_comment ? "| " : "");
for(int y = 0; y < 9; y++)
{
if (y == 3 || y == 6) {
ut_dprintf(fd, "%s ---+---+---\n", as_comment ? "| " : "");
}
ut_dprintf(fd, "%s%d ", as_comment ? "| " : "", y);
for(int x = 0; x < 9; x++)
{
if (x == 3 || x == 6) {
ut_writefill(fd, "|", 1);
}
ut_writefill(fd, tiles[y][x] ? &tiles[y][x] : " ", 1);
}
if (y / 3 == play_board_row || play_board_row == -1) {
ut_writefill(fd, "<", 1);
}
ut_writefill(fd, "\n", 1);
}
if (play_board_col == -1) {
ut_dprintf(fd, "%s ^^^ ^^^ ^^^", as_comment ? "| " : "");
} else {
ut_dprintf(fd, "%s ", as_comment ? "| " : "");
for (int i = 0; i < play_board_col; i++) {
ut_dprintf(fd, " ");
}
ut_dprintf(fd, "^^^");
}
ut_writefill(fd, "\n", 1);
#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] ? state->boards[j][i] : ' ');
}
putchar('\n');
}
}
bool getpos(const struct ut_state *state, int *x, int *y) {
/*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};
@ -399,199 +440,138 @@ bool getpos(const struct ut_state *state, int *x, int *y) {
break;
}
return true;
}
void getpos_net(const struct ut_state *state, int sock, int *x, int *y) { // TODO
const int MAX_LINE_LEN = 120;
char byte;
int n;
while (true) {
n = recv(sock, &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(sock, &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(sock);
exit(1);
}
} else {
if (byte == '\n') {
printf("Partner ended line before coords were complete - retrying\n");
ut_dprintf(sock, "| Invalid coordinates. Try again: ");
continue;
}
*x = byte - 0x30;
n = recv(sock, &byte, 1, 0);
if (n == 0) {goto conn_closed;}
if (byte == '\n') {
printf("Partner ended line before coords were complete - retrying\n");
ut_dprintf(sock, "| Invalid coordinates. Try again: ");
continue;
}
n = recv(sock, &byte, 1, 0);
if (n == 0) {goto conn_closed;}
if (byte == '\n') {
printf("Partner ended line before coords were complete - retrying\n");
ut_dprintf(sock, "| Invalid coordinates. Try again: ");
continue;
}
*y = byte - 0x30;
n = recv(sock, &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");
ut_dprintf(sock, "| Invalid coordinates. Try again: ");
// ignore the rest of this line
for (int i = 0; i < MAX_LINE_LEN; i++) {
n = recv(sock, &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(sock);
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)
{
// putchar('\n');
endwin();
// other cleanup
exit(0);
}
/*int main(int argc, char *argv[]) {
signal(SIGINT, finish);
initscr();
keypad(stdscr, TRUE);
nonl(); // \r instead of \r\n
cbreak();
noecho();
mousemask(BUTTON1_CLICKED, NULL);
struct ut_state state = ut_initial;
while (true) {
int r, c, err;
erase();
ut_draw(&state);
char w = ut_winner((char *)state.boards, 0, 3);
if(w)
{
if(w == ' ')
{
mvaddstr(21, 0, "Draw!");
}
else
{
mvaddch(21, 0, w);
mvaddstr(21, 1, " Wins!");
}
mvaddstr(22, 0, "Press any key to exit.");
refresh();
mousemask(0, NULL);
getch();
break;
}
err = ut_click(&state, &r, &c);
if (err) {continue;}
err = ut_move(&state, &state, r, c);
if (err) {continue;}
}
finish(0);
return 0;
}*/
int ut_local_game(struct ut_state *state) { // TODO replace with from ncurses main
int x, y;
int ut_ignore_line(int sock) {
char byte;
for (int i = 0; i < 128; i++) {
if (ut_readfill(sock, &byte, 1)) {
return 1;
}
if (byte == '\n') {return 0;}
}
return 2;
}
int ut_sockgetpos(const struct ut_state *state, int sock, int *y, int *x, bool readable) { // TODO
int index = 0;
char byte;
while (true) {
ut_show(state, 1, false);
ut_show_boards(state);
bool ok = getpos(state, &x, &y);
if (!ok) {continue;}
int err = ut_move(state, state, y, x);
if (err) {continue;}
if (ut_readfill(sock, &byte, 1)) {return 1;}
// allow \r\n
if (index == 3 && byte == '\r') {
if (ut_readfill(sock, &byte, 1)) {return 1;}
}
if (index == 0 && byte == '|') {
if (ut_ignore_line(sock)) {return -1;}
} else if (index == 0) {
*x = byte - 0x30;
} else if (index == 2) {
*y = byte - 0x30;
} else if (index == 1 && byte != ',' || index == 3 && byte != '\n') {
ut_cursprintf("Partner sent malformed coords - retrying\n");
if (ut_ignore_line(sock)) {return 2;}
if (readable) {
ut_dprintf(sock, "Invalid coordinates. Try again: ");
}
index = 0;
continue;
}
index++;
if (index == 4) {break;}
}
//printf("x=%d y=%d\n", *x, *y);
return 0;
}
int ut_local_game(struct ut_state *state) {
for(;;)
{
int r = 4, c = 4;
ut_curserase();
ut_cursdraw(state);
char w = ut_winner((char *)state->boards, 0, 3);
if(w)
{
if(w == ' ')
{
printf("\nDraw!\n");
ut_cursprintf("Draw!\n");
}
else
{
printf("\n%c wins!\n", (int)w);
ut_cursprintf("%c wins!\n", (int)w);
}
break;
}
if(ut_cursgetpos(state, &r, &c)) {continue;}
if(ut_move(state, state, r, c)) {continue;}
}
return 0;
}
int ut_network_game(struct ut_state *state, int sock, char player) { // TODO
int x, y;
bool host = player == state->host_player;
printf("You play as %c.\n", player);
int ut_network_game(struct ut_state *state, int sock, char player, bool readable) { // TODO
while (true) {
ut_show(state, 1, false);
ut_show_boards(state);
ut_show(state, sock, true);
if (state->player == player) {
if (host) {ut_dprintf(sock, "| Waiting for game partner ...\n");}
bool ok = getpos(state, &x, &y);
if (!ok) {continue;}
ut_dprintf(sock, "%d,%d\n", x, y);
} else {
printf("Waiting for game partner ...\n");
if (host) {
ut_dprintf(sock, "|\n"); // aligns game board each turn
ut_dprintf(sock, "| Place token %c in position x,y: ", ut_turn(player));
}
getpos_net(state, sock, &x, &y);
}
int err = ut_move(state, state, y, x);
if (err) {continue;}
ut_dprintf(sock, "|\n");
int r = 4, c = 4;
ut_curserase();
ut_cursdraw(state);
if (readable) ut_sockdraw(state, sock);
char w = ut_winner((char *)state->boards, 0, 3);
if(w)
{
if(w == ' ')
{
printf("\nDraw!\n");
ut_cursprintf("Draw!\n");
if (readable) {ut_dprintf(sock, "\nDraw!\n");}
close(sock);
}
else
{
printf("\n%c wins!\n", (int)w);
ut_cursprintf("%c wins!\n", (int)w);
if (readable) {ut_dprintf(sock, "\n%c wins!\n", (int)w);}
close(sock);
}
break;
}
if (state->player == player) {
if (readable) {ut_dprintf(sock, "Waiting for game partner ...\n");}
if(ut_cursgetpos(state, &r, &c)) {continue;}
ut_dprintf(sock, "%d,%d\n", c, r);
} else {
ut_cursprintf("Waiting for game partner ...\n");
refresh();
if (readable) {
// line feed aligns game board each turn
ut_dprintf(sock, "\nPlace token %c in position x,y: ", ut_turn(player));
}
int err = ut_sockgetpos(state, sock, &r, &c, readable);
ut_cursprintf("received row=%d col=%d\n", r, c);
if (err == 1) {
ut_cursprintf("Connection closed.\n");
return 1;
}
else if (err == 2) {
ut_cursprintf("Partner sent too much data - exiting\n");
return 1;
}
}
if (ut_move(state, state, r, c)) {continue;}
if (readable) {ut_dprintf(sock, "\n");}
}
return 0;
}
char ut_random_player(void) {
char player;
int random = open("/dev/urandom", O_RDONLY);
if (read(random, &player, 1) < 0) {return -1;}
player = (player % 2 == 0) ? 'X' : 'O';
return player;
}
int ut_host_game(struct ut_state *state) {
int sock = socket(AF_INET, SOCK_STREAM, 0);
if (sock == -1) {
int errsv = errno;
printf("error %d\n", errsv);
printf("error %d\n", errno);
return 1;
}
const struct sockaddr_in addr = {
@ -601,37 +581,56 @@ int ut_host_game(struct ut_state *state) {
};
setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &(int){1}, sizeof(int));
if (bind(sock, (struct sockaddr*)&addr, sizeof(addr)) == -1) {
int errsv = errno;
printf("error %d\n", errsv);
printf("error %d\n", errno);
return 1;
}
if (listen(sock, 128) == -1) {
int errsv = errno;
printf("error %d\n", errsv);
printf("error %d\n", errno);
return 1;
}
printf("Waiting for game partner at 127.0.0.1:6669 ...\n");
ut_cursprintf("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);
printf("error %d\n", errno);
return 1;
}
// decide X or O
if (ut_random_player(&state->host_player) != 0) {return 1;}
char player;
if ((player = ut_random_player()) < 0) {return 1;}
// check to determine readable
char byte;
ut_dprintf(conn, "| Press enter to start.\n");
if (ut_readfill(conn, &byte, 1)) {
printf("Connection closed.\n");
return 1;
}
bool readable = byte != '\0';
if (byte != '\n') {
int err = ut_ignore_line(conn);
if (err == 1) {
printf("Connection closed.\n");
return 1;
} else if (err == 2) {
printf("Partner sent too much data - exiting\n");
return 1;
}
}
// tell partner X or O
ut_dprintf(conn, "%c\n", ut_turn(state->host_player)); // TODO
ut_dprintf(conn, "%c\n", ut_turn(player));
if (readable) {
ut_dprintf(conn, "You play as: %c\n\n", ut_turn(player));
}
return ut_network_game(state, conn, state->host_player);
return ut_network_game(state, conn, player, readable);
}
int ut_join_game(struct ut_state *state) {
int sock = socket(AF_INET, SOCK_STREAM, 0);
if (sock == -1) {
int errsv = errno;
printf("error %d\n", errsv);
printf("error %d\n", errno);
return 1;
}
const struct sockaddr_in addr = {
@ -639,33 +638,87 @@ int ut_join_game(struct ut_state *state) {
.sin_port = htons(6669),
.sin_addr = { .s_addr = htonl(0x7f000001) },
};
printf("Connecting to game host at 127.0.0.1:6669 ...\n");
connect(sock, (struct sockaddr*)&addr, sizeof(addr));
ut_cursprintf("Connecting to game host at 127.0.0.1:6669 ...\n");
if(connect(sock, (struct sockaddr*)&addr, sizeof(addr)) == -1)
{
printf("error %d\n", errno);
return 1;
}
// tell host to deactivate readable
ut_writefill(sock, "\0\n", 2);
// host decides X or O
char player;
int r = ut_readfill(sock, &player, 1);
if (r == 2) {printf("Connection closed.\n");}
if (r != 0) {return 1;}
state->host_player = ut_turn(player);
char player_buf[2];
char *player = player_buf;
for (int i = 0; i < 10; i++) {
int r = ut_readfill(sock, player_buf, 1);
ut_ignore_line(sock);
if (*player == '|') {
continue;
}
if (r == 2) {printf("Connection closed.\n");}
if (r != 0) {return 1;}
break;
}
if (*player != 'X' && *player != 'O') {
printf("Host is retarded.\n");
return 1;
}
return ut_network_game(state, sock, player);
return ut_network_game(state, sock, *player, false);
}
void finish(int sig)
{
// putchar('\n');
endwin();
// other cleanup
exit(0);
}
void waitfinish(int sig)
{
ut_cursprintf("\nPress any key to exit.");
refresh();
mousemask(0, NULL);
getch();
finish(sig);
}
void begin(void)
{
signal(SIGINT, finish);
initscr();
keypad(stdscr, TRUE);
nonl(); // \r instead of \r\n
cbreak();
noecho();
mousemask(BUTTON1_CLICKED, NULL);
if (has_colors())
{
start_color();
init_pair(1, COLOR_RED, COLOR_BLACK);
init_pair(2, COLOR_BLUE, COLOR_BLACK);
}
}
int main(int argc, char **argv) {
signal(SIGINT, finish);
struct ut_state state = ut_initial;
if (argc != 2) {
printf(HELP_TEXT);
return 1;
} else if (strncmp(argv[1], arg_local, strlen(arg_local)) == 0) {
return ut_local_game(&state);
} else if (strncmp(argv[1], arg_host, strlen(arg_host)) == 0) {
return ut_host_game(&state);
} else if (strncmp(argv[1], arg_join, strlen(arg_join)) == 0) {
return ut_join_game(&state);
} else if (strncmp(argv[1], arg_local, sizeof(arg_local)) == 0) {
begin();
waitfinish(ut_local_game(&state));
} else if (strncmp(argv[1], arg_host, sizeof(arg_host)) == 0) {
begin();
waitfinish(ut_host_game(&state));
} else if (strncmp(argv[1], arg_join, sizeof(arg_join)) == 0) {
begin();
waitfinish(ut_join_game(&state));
} else {
printf(HELP_TEXT, argv[1]);
printf(HELP_TEXT);
return 1;
}
return 0;
}