gpt4 book ai didi

c++ - 永不结束的 war 游戏

转载 作者:行者123 更新时间:2023-11-30 03:31:37 25 4
gpt4 key购买 nike

大家好,我正在尝试编写 war 游戏,但我的代码生成了永无止境的游戏。当我查看输出时,我发现有一个循环,玩家一和玩家二交替获胜。谁应该获胜的评估看起来是正确的。看起来玩家一和玩家二的手势均力敌。我在比赛开始前洗牌,所以我不明白这怎么可能。我不确定这里发生了什么。我也在使用 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;
}
}

最佳答案

嘿,我知道我做错了什么了。在我移除该轮中正在玩的两张牌之前,我正在向获胜者手中添加牌。更改此功能后,一切都开始正常工作。

    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;

关于c++ - 永不结束的 war 游戏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44092172/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com