int main() { char board[3][3]; int move; for (int i = 0; i < 3; ++i) for (int j = 0; j < 3; ++j) board[i][j] = ' '; draw_board(board); do { printf("Your Move : "); scanf("%d", &move); if (move >= 0 && move <= 8 && board[move / 3][move % 3] == ' ') { board[move / 3][move % 3] = 'X'; draw_board(board); } else { printf("Invalid Move!!!\n"); continue; } if (check_winner(board) != INCOM) break; comp_move(board); draw_board(board); if (check_winner(board) != INCOM) break; } while (1); if (check_winner(board) == COMPW) printf("Computer wins the Game. Better luck next time\n\n"); else if (check_winner(board) == USERW) printf("You have won the game! \n\n"); else printf("Damn! It's a draw.\n\n"); return 0; }
static void engine_step() { char string[StringSize]; int event; // parse UCI line engine_get(Engine,string,StringSize); event = uci_parse(Uci,string); // react to events if ((event & EVENT_READY) != 0) { // the engine is now ready if (!Uci->ready) { Uci->ready = true; if (XB->proto_ver >= 2) xboard_send(XBoard,"feature done=1"); } if (!DelayPong && XB->ping >= 0) { xboard_send(XBoard,"pong %d",XB->ping); XB->ping = -1; } } if ((event & EVENT_MOVE) != 0 && State->state == THINK) { // the engine is playing a move // MEGA HACK: estimate remaining time because XBoard won't send it! my_timer_stop(State->timer); XB->my_time -= my_timer_elapsed_real(State->timer); XB->my_time += XB->inc; if (XB->mps != 0 && (game_move_nb(Game) + 1) % XB->mps == 0) XB->my_time += XB->base; if (XB->my_time < 0.0) XB->my_time = 0.0; // play the engine move comp_move(Uci->best_move); } if ((event & EVENT_PV) != 0) { // the engine has sent a new PV send_pv(); } }
static void search_update() { int move; int move_nb; board_t board[1]; ASSERT(!Uci->searching); // launch a new search if needed if (State->state == THINK || State->state == PONDER || State->state == ANALYSE) { // opening book if (State->state == THINK && option_get_bool("Book")) { game_get_board(Game,Uci->board); move = book_move(Uci->board,option_get_bool("BookRandom")); if (move != MoveNone && move_is_legal(move,Uci->board)) { my_log("POLYGLOT *BOOK MOVE*\n"); search_clear(); // clears Uci->ponder_move Uci->best_move = move; board_copy(board,Uci->board); move_do(board,move); Uci->ponder_move = book_move(board,false); // expected move = best book move Uci->best_pv[0] = Uci->best_move; Uci->best_pv[1] = Uci->ponder_move; // can be MoveNone Uci->best_pv[2] = MoveNone; comp_move(Uci->best_move); return; } } // engine search my_log("POLYGLOT START SEARCH\n"); // options uci_send_option(Uci,"UCI_Chess960","%s",option_get_bool("Chess960")?"true":"false"); if (option_get_int("UCIVersion") >= 2) { uci_send_option(Uci,"UCI_Opponent","none none %s %s",(XB->computer)?"computer":"human",XB->name); uci_send_option(Uci,"UCI_AnalyseMode","%s",(XB->analyse)?"true":"false"); } uci_send_option(Uci,"Ponder","%s",ponder()?"true":"false"); // position move = (State->state == PONDER) ? State->exp_move : MoveNone; send_board(move); // updates Uci->board global variable // search if (State->state == THINK || State->state == PONDER) { engine_send_queue(Engine,"go"); if (XB->time_limit) { // fixed time per move engine_send_queue(Engine," movetime %.0f",XB->time_max*1000.0); } else { // time controls if (colour_is_white(Uci->board->turn)) { engine_send_queue(Engine," wtime %.0f btime %.0f",XB->my_time*1000.0,XB->opp_time*1000.0); } else { engine_send_queue(Engine," wtime %.0f btime %.0f",XB->opp_time*1000.0,XB->my_time*1000.0); } if (XB->inc != 0.0) engine_send_queue(Engine," winc %.0f binc %.0f",XB->inc*1000.0,XB->inc*1000.0); if (XB->mps != 0) { move_nb = XB->mps - (Uci->board->move_nb % XB->mps); ASSERT(move_nb>=1&&move_nb<=XB->mps); engine_send_queue(Engine," movestogo %d",move_nb); } } if (XB->depth_limit) engine_send_queue(Engine," depth %d",XB->depth_max); if (State->state == PONDER) engine_send_queue(Engine," ponder"); engine_send(Engine,""); // newline } else if (State->state == ANALYSE) { engine_send(Engine,"go infinite"); } else { ASSERT(false); } // init search info ASSERT(!Uci->searching); search_clear(); Uci->searching = true; Uci->pending_nb++; } }
void xboard2uci_engine_step(char string[]) { int event; board_t board[1]; event = uci_parse(Uci,string); // react to events if ((event & EVENT_READY) != 0) { // the engine is now ready if (!Uci->ready) { Uci->ready = TRUE; // if (XB->proto_ver >= 2) xboard_send(XBoard,"feature done=1"); } if (!DelayPong && XB->ping >= 0) { gui_send(GUI,"pong %d",XB->ping); XB->ping = -1; } } if ((event & EVENT_MOVE) != 0 && State->state == THINK) { // the engine is playing a move // MEGA HACK: estimate remaining time because XBoard won't send it! my_timer_stop(State->timer); XB->my_time -= my_timer_elapsed_real(State->timer); XB->my_time += XB->inc; if (XB->mps != 0 && (game_move_nb(Game) + 1) % XB->mps == 0) XB->my_time += XB->base; if (XB->my_time < 0.0) XB->my_time = 0.0; // make sure to remember the ponder move State->hint_move=Uci->ponder_move; // play the engine move comp_move(Uci->best_move); } if ((event & EVENT_PV) != 0) { // the engine has sent a new PV send_pv(); } if ((event & EVENT_INFO) != 0) { // the engine has sent info send_info(); } if((event & (EVENT_DRAW|EVENT_RESIGN))!=0){ my_log("POYGLOT draw offer/resign from engine\n"); if(option_find(Uci->option,"UCI_DrawOffers")){ if(event & EVENT_DRAW) gui_send(GUI,"offer draw"); else gui_send(GUI,"resign"); } } if(((event & EVENT_ILLEGAL_MOVE)!=0) && (State->state == THINK)){ game_get_board(Game,board); if(board->turn==White){ gui_send(GUI,"0-1 {polyglot: resign" " (illegal engine move by white: %s)}",Uci->bestmove); }else{ gui_send(GUI,"1-0 {polyglot: resign" " (illegal engine move by black: %s)}",Uci->bestmove); } board_disp(board); XB->result = TRUE; mess(); } }
static void search_update() { int move; int move_nb; board_t board[1]; ASSERT(!Uci->searching); // launch a new search if needed if (State->state == THINK || State->state == PONDER || State->state == ANALYSE) { // [VdB] moved up as we need the move number game_get_board(Game,Uci->board); // opening book if (State->state == THINK && option_get_bool(Option,"Book") && Uci->board->move_nb<option_get_int(Option,"BookDepth") ) { move = book_move(Uci->board,option_get_bool(Option,"BookRandom")); if (move != MoveNone && move_is_legal(move,Uci->board)) { my_log("POLYGLOT *BOOK MOVE*\n"); search_clear(); // clears Uci->ponder_move Uci->best_move = move; board_copy(board,Uci->board); move_do(board,move); Uci->ponder_move = book_move(board,FALSE); // expected move = best book move Uci->best_pv[0] = Uci->best_move; Uci->best_pv[1] = Uci->ponder_move; // can be MoveNone Uci->best_pv[2] = MoveNone; comp_move(Uci->best_move); return; } } // engine search my_log("POLYGLOT START SEARCH\n"); // options uci_send_option(Uci,"UCI_3Check","%s", option_get_bool(Option,"3Check")?"true":"false"); uci_send_option(Uci,"UCI_Chess960","%s", option_get_bool(Option,"Chess960")?"true":"false"); uci_send_option(Uci,"UCI_Atomic","%s", option_get_bool(Option,"Atomic")?"true":"false"); uci_send_option(Uci,"UCI_Horde","%s", option_get_bool(Option,"Horde")?"true":"false"); if (option_get_int(Option,"UCIVersion") >= 2) { uci_send_option(Uci,"UCI_Opponent","none none %s %s",(XB->computer)?"computer":"human",XB->name); uci_send_option(Uci,"UCI_AnalyseMode","%s",(XB->analyse)?"true":"false"); } uci_send_option(Uci,"Ponder","%s",ponder()?"true":"false"); // position move = (State->state == PONDER) ? State->exp_move : MoveNone; send_board(move); // updates Uci->board global variable // search if (State->state == THINK || State->state == PONDER) { engine_send_queue(Engine,"go"); if (XB->time_limit) { // fixed time per move if(XB->node_rate > 0){ engine_send_queue(Engine, " nodes %.0f", XB->time_max*((double)XB->node_rate)); }else{ double computed_time; double st_fudge; st_fudge=(double) option_get_int(Option,"STFudge"); my_log("POLYGLOT Giving engine %.0fmsec extra time.\n",st_fudge); computed_time=XB->time_max*1000.0-st_fudge; if(computed_time< 1.0){ computed_time=1.0; } engine_send_queue(Engine, " movetime %.0f", computed_time); } } else { // time controls if(XB->node_rate > 0) { double time; move_nb = 40; if (XB->mps != 0){ move_nb = XB->mps - (Uci->board->move_nb % XB->mps); } time = XB->my_time / move_nb; if(XB->inc != 0){ time += XB->inc; } if(time > XB->my_time){ time = XB->my_time; } engine_send_queue(Engine, " nodes %.0f", time*XB->node_rate); } else { if (colour_is_white(Uci->board->turn)) { engine_send_queue(Engine, " wtime %.0f btime %.0f", XB->my_time*1000.0,XB->opp_time*1000.0); } else { engine_send_queue(Engine, " wtime %.0f btime %.0f", XB->opp_time*1000.0,XB->my_time*1000.0); } if (XB->inc != 0.0){ engine_send_queue(Engine, " winc %.0f binc %.0f", XB->inc*1000.0,XB->inc*1000.0); } if (XB->mps != 0) { move_nb = XB->mps - (Uci->board->move_nb % XB->mps); ASSERT(move_nb>=1&&move_nb<=XB->mps); engine_send_queue(Engine," movestogo %d",move_nb); } } } if (XB->depth_limit) engine_send_queue(Engine," depth %d",XB->depth_max); if (State->state == PONDER) engine_send_queue(Engine," ponder"); engine_send(Engine,""); // newline } else if (State->state == ANALYSE) { engine_send(Engine,"go infinite"); } else { ASSERT(FALSE); } // init search info ASSERT(!Uci->searching); search_clear(); Uci->searching = TRUE; Uci->pending_nb++; } }
static void engine_step() { char string[StringSize]; int event; // parse UCI line engine_get(Engine,string,StringSize); //blocking read... event = uci_parse(Uci,string); // react to events if ((event & EVENT_READY) != 0) { #ifdef _WIN32 SetEvent(Engine_ready_ok); #endif if (!Uci->ready) { Uci->ready = true; if (XB->proto_ver >= 2) xboard_send(XBoard,"feature done=1"); } if (!DelayPong && XB->ping >= 0) { xboard_send(XBoard,"pong %d",XB->ping); XB->ping = -1; } } if ((event & EVENT_MOVE) != 0 && State->state == THINK) { // the engine is playing a move // MEGA HACK: estimate remaining time because XBoard won't send it! my_timer_stop(State->timer); XB->my_time -= my_timer_elapsed_real(State->timer); XB->my_time += XB->inc; if (XB->mps != 0 && (game_move_nb(Game) + 1) % XB->mps == 0) XB->my_time += XB->base; if (XB->my_time < 0.0) XB->my_time = 0.0; // play the engine move comp_move(Uci->best_move); } if ((event & EVENT_PV) != 0) { // the engine has sent a new PV send_pv(); } if ((event & EVENT_INFO) != 0){ if(!option_get_bool("InfoStrings")) xboard_send(XBoard,"#%d %+d %.0f " S64_FORMAT " %s ",Uci->best_depth,Uci->best_score,Uci->time*100.0,Uci->node_nb,Uci->info_string); else xboard_send(XBoard,"%d %+d %.0f " S64_FORMAT " %s ",Uci->best_depth,Uci->best_score,Uci->time*100.0,Uci->node_nb,Uci->info_string); } if((event & (EVENT_DRAW|EVENT_RESIGN))!=0){ my_log("POYGLOT draw offer/resign from engine\n"); if(uci_option_exist(Uci,"UCI_DrawOffers")){ if(event & EVENT_DRAW) xboard_send(XBoard,"offer draw"); else xboard_send(XBoard,"resign"); } } return ; }