永远不会结束的战争游戏

时间:2017-05-21 00:50:32

标签: c++

大家好,我试图写一场战争游戏,但我的代码会产生永无止境的游戏。当我看到输出时,我看到有一个循环,其中玩家1和玩家2交替赢得轮次。评估谁应该获胜看起来是正确的。几乎看起来像玩家一和玩家双手均匀匹配。我在比赛开始之前一直在洗牌,所以我不知道这是怎么回事。我不确定这里发生了什么。我也使用c ++ 98。

using namespace std;

#define SSTR( x ) static_cast< std::ostringstream & >( \
        ( std::ostringstream() << std::dec << x ) ).str()

void shuffleDeck(vector<string> &);

void splitDeck(vector<string> vDeck, vector<string> &, vector<string> &);

int compareRound(char, char);

int main(int argc, char** argv) {
vector<string> vPlayerOneCards;
vector<string> vPlayerTwoCards;

vector<string> vDeck;
for (int i = 2; i < 15; i++){
    for (int e = 0; e < 4; e++){
        string currentCard;
        if (i < 11){
            currentCard += SSTR( i ).c_str();
        }
        else if (i == 11){
            currentCard += "Jack";
        }
        else if (i == 12){
            currentCard += "Queen";
        }
        else if (i == 13){
            currentCard += "King";
        }
        else if (i == 14){
            currentCard += "Ace";
        }

        currentCard += " of ";

        if (e == 0){
            currentCard += "Hearts";
        }
        else if (e == 1){
            currentCard += "Diamonds";
        }
        else if (e == 2){
            currentCard += "Clubs";
        }
        else if (e == 3){
            currentCard += "Spades";
        }
        vDeck.push_back(currentCard);
    }
}

shuffleDeck(vDeck);

splitDeck(vDeck, vPlayerOneCards, vPlayerTwoCards);

for (int i = 0; i < 26; i++){
    cout << vPlayerOneCards[i] << endl;
}

cout << endl << endl << endl;

for (int i = 0; i < 26; i++){
    cout << vPlayerTwoCards[i] << endl;
}

// This is the pile of cards that the winner of the round get's to keep.
vector<string> cardPool;
int counter = 0;
while (vPlayerOneCards.size() > 0 && vPlayerTwoCards.size() > 0){
    counter++;
    cardPool.push_back(vPlayerTwoCards[0]);
    cardPool.push_back(vPlayerOneCards[0]);

    cout << "Player One: " + vPlayerOneCards[0] + "." << endl << "VS" << endl << "Player Two: " + vPlayerTwoCards[0] + "." << endl;

    int roundWinner = compareRound(vPlayerOneCards[0][0], vPlayerTwoCards[0][0]);
    if (roundWinner == 0){
        cout << "Player One Wins The Round." << endl;
        while (cardPool.size() > 0){
            vPlayerOneCards.push_back(cardPool[cardPool.size()-1]);
            cardPool.erase(cardPool.end());
        }
    }
    else if (roundWinner == 1){
        cout << "Player Two Wins The Round." << endl;
        while (cardPool.size() > 0){
            vPlayerTwoCards.push_back(cardPool[cardPool.size()-1]);
            cardPool.erase(cardPool.end());
        }
    }
    else {
        cout << "The Round Is A Tie." << endl;
    }
    cout << endl;

    vPlayerOneCards.erase(vPlayerOneCards.begin());
    vPlayerTwoCards.erase(vPlayerTwoCards.begin());
    cout << vPlayerOneCards.size() << endl;
    cout << vPlayerTwoCards.size() << endl;
}

if (vPlayerOneCards.size() > 0){
    cout << "Player One Wins The Game Of WAR!!!!!";
}
else {
    cout << "Player Two Wins The Game Of WAR!!!!!";
}

return 0;
}

void shuffleDeck(vector<string> &vDeck){
    srand(time(0));
    random_shuffle(vDeck.begin(), vDeck.end());
}

void splitDeck(vector<string> vDeck, vector<string> &vPlayerOneCards, vector<string> &vPlayerTwoCards){
    for (int i = 1; i < 53; i++){
        if (i % 2 == 0){
            vPlayerOneCards.push_back(vDeck[i-1]);
        }
        else {
            vPlayerTwoCards.push_back(vDeck[i-1]);
        }
    }
}

int compareRound(char p1, char p2){
    char cArr[] = {'2', '3', '4', '5', '6', '7', '8', '9', '10', 'J', 'Q', 'K', 'A'};
    vector<char> cardPrecedence(cArr, cArr + sizeof(cArr) / sizeof(cArr[0]) );
    int p1Precedence = distance(cardPrecedence.begin(), find(cardPrecedence.begin(), cardPrecedence.end(), p1));
    int p2Precedence = distance(cardPrecedence.begin(), find(cardPrecedence.begin(), cardPrecedence.end(), p2));
    if (p1Precedence > p2Precedence){
        return 0;
    }
    else if (p2Precedence > p1Precedence){
        return 1;
    }
    else {
        return 2;
    }
}

1 个答案:

答案 0 :(得分:2)

嘿我弄明白我做错了什么。在我取消正在全面播放的两张牌之前,我正在为获胜者手牌添加牌。更改此功能后,每件事情都开始正常工作。

    cardPool.push_back(vPlayerTwoCards[0]);
    cardPool.push_back(vPlayerOneCards[0]);
    vPlayerOneCards.erase(vPlayerOneCards.begin());
    vPlayerTwoCards.erase(vPlayerTwoCards.begin());

    cout << "Player One: " + vPlayerOneCards[0] + "." << endl << "VS" << endl << "Player Two: " + vPlayerTwoCards[0] + "." << endl;

    int roundWinner = compareRound(vPlayerOneCards[0][0], vPlayerTwoCards[0][0]);
    if (roundWinner == 0){
        cout << "Player One Wins The Round." << endl;
        while (cardPool.size() > 0){
            vPlayerOneCards.push_back(cardPool[cardPool.size()-1]);
            cardPool.erase(cardPool.end());
        }
    }
    else if (roundWinner == 1){
        cout << "Player Two Wins The Round." << endl;
        while (cardPool.size() > 0){
            vPlayerTwoCards.push_back(cardPool[cardPool.size()-1]);
            cardPool.erase(cardPool.end());
        }
    }
    else {
        cout << "The Round Is A Tie." << endl;
    }
    cout << endl;

    cout << vPlayerOneCards.size() << endl;
    cout << vPlayerTwoCards.size() << endl;