summaryrefslogblamecommitdiffstats
path: root/mbbsd/chc.c
blob: 21f536f582eb9c59889b3eb7f6081d5b86643fea (plain) (tree)
1
2
3
4
5
6
7
8
9
          
                


                                                               
 

                                      
           
            

        
 








             

                                          
 
                          
                                  
 
                      
                                              


                             
                















                                                                                
 
 
                                                                 
 

                                       
                                                         
 


                                                               

  
                                             
                                                     

                                                     

  
                                                        









                                                       
                                         










                                                       
                                  





                        












                                                         
 













                                             










                                 
                                                                            


                                 

                                         

                                          









                                                                           

                                                        







                                 

                                                    



                       





                                             
                                                             






                                                                 
                                                                     

                             
                                                 
                                   
                                                               
     



               
                               
 








                                                                    


           
                                             

                         











                                                                           
 


                    



                                                                   




                                         

                                                                       
                                             
                                                                         
                                                          

                      
                                             
                                                
                                                          
                 






                                                              








                                              
 

                                                       
                      
                                                                


                                                                      

                                         
                                                                                  
                                               









                                                                         
                    

                                                            
                                                 














                                                                             
                    

                                                            
                                              
                                    
         





                                                         

                                               


                                           


                                                                              
                                            



                                                       
                

                                                        
                                            



                                                       
                

                                                        
                                           
                                




                                                                        

                                                                            




                                                                        

                                                                            
         

     







                               
    
                                                                

                 



                                                        


          
                      

                                    
                              
      





                                                                         

                         



                             
                                                   




                                                                       
                 
                                                                             



                                                  
                              






                              






























                                                                            







                              
           
                                                    
 

                            
                                      





                                                                
                                             







                                                                        
                                             
                                                                


           
                                                   
 




















                                                                      

 
                                  








                                               
                                        





















                                                      
                                                      













                                                              
                                           


                                    

                                             


                                 
                                           

                                  

                                             


                                 
                                           

                                  

                                             




                                                              
                                        





                                              
                                           











                                                    
                                           








                                              
                                           


                                    

                                              

                     

                                                  





                     
                           
           
                                                     


                            
                                   

                                
                                               



                                             
                         


          
                                


                             


                                      





                                                           
                                            


                             
                                   



                                                     
                                                     


                             




                            
                                                   



                                  
                                            


           
                                                      




                                                                


                                                    
                                     

 


















                                                                   
          
                                                                   
 

                                                        
 
                
 







                                                                     
         


















                                                                            
         
 




















                                                                                   
                   

                                         
         


                              

 






                                                                 
 




                                                         
                                                                              


                   

                  
 










                                 












                                                                           

 





























                                                                              
           
                                                    
 

                                          
 

                                          

                                                      
                                  
                          









                                                                         
         



                                           
                                                      

                                
                                                 
                                                      


                                 
                                                      


                                
                                                   

                                       
     




              
                                                        




                  
                                





               
                                                     
 
/* $Id$ */
#include "bbs.h"
#include "chess.h"

#define assert_not_reached() assert(!"Should never be here!!!")

extern const double elo_exp_tab[1000];

enum Turn {
    BLK = 0,
    RED 
};

enum Kind {
    KIND_K=1,
    KIND_A,
    KIND_E,
    KIND_R,
    KIND_H,
    KIND_C,
    KIND_P,
};
#define CENTER(a, b)    (((a) + (b)) >> 1)
#define CHC_TIMEOUT 300

#define PHOTO_LINE      15
#define PHOTO_COLUMN    (256 + 25)

typedef struct drc_t {
    ChessStepType   type;  /* necessary one */
    rc_t            from, to;
}               drc_t;

typedef struct {
    rc_t select;
    char selected;
} chc_tag_data_t;

/* chess framework action functions */
static void chc_init_user(const userec_t *userec, ChessUser *user);
static void chc_init_board(const ChessInfo* info, board_t board);
static void chc_drawline(const ChessInfo* info, int line);
static void chc_movecur(int r, int c);
static void chc_prepare_play(ChessInfo* info);
static int  chc_select(ChessInfo* info, rc_t location, ChessGameResult* result);
static void chc_prepare_step(ChessInfo* info, const void* step);
static int  chc_movechess(board_t board, const drc_t* move);
static void chc_drawstep(ChessInfo* info, const drc_t* move);
static void chc_gameend(ChessInfo* info, ChessGameResult result);
static void chc_genlog(ChessInfo* info, FILE* fp, ChessGameResult result);


static const char * const turn_color[2]={BLACK_COLOR, RED_COLOR};

/* some constant variable definition */

static const char * const turn_str[2] = {"黑的", "紅的"};

static const char * const num_str[2][10] = {
    {"", "1", "2", "3", "4", "5", "6", "7", "8", "9"},
    {"", "一", "二", "三", "四", "五", "六", "七", "八", "九"},
};

static const char * const chess_str[2][8] = {
    /* 0     1     2     3     4     5     6     7 */
    {"  ", "將", "士", "象", "車", "馬", "包", "卒"},
    {"  ", "帥", "仕", "相", "車", "傌", "炮", "兵"}
};

static const char * const chess_brd[BRD_ROW * 2 - 1] = {
    /* 0   1   2   3   4   5   6   7   8 */
    "┌─┬─┬─┬─┬─┬─┬─┬─┐",  /* 0 */
    "│  │  │  │\│/│  │  │  │",
    "├─┼─┼─┼─┼─┼─┼─┼─┤",  /* 1 */
    "│  │  │  │/│\│  │  │  │",
    "├─┼─┼─┼─┼─┼─┼─┼─┤",  /* 2 */
    "│  │  │  │  │  │  │  │  │",
    "├─┼─┼─┼─┼─┼─┼─┼─┤",  /* 3 */
    "│  │  │  │  │  │  │  │  │",
    "├─┴─┴─┴─┴─┴─┴─┴─┤",  /* 4 */
    "│  楚    河          漢    界  │",
    "├─┬─┬─┬─┬─┬─┬─┬─┤",  /* 5 */
    "│  │  │  │  │  │  │  │  │",
    "├─┼─┼─┼─┼─┼─┼─┼─┤",  /* 6 */
    "│  │  │  │  │  │  │  │  │",
    "├─┼─┼─┼─┼─┼─┼─┼─┤",  /* 7 */
    "│  │  │  │\│/│  │  │  │",
    "├─┼─┼─┼─┼─┼─┼─┼─┤",  /* 8 */
    "│  │  │  │/│\│  │  │  │",
    "└─┴─┴─┴─┴─┴─┴─┴─┘"   /* 9 */
};

static char * const hint_str[] = {
    "  q      認輸離開",
    "  p      要求和棋",
    "方向鍵   移動遊標",
    "Enter    選擇/移動"
};

static const ChessActions chc_actions = {
    &chc_init_user,
    (void (*) (const ChessInfo*, void*)) &chc_init_board,
    &chc_drawline,
    &chc_movecur,
    &chc_prepare_play,
    &chc_select,
    &chc_prepare_step,
    (int (*) (void*, const void*)) &chc_movechess,
    (void (*)(ChessInfo*, const void*)) &chc_drawstep,
    &chc_gameend,
    &chc_genlog
};

static const ChessConstants chc_constants = {
    sizeof(drc_t),
    CHC_TIMEOUT,
    BRD_ROW,
    BRD_COL,
    "photo_cchess",
#ifdef GLOBAL_CCHESS_LOG
    GLOBAL_CCHESS_LOG,
#else
    NULL,
#endif
    { BLACK_COLOR, RED_COLOR },
    {"黑的", "紅的"}
};

/*
 * Start of the drawing function.
 */
static void
chc_movecur(int r, int c)
{
    move(r * 2 + 3, c * 4 + 4);
}

static char *
getstep(board_t board, int my, const rc_t *from, const rc_t *to, char buf[])
{
    int             turn, fc, tc;
    char           *dir;
    int         twin = 0, twin_r = 0;
    int         len = 0;

    turn = CHE_O(board[from->r][from->c]);
    if(CHE_P(board[from->r][from->c] != KIND_P)) { // TODO 目前不管兵卒前後
    int i;
    for(i=0;i<10;i++)
        if(board[i][from->c]==board[from->r][from->c]) {
        if(i!=from->r) {
            twin=1;
            twin_r=i;
        }
        }
    }
    fc = (turn == (my ^ 1) ? from->c + 1 : 9 - from->c);
    tc = (turn == (my ^ 1) ? to->c + 1 : 9 - to->c);
    if (from->r == to->r)
    dir = "平";
    else {
    if (from->c == to->c)
        tc = from->r - to->r;
    if (tc < 0)
        tc = -tc;

    if ((turn == (my ^ 1) && to->r > from->r) ||
        (turn == my && to->r < from->r))
        dir = "進";
    else
        dir = "退";
    }


    len=sprintf(buf, "%s", turn_color[turn]);
    /* 傌二|前傌 */
    if(twin) {
    len+=sprintf(buf+len, "%s%s",
        ((from->r>twin_r)==(turn==(my^1)))?"前":"後",
        chess_str[turn][CHE_P(board[from->r][from->c])]);
    } else {
    len+=sprintf(buf+len, "%s%s",
        chess_str[turn][CHE_P(board[from->r][from->c])],
        num_str[turn][fc]);
    }
    /* 進三 */
    len+=sprintf(buf+len, "%s%s" ANSI_RESET, dir, num_str[turn][tc]);
    /* :象 */
    if(board[to->r][to->c]) {
    len+=sprintf(buf+len,":%s%s" ANSI_RESET,
        turn_color[turn^1],
        chess_str[turn^1][CHE_P(board[to->r][to->c])]);
    }
    return buf;
}

static void
showstep(const ChessInfo* info)
{
    outs(info->last_movestr);
}

inline static const char*
chc_timestr(int second)
{
    static char str[10];
    snprintf(str, sizeof(str), "%d:%02d", second / 60, second % 60);
    return str;
}

static void
chc_drawline(const ChessInfo* info, int line)
{
    int             i, j;
    board_p         board = (board_p) info->board;
    chc_tag_data_t *tag = info->tag;

    if (line == CHESS_DRAWING_TURN_ROW)
    line = info->photo ? PHOTO_TURN_ROW : REAL_TURN_ROW;
    else if (line == CHESS_DRAWING_TIME_ROW) {
    chc_drawline(info, info->photo ? PHOTO_TIME_ROW1 : REAL_TIME_ROW1);
    line = info->photo ? PHOTO_TIME_ROW2 : REAL_TIME_ROW2;
    } else if (line == CHESS_DRAWING_WARN_ROW)
    line = info->photo ? PHOTO_WARN_ROW : REAL_WARN_ROW;
    else if (line == CHESS_DRAWING_STEP_ROW)
    line = STEP_ROW;

    move(line, 0);
    clrtoeol();
    if (line == 0) {
    prints(ANSI_COLOR(1;46) "   象棋對戰   " ANSI_COLOR(45)
        "%30s VS %-20s%10s" ANSI_RESET,
           info->user1.userid, info->user2.userid,
           info->mode == CHESS_MODE_WATCH ? "[觀棋模式]" : "");
    } else if (line >= 3 && line <= 21) {
    outs("   ");
    for (i = 0; i < 9; i++) {
        j = board[RTL(line)][i];
        if ((line & 1) == 1 && j) {
        if (tag->selected &&
            tag->select.r == RTL(line) && tag->select.c == i) {
            prints("%s%s" ANSI_RESET,
               CHE_O(j) == BLK ? BLACK_REVERSE : RED_REVERSE,
               chess_str[CHE_O(j)][CHE_P(j)]);
        }
        else {
            prints("%s%s" ANSI_RESET,
               turn_color[CHE_O(j)],
               chess_str[CHE_O(j)][CHE_P(j)]);
        }
        } else
        prints("%c%c", chess_brd[line - 3][i * 4],
               chess_brd[line - 3][i * 4 + 1]);
        if (i != 8)
        prints("%c%c", chess_brd[line - 3][i * 4 + 2],
               chess_brd[line - 3][i * 4 + 3]);
    }
    } else if (line == 2 || line == 22) {
    outs("   ");
    if (line == 2)
        for (i = 1; i <= 9; i++)
        prints("%s  ", num_str[0][i]);
    else
        for (i = 9; i >= 1; i--)
        prints("%s  ", num_str[1][i]);
    }

    if (info->photo) {
    if (line >= 3 && line < 3 + CHESS_PHOTO_LINE) {
        outs(" ");
        outs(info->photo + (line - 3) * CHESS_PHOTO_COLUMN);
    } else if (line >= PHOTO_TURN_ROW && line <= PHOTO_WARN_ROW) {
        outs("         ");
        if (line == PHOTO_TURN_ROW)
        prints("%s%s" ANSI_RESET,
            TURN_COLOR,
            info->my == info->turn ? "輪到你下棋了" : "等待對方下棋");
        else if (line == PHOTO_TIME_ROW1) {
        if (info->mode == CHESS_MODE_WATCH) {
            if (!info->timelimit)
            prints("每手限時五分鐘");
            else
            prints("局時: %5s",
                chc_timestr(info->timelimit->free_time));
        } else if (info->lefthand[0])
            prints("我方剩餘時間 %s / %2d 步",
                chc_timestr(info->lefttime[0]),
                info->lefthand[0]);
        else
            prints("我方剩餘時間 %s",
                chc_timestr(info->lefttime[0]));
        } else if (line == PHOTO_TIME_ROW2) {
        if (info->mode == CHESS_MODE_WATCH) {
            if (info->timelimit) {
            if (info->timelimit->time_mode ==
                CHESS_TIMEMODE_MULTIHAND)
                prints("步時: %s / %2d 步",
                    chc_timestr(info->timelimit->limit_time),
                    info->timelimit->limit_hand);
            else
                prints("讀秒: %5d 秒",
                    info->timelimit->limit_time);
            }
        } else if (info->lefthand[1])
            prints("對方剩餘時間 %s / %2d 步",
                chc_timestr(info->lefttime[1]),
                info->lefthand[1]);
        else
            prints("對方剩餘時間 %s",
                chc_timestr(info->lefttime[1]));
        } else if (line == PHOTO_WARN_ROW)
        outs(info->warnmsg);
    }
    } else if (line >= 3 && line <= HISWIN_ROW) {
    outs("        ");
    if (line >= 3 && line < 3 + (int)dim(hint_str)) {
        outs(hint_str[line - 3]);
    } else if (line == SIDE_ROW) {
        prints(ANSI_COLOR(1) "你是%s%s" ANSI_RESET,
            turn_color[(int) info->my],
            turn_str[(int) info->my]);
    } else if (line == REAL_TURN_ROW) {
        prints("%s%s" ANSI_RESET,
            TURN_COLOR,
            info->my == info->turn ? "輪到你下棋了" : "等待對方下棋");
    } else if (line == STEP_ROW && info->last_movestr) {
        showstep(info);
    } else if (line == REAL_TIME_ROW1) {
        if (info->lefthand[0])
        prints("我方剩餘時間 %s / %2d 步",
            chc_timestr(info->lefttime[0]),
            info->lefthand[0]);
        else
        prints("我方剩餘時間 %s",
            chc_timestr(info->lefttime[0]));
    } else if (line == REAL_TIME_ROW2) {
        if (info->lefthand[1])
        prints("對方剩餘時間 %s / %2d 步",
            chc_timestr(info->lefttime[1]),
            info->lefthand[1]);
        else
        prints("對方剩餘時間 %s",
            chc_timestr(info->lefttime[1]));
    } else if (line == REAL_WARN_ROW) {
        outs(info->warnmsg);
    } else if (line == MYWIN_ROW) {
        prints(ANSI_COLOR(1;33) "%12.12s    "
            ANSI_COLOR(1;31) "%2d" ANSI_COLOR(37) "勝 "
            ANSI_COLOR(34) "%2d" ANSI_COLOR(37) "敗 "
            ANSI_COLOR(36) "%2d" ANSI_COLOR(37) "和" ANSI_RESET,
            info->user1.userid,
            info->user1.win, info->user1.lose - 1, info->user1.tie);
    } else if (line == HISWIN_ROW) {
        prints(ANSI_COLOR(1;33) "%12.12s    "
            ANSI_COLOR(1;31) "%2d" ANSI_COLOR(37) "勝 "
            ANSI_COLOR(34) "%2d" ANSI_COLOR(37) "敗 "
            ANSI_COLOR(36) "%2d" ANSI_COLOR(37) "和" ANSI_RESET,
            info->user2.userid,
            info->user2.win, info->user2.lose - 1, info->user2.tie);
    }
    }
}
/*
 * End of the drawing function.
 */


/*
 * Start of the log function.
 */
void
chc_log_step(FILE* fp, board_t board, int my, const drc_t *step)
{
    char buf[80];
    buf[0] = buf[1] = ' ';
    getstep(board, my, &step->from, &step->to, buf + 2);
    fputs(buf, fp);
    fputc('\n', fp);
}

static int
#if defined(__linux__)
chc_filter(const struct dirent *dir)
#else
chc_filter(struct dirent *dir)
#endif
{
    if (strcmp(dir->d_name, ".") == 0 || strcmp(dir->d_name, "..") == 0 )
    return 0;
    return strstr(dir->d_name, ".poem") != NULL;
}

static int
chc_log_poem(FILE* outfp)
{
    struct dirent **namelist;
    int n;

    // TODO use readdir(), don't use lots of memory
    n = scandir(BBSHOME"/etc/chess", &namelist, chc_filter, alphasort);
    if (n < 0)
    perror("scandir");
    else {
    char buf[80];
    FILE *fp;
    sprintf(buf, BBSHOME"/etc/chess/%s", namelist[random() % n]->d_name);
    if ((fp = fopen(buf, "r")) == NULL)
        return -1;

    while(fgets(buf, sizeof(buf), fp) != NULL)
        fputs(buf, outfp);
    while(n--)
        free(namelist[n]);
    free(namelist);
    fclose(fp);
    }
    return 0;
}

static void
chc_genlog(ChessInfo* info, FILE* fp, ChessGameResult result)
{
    const int nStep = info->history.used;
    board_t   board;
    int i;

    if (info->my == RED)
    fprintf(fp, "%s V.S. %s\n", info->user1.userid, info->user2.userid);
    else
    fprintf(fp, "%s V.S. %s\n", info->user2.userid, info->user1.userid);

    chc_init_board(info, board);
    for (i = 0; i < nStep; ++i) {
    const drc_t *move = (const drc_t*)  ChessHistoryRetrieve(info, i);
    chc_log_step(fp, board, info->my, move);
    chc_movechess(board, move);
    }

    if (result == CHESS_RESULT_TIE)
    fprintf(fp, "=> 和局\n");
    else if (result == CHESS_RESULT_WIN || result == CHESS_RESULT_LOST)
    fprintf(fp, "=> %s 勝\n",
        (info->my == RED) == (result== CHESS_RESULT_WIN) ?
        "紅" : "黑");
    
    fputs("\n--\n\n", fp);

    chc_log_poem(fp);
}
/*
 * End of the log function.
 */


/*
 * Start of the rule function.
 */
static void
chc_init_board(const ChessInfo* info, board_t board)
{
    const int my = info->my;

    memset(board, 0, sizeof(board_t));
    board[0][4] = CHE(KIND_K, my ^ 1);  /* 將 */
    board[0][3] = board[0][5] = CHE(KIND_A, my ^ 1);    /* 士 */
    board[0][2] = board[0][6] = CHE(KIND_E, my ^ 1);    /* 象 */
    board[0][0] = board[0][8] = CHE(KIND_R, my ^ 1);    /* 車 */
    board[0][1] = board[0][7] = CHE(KIND_H, my ^ 1);    /* 馬 */
    board[2][1] = board[2][7] = CHE(KIND_C, my ^ 1);    /* 包 */
    board[3][0] = board[3][2] = board[3][4] =
    board[3][6] = board[3][8] = CHE(KIND_P, my ^ 1);    /* 卒 */

    board[9][4] = CHE(KIND_K, my);  /* 帥 */
    board[9][3] = board[9][5] = CHE(KIND_A, my);    /* 仕 */
    board[9][2] = board[9][6] = CHE(KIND_E, my);    /* 相 */
    board[9][0] = board[9][8] = CHE(KIND_R, my);    /* 車 */
    board[9][1] = board[9][7] = CHE(KIND_H, my);    /* 傌 */
    board[7][1] = board[7][7] = CHE(KIND_C, my);    /* 炮 */
    board[6][0] = board[6][2] = board[6][4] =
    board[6][6] = board[6][8] = CHE(KIND_P, my);    /* 兵 */
}

static void
chc_prepare_step(ChessInfo* info, const void* step)
{
    const drc_t* move = (const drc_t*) step;
    getstep((board_p) info->board, info->my,
        &move->from, &move->to, info->last_movestr);
}

static int
chc_movechess(board_t board, const drc_t* move)
{
    int end = (CHE_P(board[move->to.r][move->to.c]) == KIND_K);

    board[move->to.r][move->to.c] = board[move->from.r][move->from.c];
    board[move->from.r][move->from.c] = 0;

    return end;
}

static void
chc_drawstep(ChessInfo* info, const drc_t* move)
{
    info->actions->drawline(info, LTR(move->from.r));
    info->actions->drawline(info, LTR(move->to.r));
}

/* 求兩座標行或列(rowcol)的距離 */
static int
dist(rc_t from, rc_t to, int rowcol)
{
    int             d;

    d = rowcol ? from.c - to.c : from.r - to.r;
    return d > 0 ? d : -d;
}

/* 兩座標(行或列rowcol)中間有幾顆棋子 */
static int
between(board_t board, rc_t from, rc_t to, int rowcol)
{
    int             i, rtv = 0;

    if (rowcol) {
    if (from.c > to.c)
        i = from.c, from.c = to.c, to.c = i;
    for (i = from.c + 1; i < to.c; i++)
        if (board[to.r][i])
        rtv++;
    } else {
    if (from.r > to.r)
        i = from.r, from.r = to.r, to.r = i;
    for (i = from.r + 1; i < to.r; i++)
        if (board[i][to.c])
        rtv++;
    }
    return rtv;
}

static int
chc_canmove(board_t board, int my, rc_t from, rc_t to)
{
    int             i;
    int             rd, cd, turn;

    rd = dist(from, to, 0);
    cd = dist(from, to, 1);
    turn = CHE_O(board[from.r][from.c]);

    /* general check */
    if (board[to.r][to.c] && CHE_O(board[to.r][to.c]) == turn)
    return 0;

    /* individual check */
    switch (CHE_P(board[from.r][from.c])) {
    case KIND_K:        /* 將 帥 */
    if (!(rd == 1 && cd == 0) &&
        !(rd == 0 && cd == 1))
        return 0;
    if ((turn == (my ^ 1) && to.r > 2) ||
        (turn == my && to.r < 7) ||
        to.c < 3 || to.c > 5)
        return 0;
    break;
    case KIND_A:        /* 士 仕 */
    if (!(rd == 1 && cd == 1))
        return 0;
    if ((turn == (my ^ 1) && to.r > 2) ||
        (turn == my && to.r < 7) ||
        to.c < 3 || to.c > 5)
        return 0;
    break;
    case KIND_E:        /* 象 相 */
    if (!(rd == 2 && cd == 2))
        return 0;
    if ((turn == (my ^ 1) && to.r > 4) ||
        (turn == my && to.r < 5))
        return 0;
    /* 拐象腿 */
    if (board[CENTER(from.r, to.r)][CENTER(from.c, to.c)])
        return 0;
    break;
    case KIND_R:        /* 車 */
    if (!(rd > 0 && cd == 0) &&
        !(rd == 0 && cd > 0))
        return 0;
    if (between(board, from, to, rd == 0))
        return 0;
    break;
    case KIND_H:        /* 馬 傌 */
    if (!(rd == 2 && cd == 1) &&
        !(rd == 1 && cd == 2))
        return 0;
    /* 拐馬腳 */
    if (rd == 2) {
        if (board[CENTER(from.r, to.r)][from.c])
        return 0;
    } else {
        if (board[from.r][CENTER(from.c, to.c)])
        return 0;
    }
    break;
    case KIND_C:        /* 包 炮 */
    if (!(rd > 0 && cd == 0) &&
        !(rd == 0 && cd > 0))
        return 0;
    i = between(board, from, to, rd == 0);
    if ((i > 1) ||
        (i == 1 && !board[to.r][to.c]) ||
        (i == 0 && board[to.r][to.c]))
        return 0;
    break;
    case KIND_P:        /* 卒 兵 */
    if (!(rd == 1 && cd == 0) &&
        !(rd == 0 && cd == 1))
        return 0;
    if (((turn == (my ^ 1) && to.r < 5) ||
         (turn == my && to.r > 4)) &&
        cd != 0)
        return 0;
    if ((turn == (my ^ 1) && to.r < from.r) ||
        (turn == my && to.r > from.r))
        return 0;
    break;
    }
    return 1;
}

/* 找 turn's king 的座標 */
static void
findking(board_t board, int my, int turn, rc_t * buf)
{
    int             i, r, c;

    r = (turn == (my ^ 1) ? 0 : 7);
    for (i = 0; i < 3; r++, i++)
    for (c = 3; c < 6; c++)
        if (CHE_P(board[r][c]) == KIND_K &&
        CHE_O(board[r][c]) == turn) {
        buf->r = r, buf->c = c;
        return;
        }
    assert_not_reached();
}

static int
chc_iskfk(board_t board, int my)
{
    rc_t            from, to;

    /* the `my' here doesn't matter */
    findking(board, my, BLK, &to);
    findking(board, my, RED, &from);
    if (from.c == to.c && between(board, from, to, 0) == 0)
    return 1;
    return 0;
}

static int
chc_ischeck(board_t board, int my, int turn)
{
    rc_t            from, to;

    findking(board, my, 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, my, from, to))
            return 1;
    return 0;
}
/*
 * End of the rule function.
 */

static void
chcusr_put(userec_t* userec, const ChessUser* user)
{
    userec->chc_win = user->win;
    userec->chc_lose = user->lose;
    userec->chc_tie = user->tie;
    userec->chess_elo_rating = user->rating;
}

static void
chc_init_user(const userec_t *userec, ChessUser *user)
{
    strlcpy(user->userid, userec->userid, sizeof(user->userid));
    user->win = userec->chc_win;
    user->lose = userec->chc_lose;
    user->tie = userec->chc_tie;
    user->rating = userec->chess_elo_rating;
    if(user->rating == 0)
    user->rating = 1500; /* ELO initial value */
    user->orig_rating = user->rating;
}


static void
chc_prepare_play(ChessInfo* info)
{
    if (chc_ischeck((board_p) info->board, info->my, info->turn)) {
    strlcpy(info->warnmsg, ANSI_COLOR(1;31) "將軍!" ANSI_RESET,
        sizeof(info->warnmsg));
    bell();
    } else
    info->warnmsg[0] = 0;
}

inline static void
chc_reverse(rc_t* coor)
{
    coor->r = BRD_ROW - 1 - coor->r;
    coor->c = BRD_COL - 1 - coor->c;
}

static int
chc_select(ChessInfo* info, rc_t location, ChessGameResult* result)
{
    chc_tag_data_t* tag = (chc_tag_data_t*) info->tag;
    board_p board       = (board_p)         info->board;

    assert(tag);

    if (!tag->selected) {
    /* trying to pick something */
    if (board[location.r][location.c] &&
        CHE_O(board[location.r][location.c]) == info->turn) {
        /* they can pick up this */
        tag->selected = 1;
        tag->select = location;
        chc_drawline(info, LTR(location.r));
    }
    return 0;
    } else if (tag->select.r == location.r && tag->select.c == location.c) {
    /* cancel selection */
    tag->selected = 0;
    chc_drawline(info, LTR(location.r));
    return 0;
    } else if (chc_canmove(board, info->my, tag->select, location)) {
    /* moving the chess */
    drc_t   moving = { CHESS_STEP_NORMAL, tag->select, location };
    board_t tmpbrd;
    int valid_step = 1;

    if (CHE_P(board[location.r][location.c]) == KIND_K)
        /* 移到對方將帥 */
        *result = CHESS_RESULT_WIN;
    else {
        memcpy(tmpbrd, board, sizeof(board_t));
        chc_movechess(tmpbrd, &moving);
        valid_step = !chc_iskfk(tmpbrd, info->my);
    }

    if (valid_step) {
        getstep(board, info->my, &moving.from, &moving.to, info->last_movestr);

        chc_movechess(board, &moving);
        chc_drawline(info, LTR(moving.from.r));
        chc_drawline(info, LTR(moving.to.r));

        ChessHistoryAppend(info, &moving);
        ChessStepBroadcast(info, &moving);

        chc_reverse(&moving.from);
        chc_reverse(&moving.to);
        ChessStepSendOpposite(info, &moving);

        tag->selected = 0;
        return 1;
    } else {
        /* 王見王 */
        strlcpy(info->warnmsg,
            ANSI_COLOR(1;33) "不可以王見王" ANSI_RESET,
            sizeof(info->warnmsg));
        bell();
        chc_drawline(info, WARN_ROW);
        return 0;
    }
    } else
    /* nothing happened */
    return 0;
}

int round_to_int(double x)
{
    /* assume that double cast to int will drop fraction parts */
    if(x>=0)
    return (int)(x+0.5);
    return (int)(x-0.5);
}

/*
 * ELO rating system
 * see http://www.wordiq.com/definition/ELO_rating_system
 */
static void
count_chess_elo_rating(ChessUser* user1, const ChessUser* user2, double myres)
{
    double k;
    double exp_res;
    int diff;
    int newrating;

    if(user1->rating < 1800)
    k = 30;
    else if(user1->rating < 2000)
    k = 25;
    else if(user1->rating < 2200)
    k = 20;
    else if(user1->rating < 2400)
    k = 15;
    else
    k = 10;

    //exp_res = 1.0/(1.0 + pow(10.0, (user2->rating-user1->rating)/400.0));
    //user1->rating += (int)floor(k*(myres-exp_res)+0.5);
    diff=(int)user2->rating-(int)user1->rating;
    if(diff<=-1000 || diff>=1000)
       exp_res=diff>0?0.0:1.0;
    else if(diff>=0)
       exp_res=elo_exp_tab[diff];
    else
       exp_res=1.0-elo_exp_tab[-diff];
    newrating = (int)user1->rating + round_to_int(k*(myres-exp_res));
    if(newrating > 3000) newrating = 3000;
    if(newrating < 1) newrating = 1;
    user1->rating = newrating;
}


/* 象棋功能進入點:
 * chc_main: 對奕
 * chc_personal: 打譜
 * chc_watch: 觀棋
 * talk.c: 對奕
 */
void
chc(int s, ChessGameMode mode)
{
    ChessInfo*     info = NewChessInfo(&chc_actions, &chc_constants, s, mode);
    board_t        board;
    chc_tag_data_t tag;

    chc_init_board(info, board);
    tag.selected = 0;

    info->board = board;
    info->tag   = &tag;

    if (mode == CHESS_MODE_WATCH)
    setutmpmode(CHESSWATCHING);
    else
    setutmpmode(CHC);

    ChessPlay(info);

    DeleteChessInfo(info);
}

static void
chc_gameend(ChessInfo* info, ChessGameResult result)
{
    ChessUser* const user1 = &info->user1;
    ChessUser* const user2 = &info->user2;

    if (info->mode == CHESS_MODE_VERSUS) {
    if (info->my == RED) {
        /* 由紅方作 log. 記的是下棋前的原始分數 */
        /* NOTE, 若紅方斷線則無 log */
        time_t t = time(NULL);
        char buf[100];
        sprintf(buf, "%s %s(%d,W%d/D%d/L%d) %s %s(%d,W%d/D%d/L%d)\n",
            ctime(&t),
            user1->userid, user1->rating, user1->win,
            user1->tie, user1->lose,
            (result == CHESS_RESULT_TIE ? "和" :
             result == CHESS_RESULT_WIN ? "勝" : "負"),
            user2->userid, user2->rating, user2->win,
            user2->tie, user2->lose);
        buf[24] = ' '; // replace '\n'
        log_file(BBSHOME "/log/chc.log", LOG_CREAT, buf);
    }

    user1->rating = user1->orig_rating;
    user1->lose--;
    if (result == CHESS_RESULT_WIN) {
        count_chess_elo_rating(user1, user2, 1.0);
        user1->win++;
        currutmp->chc_win++;
    } else if (result == CHESS_RESULT_LOST) {
        count_chess_elo_rating(user1, user2, 0.0);
        user1->lose++;
        currutmp->chc_lose++;
    } else {
        count_chess_elo_rating(user1, user2, 0.5);
        user1->tie++;
        currutmp->chc_tie++;
    }
    currutmp->chess_elo_rating = user1->rating;
    chcusr_put(&cuser, user1);
    passwd_update(usernum, &cuser);
    }
}

int
chc_main(void)
{
    return ChessStartGame('c', SIG_CHC, "楚河漢界之爭");
}

int
chc_personal(void)
{
    chc(0, CHESS_MODE_PERSONAL);
    return 0;
}

int
chc_watch(void)
{
    return ChessWatchGame(&chc, CHC, "楚河漢界之爭");
}