嗨,这是我的第一篇文章。如果我不遵守某些规则或约定,我会道歉。如果是这种情况,请告诉我。
我有一个游戏在一个循环中运行,直到任一玩家达到分数限制,此时另一个玩家有最后一次(迭代)机会击败第一个玩家得分。但是,在达到分数限制后,循环继续运行并且永远不会检查获胜者。
#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;
}
答案 0 :(得分:5)
suddenDeath == true;
// ^^
是一个表达式,意思是&#34;比较这两个值&#34;,然后扔掉。 C语句42;
同样有效,同样没用(a)。
您希望指定值,因此您可以使用:
suddenDeath = true;
// ^
它实际上是人们分配而不是比较的更常见if (a = 0)
问题的其他结尾。
(a)如果你想知道为什么任何心智正常的人都会允许这种语言成为一种语言,它实际上允许一些功能强大的构造用最少的代码
并且,您最有可能在之前看过它。声明i++;
就是这样的野兽。它是一个表达式i
(你在这里丢弃)和副作用,i
之后会增加。
答案 1 :(得分:1)
suddenDeath = true;
使用单个=
进行分配。 ==
用于条件检查。