📄 chesstool.c
字号:
restartchess(); putmsg( "Move pieces with left button");}staticflash_proc(item, event) Panel_item item; struct inputevent *event;{ flashon ^= 1;}restartchess(){ closepipes(); pipefd = startchess(av);}resetboard(){ int i,j; for(i = 0; i < 8; i++) for(j = 0; j < 8; j++) { piecearr[i][j] = init_piecearr[i][j]; colorarr[i][j] = init_colorarr[i][j]; } movecnt = wcapcnt = bcapcnt = 0; thinking = 0; done = 0; startclock(); unhourglass();}staticchess_event(canvas, ie) Canvas canvas; struct inputevent *ie;{ int row, col, x, y; static up = 1; static oldx, oldy; static int oldrow, oldcol, oldpiece; static startx, starty; if (thinking && ie->ie_code == MS_LEFT) { putmsg( "It's not your turn!"); return; } else if (done) { putmsg( "Game is Over!"); return; } if (ie->ie_code == LOC_RGNEXIT) { if (!up) { up = 1; pwwrite(chessboard_pixwin, SQUARESIZE * oldrow + 1 + oldx, SQUARESIZE * (8 - oldcol) + 1 + oldy, (SQUARESIZE - 1), (SQUARESIZE - 1), PIX_SRC ^ PIX_DST, outline_pr[oldpiece], 0, 0); return; } else return; } if (ie->ie_code == MS_LEFT) { row = (ie->ie_locx) / SQUARESIZE; col = 8 - ((ie->ie_locy) / SQUARESIZE); if (rotate) { row = 7 - row; col = 7 - col; } if (win_inputposevent(ie)) { if (row < 0 || row > 7 || col < 0 || col > 7) return; up = 0; oldrow = row; oldcol = col; oldpiece = piecearr[row][col]; if (oldpiece != 0) { pwwrite(chessboard_pixwin, SQUARESIZE*oldrow + 1 + 1, SQUARESIZE*(8 - oldcol) + 1 + 1, (SQUARESIZE - 1), (SQUARESIZE - 1), PIX_SRC ^ PIX_DST, outline_pr[oldpiece], 0, 0); startx = ie->ie_locx; starty = ie->ie_locy; oldx = 1; oldy = 1; } } else { if (oldpiece != 0 && !up) { up = 1; pwwrite(chessboard_pixwin, SQUARESIZE * oldrow + 1 + oldx, SQUARESIZE * (8 - oldcol) + 1 + oldy, (SQUARESIZE - 1), (SQUARESIZE - 1), PIX_SRC ^ PIX_DST, outline_pr[oldpiece], 0, 0); } else { up = 1; return; } if (row < 0 || row > 7 || col < 0 || col > 7) return; if ((oldrow != row || oldcol != col) && (sendmove(oldrow, oldcol, row, col) >= 0)) { make_move(oldrow, oldcol, row, col); } } return; } if (ie->ie_code == LOC_MOVE && event_is_down(ie)) { if (up || oldpiece == 0) return; if (!rotate) { x = ie->ie_locx - startx; y = ie->ie_locy - starty; } else { x = startx - ie->ie_locx; y = starty - ie->ie_locy; } pwwrite(chessboard_pixwin, SQUARESIZE * oldrow + 1 + x, SQUARESIZE * (8 - oldcol) + 1 + y, (SQUARESIZE - 1), (SQUARESIZE - 1), PIX_SRC ^ PIX_DST, outline_pr[oldpiece], 0, 0); pwwrite(chessboard_pixwin, SQUARESIZE * oldrow + 1 + oldx, SQUARESIZE * (8 - oldcol) + 1 + oldy, (SQUARESIZE - 1), (SQUARESIZE - 1), PIX_SRC ^ PIX_DST, outline_pr[oldpiece], 0, 0); oldx = x; oldy = y; return; } /* fprintf(stderr, "internal error: %x is illegal ie->ie_code\n", ie->ie_code); exit(1); */}paint_chessboard(){ register int i, j; pw_writebackground(chessboard_pixwin, 0, 0, BIG, BIG, PIX_CLR); for (i = 0; i < 9; i++) { pw_vector(chessboard_pixwin, SQUARESIZE * i, SQUARESIZE, SQUARESIZE * i, BOARDSIZE + SQUARESIZE, PIX_SET, 1); pw_vector(chessboard_pixwin, 0, SQUARESIZE * (i + 1), BOARDSIZE, SQUARESIZE * (i + 1), PIX_SET, 1); } for (i = 0; i < 8; i++) { for (j = 0; j < 8; j++) { paint_square(i, j); } } for (i = 0; i < bcapcnt; i++) paint_capture(BLACK, i); for (i = 0; i < wcapcnt; i++) paint_capture(WHITE, i); paint_clocks();}paint_square(i, j) register int i, j;{ int k; if ((i + j) % 2 == 1) pwwrite(chessboard_pixwin, SQUARESIZE * i + 1, SQUARESIZE * (8-j) + 1, (SQUARESIZE - 1), (SQUARESIZE - 1), PIX_SRC, &light_square, 0, 0); else pwwrite(chessboard_pixwin, SQUARESIZE * i + 1, SQUARESIZE * (8-j) + 1, (SQUARESIZE - 1), (SQUARESIZE - 1), PIX_SRC, &dark_square, 0, 0); if ((k = piecearr[i][j]) == 0) return; if (colorarr[i][j] == BLACK) { pwwrite(chessboard_pixwin, SQUARESIZE * i + 1, SQUARESIZE * (8 - j) + 1, (SQUARESIZE - 1), (SQUARESIZE - 1), PIX_SRC | PIX_DST, piece_pr[k], 0, 0); pwwrite(chessboard_pixwin, SQUARESIZE * i + 1, SQUARESIZE * (8 - j) + 1, (SQUARESIZE - 1), (SQUARESIZE - 1), PIX_NOT(PIX_SRC) & PIX_DST, outline_pr[k], 0, 0); } else if (colorarr[i][j] == WHITE) { pwwrite(chessboard_pixwin, SQUARESIZE * i + 1, SQUARESIZE * (8 - j) + 1, (SQUARESIZE - 1), (SQUARESIZE - 1), PIX_NOT(PIX_SRC) & PIX_DST, piece_pr[k], 0, 0); pwwrite(chessboard_pixwin, SQUARESIZE * i + 1, SQUARESIZE * (8 - j) + 1, (SQUARESIZE - 1), (SQUARESIZE - 1), PIX_SRC | PIX_DST, outline_pr[k], 0, 0); }}clearwcaparea(){ pwwritebackground(chessboard_pixwin, 1, 1, 7*SQUARESIZE - 1, (SQUARESIZE - 1), PIX_CLR);}clearbcaparea(){ pwwritebackground(chessboard_pixwin, 1, 9 * SQUARESIZE + 1, 7*SQUARESIZE - 1, (SQUARESIZE - 1), PIX_CLR);}paint_capture(color, cnt){ int k; if (color == BLACK) { k = bcapture[cnt]; pwwrite(chessboard_pixwin, capturepos[cnt] + 1, 9*SQUARESIZE + 1, (SQUARESIZE - 1), (SQUARESIZE - 1), PIX_SRC | PIX_DST, piece_pr[k], 0, 0); pwwrite(chessboard_pixwin, capturepos[cnt] + 1, 9*SQUARESIZE + 1, (SQUARESIZE - 1), (SQUARESIZE - 1), PIX_NOT(PIX_SRC) & PIX_DST, outline_pr[k], 0, 0); } else { k = wcapture[cnt]; pwwrite(chessboard_pixwin, capturepos[cnt] + 1, 1, (SQUARESIZE - 1), (SQUARESIZE - 1), PIX_NOT(PIX_SRC) & PIX_DST, piece_pr[k], 0, 0); pwwrite(chessboard_pixwin, capturepos[cnt] + 1, 1, (SQUARESIZE - 1), (SQUARESIZE - 1), PIX_SRC | PIX_DST, outline_pr[k], 0, 0); }}static Notify_valuechesspipeready(clnt, fd) int clnt; int fd;{ readfromchess(); return(NOTIFY_DONE);}static intkillkids(){ killpg(getpgrp(), SIGINT);/* get rid of forked processes */}static showinghour;hourglass(){ int fd; if (showinghour) return; fd = (int) window_get(panel, WIN_FD); win_getcursor(fd, &oldcursor1); win_setcursor(fd, &hourglasscursor); fd = (int) window_get(chessboard, WIN_FD); win_getcursor(fd, &oldcursor2); win_setcursor(fd, &hourglasscursor); fd = (int) window_get(frame, WIN_FD); win_getcursor(fd, &oldcursor3); win_setcursor(fd, &hourglasscursor); showinghour = 1;}unhourglass(){ int fd; if (!showinghour) return; fd = (int) window_get(panel, WIN_FD); win_setcursor(fd, &oldcursor1); fd = (int) window_get(chessboard, WIN_FD); win_setcursor(fd, &oldcursor2); fd = (int) window_get(frame, WIN_FD); win_setcursor(fd, &oldcursor3); showinghour = 0;}/* * 1 sec timer: If waiting for human and iconic, flash icon. * Otherwise just update clocks. */Notify_valueclock_itimer(clnt, which) Notify_client clnt;{ if (window_get(frame, FRAME_CLOSED)) { if (!thinking && flashon) flashicon(); } else update_clock();}flashicon(){ Pixwin *pw; int w, h; pw = (Pixwin *)window_get(frame, WIN_PIXWIN); h = (int)window_get(frame, WIN_HEIGHT); w = (int)window_get(frame, WIN_WIDTH); pw_writebackground(pw, 0, 0, w, h, PIX_NOT(PIX_DST)); slp(2); pw_writebackground(pw, 0, 0, w, h, PIX_NOT(PIX_DST));}slp(x){ struct timeval tv; tv.tv_sec = x / 64; tv.tv_usec = (x % 64) * (1000000/64); select(32, 0, 0, 0, &tv);}pwwrite(pw, xd, yd, w, h, op, pr, xs, ys) struct pixwin *pw; struct pixrect *pr;{ if (!rotate) pw_write(pw, xd, yd, w, h, op, pr, xs, ys); else pw_write(pw, BOARDSIZE - xd - w, BOARDSIZE + 2*SQUARESIZE - yd - h, w, h, op, pr, xs, ys);}pwwritebackground(pw, xd, yd, w, h, op) struct pixwin *pw;{ if (!rotate) pw_writebackground(pw, xd, yd, w, h, op); else pw_writebackground(pw, BOARDSIZE - xd - w, BOARDSIZE + 2*SQUARESIZE - yd - h, w, h, op);}putmsg(str) char *str;{ panel_set(msg_item, PANEL_LABEL_STRING, str, 0);}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -