功能实现

  1. 完善抽牌
  2. 抽牌池剩余数量

完善抽牌

写一个获取抽牌区卡牌数量的功能

int cardManage::getCardPoolNum(Player player, Card cards[], int length) {
    int num = length;
    for (int i = 0; i < length; i++) {
        if (cards[i].cardState != cards[i].cardPool) {
            num--;
        }
    }
    if (player.humanPlayer) {            //是人类玩家
        playerCardPoolNum = num;
        return playerCardPoolNum;
    } else {
        enemyCardPoolNum = num;
        return enemyCardPoolNum;
    }
}

在抽牌区寻找符合条件的卡牌

vector<int> cardManage::searchCard(Player player, Card cards[], int length, int type, int cost) {
    vector<int> pos;
    if (getCardPoolNum(player, cards, length) == 0) {
        return pos;
    } else {
        switch (type) {
            case -1:
                for (int i = 0; i < length; i++) {
                    if (cards[i].cardState == cards[i].cardPool) {
                        pos.push_back(i);
                    }
                }
                break;
            case 0:case 1:case 2:case 3:case 4:case 5:
                if (cost == -1) {
                    for (int i = 0; i < length; i++) {
                        if (cards[i].cardState == cards[i].cardPool && cards[i].cardType == type) {
                            pos.push_back(i);
                        }
                    }
                } else {
                    for (int i = 0; i < length; i++) {
                        if (cards[i].cardState == cards[i].cardPool && cards[i].cardType == type && cards[i].cost == cost) {
                            pos.push_back(i);
                        }
                    }
                }
                break;
        }
    }
    return pos;
}

再次修改之前的抽牌功能……

void cardManager::getCard(Player* player, Card cards[], int getNum, int cardLength, int cardType) {
    if (player->handCardNums == player->handMaxNum) {                                //手牌已达上限
        return;
    } else if (getNum + player->handCardNums > player->handMaxNum) {                //抽牌后会达到上限
        getNum = player->handMaxNum - player->handCardNums;
    }
    srand((unsigned)time(NULL));                                                    //随机
    vector<int> pos;
    if (player->handCardNums <= player->handMaxNum) {
        for (; getNum != 0;) {
            int num = rand() % cardLength;
            for (int i = 0; i < player->handMaxNum; i++) {
                if (getNum == 0) {
                    return;
                }
                if (player->handCards[i] == &player->nullCard) {
                    switch (cardType) {
                        case -1:
                            pos = searchCard(*player, cards, cardLength, cardType, -1);
                            if (pos.empty()) {
                                return;
                            } else {
                                int randPos = rand() % pos.size();
                                cards[pos[randPos]].cardState = cards[pos[randPos]].handPool;            //卡牌移到手牌区
                                player->handCardNums += 1;                                //手牌数加一
                                player->handCards[i] = &cards[pos[randPos]];                    //手牌数组赋值
                                getNum--;
                                if (player->humanPlayer) {

                                    playerCardPoolNum--;
                                } else {
                                    enemyCardPoolNum--;
                                }
                                break;
                            }
                        case 0:case 1:case 2:case 3:case 4:case 5:
                            pos = searchCard(*player, cards, cardLength, cardType, -1);
                            if (pos.empty()) {
                                return;
                            } else {
                                int randPos = rand() % pos.size();
                                cards[pos[randPos]].cardState = cards[pos[randPos]].handPool;            //卡牌移到手牌区
                                player->handCardNums += 1;                            //手牌数加一
                                player->handCards[i] = &cards[pos[randPos]];                    //手牌数组赋值
                                getNum--;
                                if (player->humanPlayer) {
                                    playerCardPoolNum--;
                                } else {
                                    enemyCardPoolNum--;

                                }
                                break;
                            }
                        case 6:
                            pos = searchCard(*player, cards, cardLength, 2, 1);                    //两张消耗为1的咒术牌
                            if (pos.empty()) {
                                return;
                            } else {
                                int randPos = rand() % pos.size();
                                cards[pos[randPos]].cardState = cards[pos[randPos]].handPool;            //卡牌移到手牌区
                                player->handCardNums += 1;                            //手牌数加一
                                player->handCards[i] = &cards[pos[randPos]];                    //手牌数组赋值
                                getNum--;
                                if (player->humanPlayer) {
                                    playerCardPoolNum--;
                                } else {
                                    enemyCardPoolNum--;
                                }
                                break;
                            }
                        case 7:                        //抽牌到上限

                            break;
                    }
                }
            }
        }
    }
}

再在玩家和敌人回合开始时判断一下抽牌池是不是空,是的话就重置

if (whosTurn == eEnemyTurn) {
    if (enemyCarpente.getCardState) {
        if (cardManage.enemyCardPoolNum == 0) {
            for (int i = 0; i < sizeof(cardManage.enemyCards) / sizeof(cardManage.enemyCards[0]); i++) {            //遍历
                if (cardManage.enemyCards[i].cardState == cardManage.enemyCards[i].disCardPool) {                    //如果是在弃牌堆就转移到抽牌堆中
                    cardManage.enemyCards[i].cardState = cardManage.enemyCards[i].cardPool;
                }
            }
        }
    }
} else if (whosTurn == ePlayerTurn && !gameLose) {
    if (humanPlayer.getCardState) {
        if (cardManage.playerCardPoolNum == 0) {                                                                    //如果卡池抽完了
            for (int i = 0; i < sizeof(cardManage.playerCards) / sizeof(cardManage.playerCards[0]); i++) {            //遍历
                if (cardManage.playerCards[i].cardState == cardManage.playerCards[i].disCardPool) {                    //如果是在弃牌堆就转移到抽牌堆中
                    cardManage.playerCards[i].cardState = cardManage.playerCards[i].cardPool;
                }
            }
        }
        humanPlayer.mp /= 2;                    //每回合魔法减半
        if (humanPlayer.movePower == 0) {        //每回合回复一点行动力
            humanPlayer.movePower = 1;
        }
        cardManage.getCard(&humanPlayer, cardManage.playerCards, humanPlayer.getCardNum, sizeof(cardManage.playerCards) / sizeof(cardManage.playerCards[0]), -1);
    }
}

抽卡池剩余卡牌绘制

void cardManager::initCards() {
    textFont.loadFromFile("./data/font/simsun.ttc");
    textCardPoolNum.setFont(textFont);
    textCardPoolNum.setCharacterSize(20);
    textCardPoolNum.setPosition(1202, 810);
    textCardPoolNum.setFillColor(Color::Black);
}
void cardManager::drawCardPoolNum(RenderWindow* window) {
    textCardPoolNum.setString(to_string(playerCardPoolNum));
    window->draw(textCardPoolNum);
}
void Game::drawPlayer() {
    cardManage.drawCardPoolNum(&window);
}

大致效果

Last modification:February 12th, 2021 at 06:39 am