progress
このコミットが含まれているのは:
コミット
a4656c7adc
79
uttt.c
79
uttt.c
|
@ -10,6 +10,9 @@
|
|||
#include <arpa/inet.h>
|
||||
#include <string.h>
|
||||
#include <errno.h>
|
||||
#include <sys/stat.h>
|
||||
#include <fcntl.h>
|
||||
#include <unistd.h>
|
||||
|
||||
struct ut_state
|
||||
{
|
||||
|
@ -142,6 +145,15 @@ void ut_show(const struct ut_state *state) {
|
|||
#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]);
|
||||
}
|
||||
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);
|
||||
|
@ -173,6 +185,10 @@ bool getpos(const struct ut_state *state, int *x, int *y) {
|
|||
return true;
|
||||
}
|
||||
|
||||
bool getpos_net(const struct ut_state *state, int conn, int *x, int *y) {
|
||||
//recv(conn, )
|
||||
}
|
||||
|
||||
void finish(int sig)
|
||||
{
|
||||
putchar('\n');
|
||||
|
@ -182,16 +198,11 @@ void finish(int sig)
|
|||
}
|
||||
|
||||
void ut_local_game(struct ut_state *state) {
|
||||
int x, y;
|
||||
while (true) {
|
||||
int x, y;
|
||||
ut_show(state);
|
||||
for (int j = 0; j < 3; j++) {
|
||||
for (int i = 0; i < 3; i++) {
|
||||
putchar(state->boards[j][i]);
|
||||
}
|
||||
putchar('\n');
|
||||
}
|
||||
bool ok = getpos(state, &y, &x);
|
||||
ut_show_boards(state);
|
||||
bool ok = getpos(state, &x, &y);
|
||||
if (!ok) {continue;}
|
||||
int err = ut_move(state, state, x, y);
|
||||
if (err) {continue;}
|
||||
|
@ -210,8 +221,34 @@ void ut_local_game(struct ut_state *state) {
|
|||
}
|
||||
}
|
||||
|
||||
void ut_host_game(struct ut_state *state, int sock) {
|
||||
printf(":P\n");
|
||||
void ut_host_game(struct ut_state *state, int conn, char host_player) {
|
||||
int x, y;
|
||||
printf("You play as %c.\n", host_player);
|
||||
while (true) {
|
||||
ut_show(state);
|
||||
ut_show_boards(state);
|
||||
if (state->player == host_player) {
|
||||
bool ok = getpos(state, &x, &y);
|
||||
if (!ok) {continue;}
|
||||
int err = ut_move(state, state, x, y);
|
||||
if (err) {continue;}
|
||||
} else {
|
||||
printf("Waiting for game parter ...");
|
||||
getpos_net(state, conn, &x, &y);
|
||||
}
|
||||
char w = ut_winner((char *)state->boards, 0, 3);
|
||||
if(w)
|
||||
{
|
||||
if(w == ' ')
|
||||
{
|
||||
printf("\nDraw!\n");
|
||||
}
|
||||
else
|
||||
{
|
||||
printf("\n%c wins!\n", (int)w);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
int main(int argc, char **argv) {
|
||||
|
@ -231,16 +268,36 @@ int main(int argc, char **argv) {
|
|||
.sin_port = htons(6669),
|
||||
.sin_addr = { .s_addr = htonl(0x7f000001) },
|
||||
};
|
||||
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);
|
||||
return 1;
|
||||
}
|
||||
ut_host_game(&state, sock);
|
||||
if (listen(sock, 128) == -1) {
|
||||
int errsv = errno;
|
||||
printf("error %d\n", errsv);
|
||||
return 1;
|
||||
}
|
||||
printf("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);
|
||||
return 1;
|
||||
}
|
||||
int random = open("/dev/urandom", O_RDONLY);
|
||||
int byte;
|
||||
read(random, &byte, 1);
|
||||
//printf("%d\n", byte);
|
||||
char host_player = byte % 2 == 0 ? 'X' : 'O';
|
||||
//printf("%c\n", host_player);
|
||||
ut_host_game(&state, conn, host_player);
|
||||
} else if (strncmp(argv[1], arg_join, strlen(arg_join)) == 0) {
|
||||
printf("no ;)\n");
|
||||
} else {
|
||||
printf("%s\n", argv[1]);
|
||||
}
|
||||
return 0;
|
||||
|
||||
}
|
||||
|
|
読み込み中…
新しいイシューから参照