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
このコミットが含まれているのは:
コミット
031c466236
73
uttt.c
73
uttt.c
|
@ -444,16 +444,11 @@ int ut_ignore_line(int sock) {
|
|||
return 2;
|
||||
}
|
||||
|
||||
int ut_sockgetpos(const struct ut_state *state, int sock, int *x, int *y, bool readable) { // TODO
|
||||
int ut_sockgetpos(const struct ut_state *state, int sock, int *y, int *x, bool readable) { // TODO
|
||||
int index = 0;
|
||||
char byte;
|
||||
while (true) {
|
||||
int r = ut_readfill(sock, &byte, 1);
|
||||
if (r < 0) {return r;}
|
||||
if (r == 0) {
|
||||
printf("Connection closed.\n");
|
||||
return -1;
|
||||
}
|
||||
if (ut_readfill(sock, &byte, 1)) {return 1;}
|
||||
if (index == 0 && byte == '|') {
|
||||
if (ut_ignore_line(sock)) {return -1;}
|
||||
} else if (index == 0) {
|
||||
|
@ -461,8 +456,8 @@ int ut_sockgetpos(const struct ut_state *state, int sock, int *x, int *y, bool r
|
|||
} else if (index == 2) {
|
||||
*y = byte - 0x30;
|
||||
} else if (index == 1 && byte != ',' || index == 3 && byte != '\n') {
|
||||
printf("Partner sent malformed coords - retrying\n");
|
||||
if (ut_ignore_line(sock)) {return -1;}
|
||||
ut_cursprintf("Partner sent malformed coords - retrying\n");
|
||||
if (ut_ignore_line(sock)) {return 2;}
|
||||
if (readable) {
|
||||
ut_dprintf(sock, "Invalid coordinates. Try again: ");
|
||||
}
|
||||
|
@ -472,7 +467,7 @@ int ut_sockgetpos(const struct ut_state *state, int sock, int *x, int *y, bool r
|
|||
index++;
|
||||
if (index == 4) {break;}
|
||||
}
|
||||
printf("x=%d y=%d\n", *x, *y);
|
||||
//printf("x=%d y=%d\n", *x, *y);
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
@ -503,49 +498,51 @@ int ut_local_game(struct ut_state *state) {
|
|||
}
|
||||
|
||||
int ut_network_game(struct ut_state *state, int sock, char player, bool readable) { // TODO
|
||||
int x, y;
|
||||
//printf("You play as %c.\n", player);
|
||||
while (true) {
|
||||
//ut_show(state, 1, false);
|
||||
//ut_show_boards(state);
|
||||
int r = 4, c = 4;
|
||||
ut_curserase();
|
||||
ut_cursdraw(state);
|
||||
ut_sockdraw(state, sock);
|
||||
if (state->player == player) {
|
||||
if (readable) {ut_dprintf(sock, "Waiting for game partner ...\n");}
|
||||
bool ok = ut_cursgetpos(state, &x, &y);
|
||||
if (!ok) {continue;}
|
||||
ut_dprintf(sock, "%d,%d\n", x, y);
|
||||
} else {
|
||||
printf("Waiting for game partner ...\n");
|
||||
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, &x, &y, readable);
|
||||
if (err == -1) {
|
||||
printf("Connection closed.\n");
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
int err = ut_move(state, state, y, x);
|
||||
if (err) {continue;}
|
||||
ut_dprintf(sock, "\n");
|
||||
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", r, c);
|
||||
} else {
|
||||
ut_cursprintf("Waiting for game partner ...\n");
|
||||
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;}
|
||||
ut_dprintf(sock, "\n");
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
@ -579,7 +576,7 @@ int ut_host_game(struct ut_state *state) {
|
|||
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) {
|
||||
printf("error %d\n", errno);
|
||||
|
@ -629,7 +626,7 @@ 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");
|
||||
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);
|
||||
|
|
読み込み中…
新しいイシューから参照