gpt4 book ai didi

c++ - 无法退出 while 循环与 boolean 条件 c++

转载 作者:太空宇宙 更新时间:2023-11-04 15:39:32 25 4
gpt4 key购买 nike

<分区>

您好,这是我的第一篇文章。如果我没有遵守某些规则或惯例,我深表歉意。如果是这样,请告诉我。

我有一个在 while 循环中运行的游戏,直到任一玩家达到得分限制,此时另一个玩家有最后(迭代)机会击败第一个玩家得分。然而,在达到分数限制后,循环继续运行并且永远不会检查获胜者。

#include <iostream>
#include <stdlib.h>
#include <time.h>
#include <string>
using namespace std;

int roll();
int playTurn(int);

int main(){

const int LIMIT = 5;
int whoseTurn = 1;
int pnts1 = 0;
int pnts2 = 0;
bool suddenDeath = false; //True when score limit is reached



while(!suddenDeath){

if(pnts1 >= LIMIT || pnts2 >= LIMIT){ //Limit was reached by previous player.
suddenDeath == true; //Next player has 1 turn to win

}

if(whoseTurn == 1){
pnts1 += playTurn(whoseTurn); //Play turn and tally points
whoseTurn = 2; //Swith player for next iteration
}
else if(whoseTurn == 2){
pnts2 += playTurn(whoseTurn);
whoseTurn = 1;
}

cout << "-------------------------------------" << endl //Display score
<< "Player 1 has " << pnts1 << " points" << endl
<< "Player 2 has " << pnts2 << " points" << endl
<< "-------------------------------------" << endl << endl;

};

if(pnts1 > pnts2)
cout << "Congratulations Player 1! You won with a score of: " << pnts1 << " - " << pnts2;
else if(pnts2 > pnts1)
cout << "Congratulations Player 2! You won with a score of: " << pnts2 << " - " << pnts1;
else if(pnts1 == pnts2)
cout << "A tie! What are the chances?";

return 0;

}

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