int chc_ischeck(board_t board, int turn) { rc_t from, to; findking(board, turn, &to); for(from.r = 0;from.r < BRD_ROW; from.r++) for(from.c = 0; from.c < BRD_COL; from.c++) if(board[from.r][from.c] && CHE_O(board[from.r][from.c]) != turn) if(chc_canmove(board, from, to)) return 1; return 0; }
static int myplay(int s, board_t board, board_t tmpbrd) { int ch, start_time; int endgame = 0, endturn = 0; chc_ipass = 0, chc_selected = 0; start_time = time(NULL); chc_lefttime = CHC_TIMEOUT - (time(NULL) - start_time); bell(); while(!endturn) { chc_drawline(board, TIME_ROW); chc_movecur(chc_cursor.r, chc_cursor.c); oflush(); ch = igetkey(); chc_lefttime = CHC_TIMEOUT - (time(NULL) - start_time); if(chc_lefttime < 0) ch = 'q'; switch(ch) { case I_OTHERDATA: if(chc_recvmove(s)) { /* disconnect */ endgame = 1; endturn = 1; } else if(chc_from.r == -1 && chc_ipass) { endgame = 3; endturn = 1; } break; case KEY_UP: chc_cursor.r--; if(chc_cursor.r < 0) chc_cursor.r = BRD_ROW - 1; break; case KEY_DOWN: chc_cursor.r++; if(chc_cursor.r >= BRD_ROW) chc_cursor.r = 0; break; case KEY_LEFT: chc_cursor.c--; if(chc_cursor.c < 0) chc_cursor.c = BRD_COL - 1; break; case KEY_RIGHT: chc_cursor.c++; if(chc_cursor.c >= BRD_COL) chc_cursor.c = 0; break; case 'q': endgame = 2; endturn = 1; break; case 'p': chc_ipass = 1; chc_from.r = -1; chc_sendmove(s); strcpy(chc_warnmsg, "\033[1;33m要求和棋!\033[m"); chc_drawline(board, WARN_ROW); bell(); break; case '\r': case '\n': case ' ': if(chc_selected) { if(chc_cursor.r == chc_select.r && chc_cursor.c == chc_select.c) { chc_selected = 0; chc_drawline(board, LTR(chc_cursor.r)); } else if(chc_canmove(board, chc_select, chc_cursor)) { if(CHE_P(board[chc_cursor.r][chc_cursor.c]) == 1) endgame = 1; chc_from = chc_select; chc_to = chc_cursor; if(!endgame) { memcpy(tmpbrd, board, sizeof(board_t)); chc_movechess(tmpbrd); } if(endgame || !chc_iskfk(tmpbrd)) { chc_drawline(board, STEP_ROW); chc_movechess(board); chc_sendmove(s); chc_selected = 0; chc_drawline(board, LTR(chc_from.r)); chc_drawline(board, LTR(chc_to.r)); endturn = 1; } else { strcpy(chc_warnmsg, "\033[1;33m不可以王見王\033[m"); bell(); chc_drawline(board, WARN_ROW); } } } else if(board[chc_cursor.r][chc_cursor.c] && CHE_O(board[chc_cursor.r][chc_cursor.c]) == chc_turn) { chc_selected = 1; chc_select = chc_cursor; chc_drawline(board, LTR(chc_cursor.r)); } break; } } return endgame; }