//判断在走的X,Y轴上,moveID和killID之间有多少个棋子 int GameScene::getStoneCountInSingleXY(int moveID, int killID, int tox, int toy){ int count = 0; Stone* moveStone = sts[moveID]; if (moveStone->getX() != tox && moveStone->getY() != toy){ return -1; } int m_x = moveStone->getX(); int m_y = moveStone->getY(); if (m_x == tox){ int y_min = m_y < toy ? m_y : toy; int y_max = m_y > toy ? m_y : toy; for (int i = y_min + 1; i < y_max; i++){ if (getStoneIDByXY(m_x, i) != -1){ count++; } } } else if (m_y == toy){ int x_min = m_x < tox ? m_x : tox; int x_max = m_x > tox ? m_x : tox; for (int i = x_min + 1; i < x_max; i++){ if (getStoneIDByXY(i, m_y) != -1){ count++; } } } else{ return -1; } return count; }
bool GameScene::canMoveBING(int moveID, int killID, int tox, int toy){ Stone* moveStone = sts[moveID]; int m_x = moveStone->getX(); int m_y = moveStone->getY(); int xoff = abs(m_x - tox); int yoff = abs(m_y - toy); int checkXY = xoff * 10 + yoff; if (checkXY != 1 && checkXY != 10){ return false; } if (clickRed == moveStone->getisRed()){//棋盘的下半部分 if (toy < m_y){ return false; } if (m_y <= 4 && m_x != tox){//过河了 return false; } } else{//棋盘的上半部分 if (toy > m_y){ return false; } if (m_y >= 5 && m_x != tox){//过河了 return false; } } return true; }
bool GameScene::canMoveXIANG(int moveID, int killID, int tox, int toy){ Stone* moveStone = sts[moveID]; int m_x = moveStone->getX(); int m_y = moveStone->getY(); int xoff = abs(m_x - tox); int yoff = abs(m_y - toy); if (xoff != 2 || yoff != 2){ return false; } /*判断象眼位置*/ int mid_x = (m_x + tox) / 2; int mid_y = (m_y + toy) / 2; int mid_stone = getStoneIDByXY(mid_x, mid_y); if (mid_stone != -1){ return false; } //如果是在棋盘下面,y如果超过4,就越过楚河汉界了 if (clickRed == sts[moveID]->getisRed()){ if (toy > 4){ return false; } } else{ //如果是在棋盘上面,y如果小于5,就越过楚河汉界了 if (toy < 5){ return false; } } return true; }
bool GameScene::canMoveSHI(int moveID, int tox, int toy){ Stone* moveStone = sts[moveID]; int m_x = moveStone->getX(); int m_y = moveStone->getY(); int xoff = abs(m_x - tox); int yoff = abs(m_y - toy); if (xoff != 1 || yoff != 1){ return false; } /*判断将或帅是否走出9宫格*/ if (tox > 5 || tox < 3){ return false; } if (clickRed == sts[moveID]->getisRed()){ if (toy > 2 || toy < 0){ return false; } } else{ if (toy > 9 || toy < 7){ return false; } } return true; }
bool GameScene::canMoveJIANG(int moveID, int killID, int tox, int toy){ /*如果要杀的对面的将或帅,则直接按车的走法*/ if (killID != -1 && sts[killID]->getType() == Stone::JIANG){ return canMoveJU(moveID, killID, tox, toy); } /*判断是否走的一格*/ Stone* moveStone = sts[moveID]; int m_x = moveStone->getX(); int m_y = moveStone->getY(); int xoff = abs(m_x - tox); int yoff = abs(m_y - toy); int checkXY = xoff * 10 + yoff; if (checkXY != 1 && checkXY != 10){ return false; } /*判断将或帅是否走出9宫格*/ if (tox > 5 || tox < 3){ return false; } if (clickRed == sts[moveID]->getisRed()){ if (toy > 2 || toy < 0){ return false; } } else{ if (toy > 9 || toy < 7){ return false; } } return true; }
bool GameScene::canMoveMA(int moveID, int killID, int tox, int toy){ Stone* moveStone = sts[moveID]; int m_x = moveStone->getX(); int m_y = moveStone->getY(); int xoff = abs(m_x - tox); int yoff = abs(m_y - toy); int checkXY = xoff * 10 + yoff; if (checkXY != 12 && checkXY != 21){ return false; } if (xoff == 2 && (getStoneIDByXY((m_x + tox) / 2, m_y) != -1)){ return false; } else if (yoff == 2 && (getStoneIDByXY(m_x, (m_y + toy) / 2) != -1)){ return false; } else{} return true; }
bool GameScene::rabbitHitByStone() { int rabbitX = rabbit->getX(); int rabbitY = rabbit->getY(); for (StoneAnimation *s: stones) { Stone *stone = s->getStone(); int stoneX = stone->getX(); int stoneY = stone->getY(); float stoneScale = stone->getScale(); if (pow((stoneX-rabbitX), 2.) + pow((stoneY-rabbitY), 2.) < pow(STONE_HIT_DISTANCE, 2.) && stoneScale > .5) { stone->setHidden(true); return true; } } return false; }