gpt4 book ai didi

c++程序不会在void()函数中退出循环

转载 作者:行者123 更新时间:2023-11-30 02:28:58 25 4
gpt4 key购买 nike

你好,我正在做一项作业,但在执行这部分程序时遇到了问题。

“23”游戏是一种两人游戏,从一堆 23 个牙签开始。玩家轮流,一次取出 1、2 或 3 根牙签。抽出最后一根牙签的玩家输掉了游戏。编写一个播放“23”的人机对战程序。人类应该始终先行。当轮到电脑时,它应该按照以下规则播放:

  • 取出 4 – X 根牙签,其中 X 是人类在上一轮取出的牙签数。

  • 如果还剩下 2 到 4 根牙签,那么计算机应该抽出足够的牙签来留下 1 根。

  • 如果还剩一根牙签,那么电脑就得拿走它,它就输了。

当人类玩家输入要取出的牙签数量时,程序应执行输入验证。确保输入的数字在 1 到 3 之间,并且玩家没有试图取出比堆中现有的牙签更多的牙签。

这是我的代码。任何帮助将不胜感激。

#include <iostream>
using namespace std;

void compAlgorithm(int totalTp, int compTp, int userTp, int countr) {
do {
if (totalTp > 4) {
compTp = 4 - userTp;
totalTp += totalTp - compTp;
countr--;
}
if (totalTp >= 2 && totalTp <= 4) {
switch (totalTp) {
case 2:
totalTp += totalTp - 1;
countr--;
break;
case 3:
totalTp += totalTp - 2;
countr--;
break;
case 4:
totalTp += totalTp - 3;
countr--;
break;
}
}
} while (countr == 1);
}

int main() {
int userTp = 0;
int compTp = 0;
int totalTp = 23;
int countr = 0;

do {
if (countr == 0) {
cout << "please enter a vale of toothpics between 1 and 3" << endl;
cin >> userTp;
totalTp += totalTp - userTp;
countr++;
}
else if (countr == 1) {
compAlgorithm;
}
} while (totalTp >= 2);

if (totalTp == 1 && countr == 1) {
cout << "you win" << endl;
}
else if (totalTp > 0 && totalTp < 2 && countr == 0) {
cout << "please enter a vale of toothpics between 1 and 2" << endl;
cin >> userTp;
totalTp = totalTp - userTp;
switch (totalTp) {
case 1:
cout << "you win" << endl;
break;
case 2:
cout << "you loose" << endl;
break;
}
}
system("pause");
return 0;
}

提前致谢。

最佳答案

这是错误的:

else if (countr == 1) {
compAlgorithm;
}

如果这是您的意图,您必须将参数传递给此函数调用。目前这一行 compAlgorithm; 什么都不做,可能会导致无限循环。

关于c++程序不会在void()函数中退出循环,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40184673/

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