fixing and host_player TODO

このコミットが含まれているのは:
woosh 2023-11-15 05:26:20 +00:00
コミット 4409c01211
1個のファイルの変更54行の追加36行の削除

90
uttt.c
ファイルの表示

@ -127,26 +127,19 @@ int ut_move(struct ut_state *new_state, const struct ut_state *old_state, int ro
}
int curs_line; // set this to 21 and reset it on erase TODO - use this as an offset rather
int curs_line;
void ut_curserase(void)
{
curs_line = 0;
erase();
}
void ut_cursprint(const char *str, int n)
{
mvaddnstr(info_line, 0, str, n);
mvaddnstr(curs_line, 0, str, n);
int y, x;
getyx(stdscr, y, x);
info_line = y;
curs_line = y;
}
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_writefill(int fd, const char *x, size_t l)
{
while(l > 0)
@ -162,6 +155,17 @@ int ut_writefill(int fd, const char *x, size_t l)
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];
@ -207,7 +211,7 @@ void ut_drawBoard(void (*mvch)(void*, int, int, char), void *arg, const char *ti
}
#define DTILES_Y 21
#define DTILES_X 21
void ut_drawTiles(void (*mvch)(void*, int, int, char), void *arg, const struct ut_state *state)
void ut_drawTiles(void (*mvch)(void*, int, int, char), void *arg, const struct ut_state *state, bool numbers)
{
for(int r = 0; r < 3; r++)
for(int c = 0; c < 3; c++)
@ -216,6 +220,15 @@ void ut_drawTiles(void (*mvch)(void*, int, int, char), void *arg, const struct u
7 * r + 1, 7 * c + 1,
state->boards[r][c],
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);
}
}
#define DBOARDS_Y 7
#define DBOARDS_X 7
@ -242,8 +255,8 @@ void ut_bmvch(void *arg, int y, int x, char c)
void ut_cursdraw(const struct ut_state *state)
{
ut_cursprintf(fd, "Turn: %c Play board: %d\n", (int)state->player, state->playBoard);
ut_drawTiles(ut_cursmvch, NULL, state);
ut_cursprintf("Turn: %c Play board: %d\n", (int)state->player, state->playBoard);
ut_drawTiles(ut_cursmvch, NULL, state, false);
curs_line += DTILES_Y;
//ut_drawBoards(ut_cursmvch, NULL, state);
@ -252,7 +265,7 @@ void ut_sockdraw(const struct ut_state *state, int fd)
{
ut_dprintf(fd, "| Turn: %c Play board: %d\n", (int)state->player, state->playBoard);
char dtiles[DTILES_Y][DTILES_X];
ut_drawTiles(ut_tmvch, dtiles, state);
ut_drawTiles(ut_tmvch, dtiles, state, true);
for(int r = 0; r < DTILES_Y; r++)
{
ut_dprintf(fd, "| %.*s\n", DTILES_X, dtiles[r]);
@ -390,7 +403,7 @@ int ut_readline(struct ut_socket_buffer *sockbuf, char *line, size_t len) {
return nb + ns;
}
int ut_click(const struct ut_state *state, int *r, int *c)
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))
@ -537,28 +550,33 @@ int getpos_net(const struct ut_state *state, struct ut_socket_buffer *sockbuf, i
}
int ut_local_game(struct ut_state *state) { // TODO replace with from ncurses main
int ut_local_game(struct ut_state *state) {
int x, y;
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;}
int r, c, err;
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);
}
ut_cursprintf("Press any key to exit.");
refresh();
mousemask(0, NULL);
getch();
break;
}
err = ut_cursgetpos(state, &r, &c);
if (err) {continue;}
err = ut_move(state, state, r, c);
if (err) {continue;}
}
return 0;
}
@ -707,6 +725,13 @@ int ut_join_game(struct ut_state *state) {
return ut_network_game(state, &sockbuf, *player);
}
void finish(int sig)
{
// putchar('\n');
endwin();
// other cleanup
exit(0);
}
void begin(void)
{
signal(SIGINT, finish);
@ -717,13 +742,6 @@ void begin(void)
noecho();
mousemask(BUTTON1_CLICKED, NULL);
}
void finish(int sig)
{
// putchar('\n');
endwin();
// other cleanup
exit(0);
}
/*int main(int argc, char *argv[]) {
signal(SIGINT, finish);