From 0cb00f5b804d820ff252a7e9f3631aab1fb76aa0 Mon Sep 17 00:00:00 2001 From: Aaron Taylor Date: Thu, 27 May 2021 17:04:26 -0700 Subject: [PATCH] Two player mode now works with basic functionality. --- engine/interface.c | 2 +- interface/play_twoplayer.c | 81 ++++++++++++++++++++++---------------- 2 files changed, 47 insertions(+), 36 deletions(-) diff --git a/engine/interface.c b/engine/interface.c index e0ba5e4..faaa5d6 100644 --- a/engine/interface.c +++ b/engine/interface.c @@ -231,7 +231,7 @@ gameinfo_print(Gameinfo *gameinfo) printf("Move Number: %d\n", movenum); printf("To Move: %s\n", color_to_string(gameinfo->to_move)); - printf("Computer player: "); + printf("Opponent plays: "); if (gameinfo->computer_player == WHITE) printf("White\n"); else if (gameinfo->computer_player == BLACK) diff --git a/interface/play_twoplayer.c b/interface/play_twoplayer.c index f3774fb..2941e37 100644 --- a/interface/play_twoplayer.c +++ b/interface/play_twoplayer.c @@ -539,34 +539,51 @@ init_sgf(Gameinfo* ginfo) sgffile_recordboard(sgftree.root); } +/* + * Communication with second player. + */ + +static int +twoplayer_recv_move(int fd) +{ + int move; + printf("\nWaiting to receive move from other player...\n"); + if (read(fd, &move, sizeof(move)) != sizeof(int)) { + printf("\n"); + printf(WHITECOLOR " " RESETCOLOR); + printf("\n"); + printf(WHITECOLOR " " RESETCOLOR); + printf("Unrecoverable error. Save game if desired, then quit."); + printf(WHITECOLOR " " RESETCOLOR); + printf("\n"); + printf(WHITECOLOR " " RESETCOLOR); + printf("\n"); + printf("\n"); + move = NO_MOVE; + } + return move; +} + +static void +twoplayer_send_move(int move, int fd) +{ + write(fd, &move, sizeof(move)); +} + /* * Generate the computer move. */ static int -computer_move(Gameinfo* gameinfo, int* passes, int transmit_fifo_fd, int receive_fifo_fd) +computer_move(Gameinfo* gameinfo, int* passes, int receive_fifo_fd) { int move; - float move_value; - int resign; int resignation_declined = 0; float upper_bound, lower_bound; init_sgf(gameinfo); - /* Generate computer move. */ - if (autolevel_on) - adjust_level_offset(gameinfo->to_move); - move = genmove(gameinfo->to_move, &move_value, &resign); - if (resignation_allowed && resign) { - int state = twoplayer_endgame(gameinfo, 2); - if (state != -1) - return state; - - /* The opponent declined resignation. Remember not to resign again. */ - resignation_allowed = 0; - resignation_declined = 1; - } + move = twoplayer_recv_move(receive_fifo_fd); if (showscore) { gnugo_estimate_score(&upper_bound, &lower_bound); @@ -581,7 +598,6 @@ computer_move(Gameinfo* gameinfo, int* passes, int transmit_fifo_fd, int receive *passes = 0; gnugo_play_move(move, gameinfo->to_move); - sgffile_add_debuginfo(sgftree.lastnode, move_value); sgftreeAddPlay(&sgftree, gameinfo->to_move, I(move), J(move)); if (resignation_declined) sgftreeAddComment(&sgftree, "GNU Go resignation was declined"); @@ -611,6 +627,8 @@ do_move(Gameinfo* gameinfo, char* command, int* passes, int force, return 0; } + twoplayer_send_move(move,transmit_fifo_fd); + *passes = 0; TRACE("\nyour move: %1m\n\n", move); init_sgf(gameinfo); @@ -621,7 +639,6 @@ do_move(Gameinfo* gameinfo, char* command, int* passes, int force, if (opt_showboard) { twoplayer_showboard(); - printf("GNU Go is thinking...\n"); } if (force) { @@ -632,7 +649,7 @@ do_move(Gameinfo* gameinfo, char* command, int* passes, int force, } gameinfo->to_move = OTHER_COLOR(gameinfo->to_move); - return computer_move(gameinfo, passes, transmit_fifo_fd, receive_fifo_fd); + return computer_move(gameinfo, passes, receive_fifo_fd); } /* @@ -643,6 +660,8 @@ static int do_pass(Gameinfo* gameinfo, int* passes, int force, int transmit_fifo_fd, int receive_fifo_fd) { + twoplayer_send_move(NO_MOVE,transmit_fifo_fd); + (*passes)++; init_sgf(gameinfo); gnugo_play_move(PASS_MOVE, gameinfo->to_move); @@ -657,7 +676,7 @@ do_pass(Gameinfo* gameinfo, int* passes, int force, return 0; } - return computer_move(gameinfo, passes, transmit_fifo_fd, receive_fifo_fd); + return computer_move(gameinfo, passes, receive_fifo_fd); } /* @@ -669,26 +688,18 @@ void play_twoplayer(SGFTree* tree, Gameinfo* gameinfo, char* filename, char* unt int sz; int transmit_fifo_fd, receive_fifo_fd; - printf("DEBUG: Attempting to setup connection with other player.\n"); if (mknod(MASTER_FIFO_NAME, S_IFIFO | 0666, 0) == 0) { - printf("DEBUG: We are the master. Created %s.\n", MASTER_FIFO_NAME); gameinfo->computer_player = WHITE; // We are black, so our opponent is white. - printf("DEBUG: Waiting for other player to connect.\n"); + printf("Waiting for other player to connect.\n"); transmit_fifo_fd = open(MASTER_FIFO_NAME, O_WRONLY); - printf("DEBUG: Slave connected to our FIFO. Now we connect to the slave's FIFO.\n"); receive_fifo_fd = open(SLAVE_FIFO_NAME, O_RDONLY); - printf("DEBUG: Bidirectional communication established.\n"); } else { - printf("DEBUG: Failed to create master FIFO. Perhaps we are the slave?\n"); gameinfo->computer_player = BLACK; // We are white, so our opponent is black. if (mknod(SLAVE_FIFO_NAME, S_IFIFO | 0666, 0) == 0) { - printf("DEBUG: We are the slave. Created %s.\n", SLAVE_FIFO_NAME); receive_fifo_fd = open(MASTER_FIFO_NAME, O_RDONLY); - printf("DEBUG: We connected to master's FIFO. Now we wait for master's connection to our FIFO.\n"); transmit_fifo_fd = open(SLAVE_FIFO_NAME, O_WRONLY); - printf("DEBUG: Bidirectional communication established.\n"); } else { - printf("DEBUG: Failed to create slave FIFO. This is an unrecoverable error.\n"); + printf("ERROR: Failed to create both master and slave FIFO.\n"); exit(EXIT_FAILURE); } } @@ -696,8 +707,6 @@ void play_twoplayer(SGFTree* tree, Gameinfo* gameinfo, char* filename, char* unt // this is a best-effort situation for now. unlink(MASTER_FIFO_NAME); unlink(SLAVE_FIFO_NAME); -while(1){sleep(1);} -exit(EXIT_SUCCESS); setvbuf(stdout, (char*)NULL, _IONBF, 0); /* No buffering. */ @@ -765,7 +774,7 @@ void do_play_twoplayer(Gameinfo* gameinfo, int transmit_fifo_fd, int receive_fif /* Does the computer play first? If so, make a move. */ if (gameinfo->computer_player == gameinfo->to_move) - state = computer_move(gameinfo, &passes, transmit_fifo_fd, receive_fifo_fd); + state = computer_move(gameinfo, &passes, receive_fifo_fd); /* main ANSI Play loop */ while (state == 0) { @@ -795,7 +804,7 @@ void do_play_twoplayer(Gameinfo* gameinfo, int transmit_fifo_fd, int receive_fif /* Get the command or move. */ switch (get_command(command)) { case RESIGN: - state = twoplayer_endgame(gameinfo, 1); + //state = twoplayer_endgame(gameinfo, 1); break; case END: @@ -1130,8 +1139,10 @@ void do_play_twoplayer(Gameinfo* gameinfo, int transmit_fifo_fd, int receive_fif break; } - if (passes >= 2) + if (passes >= 2) { + twoplayer_send_move(NO_MOVE,transmit_fifo_fd); state = twoplayer_endgame(gameinfo, 0); + } } #if READLINE free(line_ptr); -- 2.20.1