more moving fixing
このコミットが含まれているのは:
コミット
5b976d0c8b
285
uttt.c
285
uttt.c
|
@ -173,6 +173,7 @@ int ut_dprintf(int fd, const char *restrict format, ...)
|
|||
if(ut_writefill(fd, line, n)) {return -1;}
|
||||
return n;
|
||||
}
|
||||
|
||||
// TODO highlight last placed
|
||||
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)
|
||||
{
|
||||
|
@ -204,6 +205,8 @@ void ut_drawBoard(void (*mvch)(void*, int, int, char), void *arg, const char *ti
|
|||
for(int c = 0; c < 7; c++)
|
||||
mvch(arg, iy + 6 * r, ix + c, ' ');
|
||||
}
|
||||
#define DTILES_Y 21
|
||||
#define DTILES_X 21
|
||||
void ut_drawTiles(void (*mvch)(void*, int, int, char), void *arg, const struct ut_state *state)
|
||||
{
|
||||
for(int r = 0; r < 3; r++)
|
||||
|
@ -214,6 +217,8 @@ void ut_drawTiles(void (*mvch)(void*, int, int, char), void *arg, const struct u
|
|||
state->boards[r][c],
|
||||
state->playBoard == -1 || state->playBoard == 3 * r + c);
|
||||
}
|
||||
#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,
|
||||
|
@ -224,16 +229,12 @@ void ut_drawBoards(void (*mvch)(void*, int, int, char), void *arg, const struct
|
|||
|
||||
void ut_cursmvch(void *arg, int y, int x, char c)
|
||||
{
|
||||
mvaddch(/*curs_line + */y, x, c);
|
||||
mvaddch(curs_line + y, x, c);
|
||||
}
|
||||
#define DTILES_Y 21
|
||||
#define DTILES_X 21
|
||||
void ut_tmvch(void *arg, int y, int x, char c)
|
||||
{
|
||||
((char (*)[DTILES_X])arg)[y][x] = c;
|
||||
}
|
||||
#define DBOARDS_Y 7
|
||||
#define DBOARDS_X 7
|
||||
void ut_bmvch(void *arg, int y, int x, char c)
|
||||
{
|
||||
((char (*)[DBOARDS_X])arg)[y][x] = c;
|
||||
|
@ -243,6 +244,7 @@ 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);
|
||||
curs_line += DTILES_Y;
|
||||
//ut_drawBoards(ut_cursmvch, NULL, state);
|
||||
|
||||
}
|
||||
|
@ -257,6 +259,55 @@ void ut_sockdraw(const struct ut_state *state, int fd)
|
|||
}
|
||||
}
|
||||
|
||||
/*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');
|
||||
}
|
||||
}*/
|
||||
|
||||
|
||||
int ut_readfill(int fd, char *x, size_t l)
|
||||
|
@ -339,87 +390,6 @@ int ut_readline(struct ut_socket_buffer *sockbuf, char *line, size_t len) {
|
|||
return nb + ns;
|
||||
}
|
||||
|
||||
/*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) {
|
||||
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;
|
||||
break;
|
||||
}
|
||||
return true;
|
||||
}*/
|
||||
|
||||
int ut_click(const struct ut_state *state, int *r, int *c)
|
||||
{
|
||||
#define P(r, c, d) ((state->playBoard == -1 || state->playBoard == 3 * (r / 3) + (c / 3)) && \
|
||||
|
@ -512,6 +482,37 @@ int ut_click(const struct ut_state *state, int *r, int *c)
|
|||
#undef P
|
||||
}
|
||||
|
||||
/*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;
|
||||
break;
|
||||
}
|
||||
return true;
|
||||
}*/
|
||||
|
||||
int getpos_net(const struct ut_state *state, struct ut_socket_buffer *sockbuf, int *x, int *y) { // TODO
|
||||
const int MAX_LINE_LEN = 128;
|
||||
char byte;
|
||||
|
@ -535,54 +536,6 @@ int getpos_net(const struct ut_state *state, struct ut_socket_buffer *sockbuf, i
|
|||
|
||||
}
|
||||
|
||||
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;
|
||||
|
@ -754,12 +707,68 @@ 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);
|
||||
}
|
||||
|
||||
/*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 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;
|
||||
if (argc != 2) {
|
||||
printf(HELP_TEXT);
|
||||
return 1;
|
||||
finish(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) {
|
||||
|
@ -767,7 +776,9 @@ int main(int argc, char **argv) {
|
|||
} else if (strncmp(argv[1], arg_join, strlen(arg_join)) == 0) {
|
||||
return ut_join_game(&state);
|
||||
} else {
|
||||
printf(HELP_TEXT, argv[1]);
|
||||
return 1;
|
||||
printf(HELP_TEXT);
|
||||
finish(1);
|
||||
}
|
||||
finish(0);
|
||||
return 0;
|
||||
}
|
||||
|
|
読み込み中…
新しいイシューから参照