wait for key before exiting ncurses and more
このコミットが含まれているのは:
コミット
046feddf0b
45
uttt.c
45
uttt.c
|
@ -552,7 +552,8 @@ int ut_sockgetpos(const struct ut_state *state, struct ut_socket_buffer *sockbuf
|
|||
|
||||
int ut_local_game(struct ut_state *state) {
|
||||
int x, y;
|
||||
while (true) {
|
||||
for(;;)
|
||||
{
|
||||
int r, c, err;
|
||||
ut_curserase();
|
||||
ut_cursdraw(state);
|
||||
|
@ -567,10 +568,6 @@ int ut_local_game(struct ut_state *state) {
|
|||
{
|
||||
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);
|
||||
|
@ -581,22 +578,21 @@ int ut_local_game(struct ut_state *state) {
|
|||
return 0;
|
||||
}
|
||||
|
||||
int ut_network_game(struct ut_state *state, struct ut_socket_buffer *sockbuf, char player) { // TODO
|
||||
int ut_network_game(struct ut_state *state, struct ut_socket_buffer *sockbuf, char player, bool readable) { // TODO
|
||||
int x, y;
|
||||
bool host = player == state->host_player;
|
||||
printf("You play as %c.\n", player);
|
||||
//printf("You play as %c.\n", player);
|
||||
while (true) {
|
||||
ut_show(state, 1, false);
|
||||
ut_show_boards(state);
|
||||
ut_show(state, sockbuf->fd, true);
|
||||
if (state->player == player) {
|
||||
if (host) {ut_dprintf(sockbuf->fd, "| Waiting for game partner ...\n");}
|
||||
if (readable) {ut_dprintf(sockbuf->fd, "| Waiting for game partner ...\n");}
|
||||
bool ok = getpos(state, &x, &y);
|
||||
if (!ok) {continue;}
|
||||
ut_dprintf(sockbuf->fd, "%d,%d\n", x, y);
|
||||
} else {
|
||||
printf("Waiting for game partner ...\n");
|
||||
if (host) {
|
||||
if (readable) {
|
||||
ut_dprintf(sockbuf->fd, "|\n"); // aligns game board each turn
|
||||
ut_dprintf(sockbuf->fd, "| Place token %c in position x,y: ", ut_turn(player));
|
||||
}
|
||||
|
@ -621,13 +617,13 @@ int ut_network_game(struct ut_state *state, struct ut_socket_buffer *sockbuf, ch
|
|||
if(w == ' ')
|
||||
{
|
||||
printf("\nDraw!\n");
|
||||
if (host) {ut_dprintf(sockbuf->fd, "|\n|Draw!\n");}
|
||||
if (readable) {ut_dprintf(sockbuf->fd, "|\n|Draw!\n");}
|
||||
close(sockbuf->fd);
|
||||
}
|
||||
else
|
||||
{
|
||||
printf("\n%c wins!\n", (int)w);
|
||||
if (host) {ut_dprintf(sockbuf->fd, "|\n|%c wins!\n", (int)w);}
|
||||
if (readable) {ut_dprintf(sockbuf->fd, "|\n|%c wins!\n", (int)w);}
|
||||
close(sockbuf->fd);
|
||||
}
|
||||
break;
|
||||
|
@ -673,10 +669,13 @@ int ut_host_game(struct ut_state *state) {
|
|||
}
|
||||
|
||||
// decide X or O
|
||||
if ((state->host_player = ut_random_player()) < 0) {return 1;}
|
||||
char player;
|
||||
if ((player = ut_random_player()) < 0) {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));
|
||||
|
||||
// TODO check to determine readable
|
||||
|
||||
struct ut_socket_buffer connbuf = {
|
||||
.fd = conn,
|
||||
|
@ -685,7 +684,7 @@ int ut_host_game(struct ut_state *state) {
|
|||
.start = 0,
|
||||
.stop = 0,
|
||||
};
|
||||
return ut_network_game(state, &connbuf, state->host_player);
|
||||
return ut_network_game(state, &connbuf, player);
|
||||
}
|
||||
|
||||
int ut_join_game(struct ut_state *state) {
|
||||
|
@ -714,6 +713,8 @@ int ut_join_game(struct ut_state *state) {
|
|||
if (r != 0) {return 1;}
|
||||
state->host_player = ut_turn(*player);
|
||||
|
||||
// TODO send '\0' to deactivate readable
|
||||
|
||||
struct ut_socket_buffer sockbuf = {
|
||||
.fd = sock,
|
||||
.buf = {0},
|
||||
|
@ -731,6 +732,14 @@ void finish(int sig)
|
|||
// 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);
|
||||
|
@ -791,13 +800,13 @@ int main(int argc, char **argv) {
|
|||
return 1;
|
||||
} else if (strncmp(argv[1], arg_local, sizeof(arg_local)) == 0) {
|
||||
begin();
|
||||
finish(ut_local_game(&state));
|
||||
waitfinish(ut_local_game(&state));
|
||||
} else if (strncmp(argv[1], arg_host, sizeof(arg_host)) == 0) {
|
||||
begin();
|
||||
finish(ut_host_game(&state));
|
||||
waitfinish(ut_host_game(&state));
|
||||
} else if (strncmp(argv[1], arg_join, sizeof(arg_join)) == 0) {
|
||||
begin();
|
||||
finish(ut_join_game(&state));
|
||||
waitfinish(ut_join_game(&state));
|
||||
} else {
|
||||
printf(HELP_TEXT);
|
||||
return 1;
|
||||
|
|
読み込み中…
新しいイシューから参照