int main (void) { initialize_curses(); initialize_field_window(); bdinit (_board); // Randomly choose who goes first enum { USER, // get input from standard input PROGRAM // get input from program }; unsigned input[2]; _humanPlayer = nrand(2); input[_humanPlayer] = USER; input[!_humanPlayer] = PROGRAM; _plyr[_humanPlayer] = "you"; _plyr[!_humanPlayer] = "me"; for (unsigned color = BLACK;; color = !color) { bdisp(); unsigned curmove; if (input[color] == USER) curmove = _lastHumanMove = usermove(); else curmove = _lastComputerMove = pickmove (color); int mv = makemove(color, curmove); if (mv != MOVEOK) { // Game finished. Display result and quit. if (mv != RESIGN) display_game_result_message (mv, input[color] == USER); break; } } return EXIT_SUCCESS; }
static unsigned makemove (enum EPlayer player) { if (player == USER) return usermove(); else { unsigned m = compmove(); _asked[COMPUTER][m] = true; wattron (_wmsg, A_BOLD); wprintw (_wmsg, "I ask you for %s. ", c_CardNames[m]); wattroff (_wmsg, A_BOLD); return m; } }
int main(int argc, char *argv[]) { int ch, move; while ((ch = getopt(argc, argv, "ph")) != -1) switch(ch) { case 'p': promode = 1; break; case '?': case 'h': default: usage(); } srandomdev(); instructions(); init(); if (nrandom(2) == 1) { printplayer(COMPUTER); (void)printf("get to start.\n"); goto istart; } printplayer(USER); (void)printf("get to start.\n"); for (;;) { move = usermove(); if (!comphand[move]) { if (gofish(move, USER, userhand)) continue; } else { goodmove(USER, move, userhand, comphand); continue; } istart: for (;;) { move = compmove(); if (!userhand[move]) { if (!gofish(move, COMPUTER, comphand)) break; } else goodmove(COMPUTER, move, comphand, userhand); } } /* NOTREACHED */ }
/* returns -1 for stalemate or winner's color */ int playchess() { int use_pondering = 0; printboard_and_time(); for (;;) { long starttime, endtime; move m; int g; g = gameoverp(tomove()); if (g) { switch (g) { case END_CHECKMATE: if (tomove() == BLACK) { return end(WHITE, "white mates"); } else { return end(BLACK, "black mates"); } case END_STALEMATE: return end(-1, "stalemate"); case NON_MATERIAL: return end(-1, "insufficient material"); case REP_DRAW: if (!robo_mode) { printf("drawable position\n"); } if (computer[WHITE]||computer[BLACK]) { if (robo_mode) tellics("draw\n"); return end(-1, "draw by repetition of moves"); } break; } } starttime = get_ms(); if (computer[tomove()]) { m = bce(); if ((m!=dummymove)&&(validmove(m)==1)) { printf("move %s\n", movestring(m)); } else { if (robo_mode) { tellics("mailmoves\n"); tellics( "message madhacker valid? = %d, move = %s, wouldbeincheckp() = %d, wouldbeinfullcheckp() = %d, pv = %s\n", validmove(m), movestring(m), wouldbeincheckp(m), wouldbeincheckfullp(m), thoughts); tellics("abort\n"); } else { printf("BCE returned invalid move: %s\n", movestring(m)); printf("valid? = %d\n", validmove(m)); fprintf(stdout, "random seed = %ld\n", seed); fprintf(stdout, "hash = %lld\n", board->hash); fprintf(stdout, "draw? = %d g = %d\n", draw_by_rep(), g); computer[1] = computer[0] = 0; } } use_pondering = 1; } else { if ((ponder_mode && computer[opp(tomove())])&& use_pondering) { ponder(); use_pondering = 0; } m = usermove(); use_pondering = 0; } endtime = get_ms(); chessclock[tomove()] -= (endtime-starttime); chessclock[tomove()] += clockinc; if (m) { domove(m); update_state(m); printboard_and_time(); } } }