我几乎已经完成了我的程序,它运行得很好,但是当我同时为玩家和AI滚动,并滚动1(半身数)时,它没有设置我的pot = 0。它只会保持运行编号并继续运行。这里有没有我不明白的逻辑?PS -它不让我发布我的全部代码,所以我只发布了部分代码
//AI
int aiTurn(int AI) {
//Variables
int pot = 0;
int count = 0;
char choice = ' ';
cout << "AI turn" << endl;
//While loop until AI turn reaches 20 or score >=50
while (count < 20 && AI < 50) {
int rollValue = diceRoll();
//Incrementing turn
count++;
//Checking for bust
if (rollValue == 1) {
cout << "Die Roll " << rollValue << " : BUST" << endl << endl;
pot = 0;
return AI;
}
//Else increment
else {
pot += rollValue;
cout << "Die Roll : " << rollValue << " Pot : " << pot << endl;
AI += rollValue;
}
}
return AI;
}发布于 2019-02-11 07:43:41
我的(count < 20 && AI < 50)是错误的,对于你所指定的规格,它应该是((count < 20) || (AI < 50))。请参考下面的代码进行进一步的修改。
#include <iostream>
#include <cstdio>
#include <cstdlib>
using std::cout;
using std::endl;
int diceRoll() { return rand() % 6 + 1; }
int aiTurn(int AI) {
int pot = 0;
int AI_value_to_be_returned = 0;
//char choice;
cout << "AI turn\n";
//While loop until AI turn reaches 20 or score >=50
int count = 0;
while ((count < 20) || (AI < 50)) {
int rollValue = diceRoll();
printf("dice roll {%d}\n", rollValue);
//Incrementing turn
//Checking for bust
if (rollValue == 1) {
cout << "Die Roll " << rollValue << " : BUST" << endl << endl;
pot = 0;
AI_value_to_be_returned = AI;
cout << "Die Roll " << rollValue << " : BUST Pot Value = " << pot << endl;
break;
} else {
pot += rollValue;
cout << "Die Roll : " << rollValue << " Pot : " << pot << endl;
AI += rollValue;
}
count++;
}
return AI_value_to_be_returned;
}
int main() { printf("aiTurn {%d}\n", aiTurn(50)); return 0; }输出:
AI turn
dice roll {2}
Die Roll : 2 Pot : 2
dice roll {5}
Die Roll : 5 Pot : 7
dice roll {4}
Die Roll : 4 Pot : 11
dice roll {2}
Die Roll : 2 Pot : 13
dice roll {6}
Die Roll : 6 Pot : 19
dice roll {2}
Die Roll : 2 Pot : 21
dice roll {5}
Die Roll : 5 Pot : 26
dice roll {1}
Die Roll 1 : BUST
Die Roll 1 : BUST Pot Value = 0https://stackoverflow.com/questions/54621940
复制相似问题