add fully functional multiplayer (jk)
check time since last commit, yes it took this long
このコミットが含まれているのは:
コミット
067c4eca20
59
uttt.c
59
uttt.c
|
@ -5,6 +5,11 @@
|
|||
#include <string.h>
|
||||
#include <curses.h>
|
||||
#include <signal.h>
|
||||
#include <sys/socket.h>
|
||||
#include <netinet/in.h>
|
||||
#include <arpa/inet.h>
|
||||
#include <string.h>
|
||||
#include <errno.h>
|
||||
|
||||
struct ut_state
|
||||
{
|
||||
|
@ -20,6 +25,8 @@ const struct ut_state ut_initial = {
|
|||
.player = 'X'
|
||||
};
|
||||
|
||||
const char *arg_host = "--host";
|
||||
const char *arg_join = "--join";
|
||||
|
||||
char ut_turn(char player)
|
||||
{
|
||||
|
@ -173,26 +180,21 @@ void finish(int sig)
|
|||
exit(0);
|
||||
}
|
||||
|
||||
int main(int *argc, char **argv) {
|
||||
signal(SIGINT, finish);
|
||||
/*initscr();
|
||||
keypad(stdscr, TRUE);*/
|
||||
|
||||
struct ut_state state = ut_initial;
|
||||
void ut_local_game(struct ut_state *state) {
|
||||
while (true) {
|
||||
int x, y;
|
||||
ut_show(&state);
|
||||
ut_show(state);
|
||||
for (int j = 0; j < 3; j++) {
|
||||
for (int i = 0; i < 3; i++) {
|
||||
putchar(state.boards[j][i]);
|
||||
putchar(state->boards[j][i]);
|
||||
}
|
||||
putchar('\n');
|
||||
}
|
||||
bool ok = getpos(&state, &y, &x);
|
||||
bool ok = getpos(state, &y, &x);
|
||||
if (!ok) {continue;}
|
||||
int err = ut_move(&state, &state, x, y);
|
||||
int err = ut_move(state, state, x, y);
|
||||
if (err) {continue;}
|
||||
char w = ut_winner((char *)state.boards, 0, 3);
|
||||
char w = ut_winner((char *)state->boards, 0, 3);
|
||||
if(w)
|
||||
{
|
||||
if(w == ' ')
|
||||
|
@ -203,8 +205,41 @@ int main(int *argc, char **argv) {
|
|||
{
|
||||
printf("\n%c wins!\n", (int)w);
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void ut_host_game(struct ut_state *state, int sock) {
|
||||
printf(":P\n");
|
||||
}
|
||||
|
||||
int main(int argc, char **argv) {
|
||||
signal(SIGINT, finish);
|
||||
struct ut_state state = ut_initial;
|
||||
if (argc < 2) {
|
||||
ut_local_game(&state);
|
||||
} else if (strncmp(argv[1], arg_host, strlen(arg_host)) == 0) {
|
||||
int sock = socket(AF_INET, SOCK_STREAM, 0);
|
||||
if (sock == -1) {
|
||||
int errsv = errno;
|
||||
printf("error %d\n", errsv);
|
||||
return 1;
|
||||
}
|
||||
const struct sockaddr_in addr = {
|
||||
.sin_family = AF_INET,
|
||||
.sin_port = htons(6669),
|
||||
.sin_addr = { .s_addr = htonl(0x7f000001) },
|
||||
};
|
||||
if (bind(sock, (struct sockaddr*)&addr, sizeof(addr)) == -1) {
|
||||
int errsv = errno;
|
||||
printf("error %d\n", errsv);
|
||||
return 1;
|
||||
}
|
||||
ut_host_game(&state, sock);
|
||||
} else if (strncmp(argv[1], arg_join, strlen(arg_join)) == 0) {
|
||||
printf("no ;)\n");
|
||||
} else {
|
||||
printf("%s\n", argv[1]);
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
|
読み込み中…
新しいイシューから参照