中国象棋是由两人轮流走子,分执红黑棋子,以“将死”或“困毙”对方将(帅)为胜,双方子力相同,每方各16颗(7种)棋子,在9*10的交叉线上交战,其中的开局套路、中局搏杀、残局破解变化无穷。 因为本次活动只考虑界面,故算法极为简陋(最大最小值)。先show一下界面: RT1052做SoC,勉强可以执行 摘出象棋的棋子走法 bool canMoveJu(uint8_t id,uint8_t dstrow,uint8_t dstcol){ if (dstrow>9) return false; if (dstcol>8) return false; if (getStoneId(dstrow,dstcol)>=0 && mystone[id].isred==mystone[getStoneId(dstrow,dstcol)].isred) return false; if (getStoneNum(mystone[id].row,mystone[id].col,dstrow,dstcol)==0){ return true; }else{ return false; } } bool canMoveShuai(uint8_t id,uint8_t dstrow,uint8_t dstcol){ if (dstrow>9) return false; if (dstcol>8) return false; if (getStoneId(dstrow,dstcol)>=0 && mystone[id].isred==mystone[getStoneId(dstrow,dstcol)].isred) return false; if (mystone[getStoneId(dstrow,dstcol)].type==0){ return canMoveJu(id,dstrow,dstcol); } if (dstcol>5 || dstcol<3) return false; int r = relation(mystone[id].row, mystone[id].col, dstrow, dstcol); if (r != 1 && r != 10) return false; if (mystone[id].isred && dstrow<7) return false; if (!mystone[id].isred && dstrow>2) return false; return true; } bool canMovePao(uint8_t id,uint8_t dstrow,uint8_t dstcol){ if (dstrow>9) return false; if (dstcol>8) return false; if (getStoneId(dstrow,dstcol)>=0 && mystone[id].isred==mystone[getStoneId(dstrow,dstcol)].isred) return false; if (getStoneId(dstrow,dstcol)==-1){ return canMoveJu(id,dstrow,dstcol); }else{ if (mystone[getStoneId(dstrow,dstcol)].isexist && getStoneNum(mystone[id].row,mystone[id].col,dstrow,dstcol)==1 && mystone[id].isred!=mystone[getStoneId(dstrow,dstcol)].isred){ return true; }else{ return false; } } } bool canMoveMa(uint8_t id,uint8_t dstrow,uint8_t dstcol){ if (dstrow>9) return false; if (dstcol>8) return false; if (getStoneId(dstrow,dstcol)>=0 && mystone[id].isred==mystone[getStoneId(dstrow,dstcol)].isred) return false; int r = relation(mystone[id].row, mystone[id].col, dstrow, dstcol); if (r != 12 && r != 21) return false; if (r==12 && getStoneId(mystone[id].row,(mystone[id].col+dstcol)/2)>=0) return false; if (r==21 && getStoneId((mystone[id].row+dstrow)/2,mystone[id].col)>=0) return false; return true; } bool canMoveXiang(uint8_t id,uint8_t dstrow,uint8_t dstcol){ if (dstrow>9) return false; if (dstcol>8) return false; if (getStoneId(dstrow,dstcol)>=0 && mystone[id].isred==mystone[getStoneId(dstrow,dstcol)].isred) return false; int r = relation(mystone[id].row, mystone[id].col, dstrow, dstcol); if(r != 22) return false; if(mystone[id].isred && dstrow<5) return false; if(!mystone[id].isred && dstrow>4) return false; if(getStoneId((mystone[id].row+dstrow)/2,(mystone[id].col+dstcol)/2)>=0) return false; return true; } bool canMoveShi(uint8_t id,uint8_t dstrow,uint8_t dstcol){ if (dstrow>9) return false; if (dstcol>8) return false; if (getStoneId(dstrow,dstcol)>=0 && mystone[id].isred==mystone[getStoneId(dstrow,dstcol)].isred) return false; int r = relation(mystone[id].row, mystone[id].col, dstrow, dstcol); if(r != 11) return false; if(dstcol>5 || dstcol<3) return false; if(mystone[id].isred && dstrow<7) return false; if(!mystone[id].isred && dstrow>2) return false; return true; } bool canMoveBing(uint8_t id,uint8_t dstrow,uint8_t dstcol){ if (dstrow>9) return false; if (dstcol>8) return false; if (getStoneId(dstrow,dstcol)>=0 && mystone[id].isred==mystone[getStoneId(dstrow,dstcol)].isred) return false; int r = relation(mystone[id].row, mystone[id].col, dstrow, dstcol); if (r != 10 && r !=01) return false; if (mystone[id].isred){ if(mystone[id].row<dstrow) return false; if(mystone[id].row>4 && mystone[id].row<=dstrow) return false; }else{ if(mystone[id].row>dstrow) return false; if(mystone[id].row<5 && mystone[id].row>=dstrow) return false; } return true; } bool canMove(uint8_t dstrow,uint8_t dstcol){ if(selectedId==-1){ //PRINTF("no selected\r\n"); return false; } if(redturn!=mystone[selectedId].isred){ return false; } switch (mystone[selectedId].type){ case 0: return canMoveShuai(selectedId,dstrow,dstcol); case 1: return canMoveShi(selectedId,dstrow,dstcol); case 2: return canMoveXiang(selectedId,dstrow,dstcol); case 3: return canMoveMa(selectedId,dstrow,dstcol); case 4: return canMoveJu(selectedId,dstrow,dstcol); case 5: return canMovePao(selectedId,dstrow,dstcol); case 6: return canMoveBing(selectedId,dstrow,dstcol); default: //PRINTF("default false\r\n"); return false; } }
|