gpt4 book ai didi

c++ - 掷骰子游戏做 while 循环不满足退出条件

转载 作者:行者123 更新时间:2023-11-27 23:36:49 34 4
gpt4 key购买 nike

我正在尝试使用 C++ 模拟掷骰子游戏,其中会进行多次游戏迭代,并记录每次游戏的每次赢、输和掷骰数。

每场比赛的规则:

掷 2 个骰子并对结果求和。 2、3 或 12 = 输(1 卷),7 或 11 = 赢(1 卷)。如果两者都不是,则再次掷出总和的骰子,直到玩家掷出 7(输)或再次掷出他们的原始分数(赢)。

#include<iostream>
#include<cstdlib>
#include<ctime>
#include<vector> // Allows use of vectors

double random(); // Returns random double between 0 and 1
int dice(double x); // Converts a double between 0 and 1 into an int between 1 and 6
int two_dice(int die_one, int die_two); // Sums 2 ints together, in this case dice rolls


int main()
{
// Sets the seed for rand using the current time, i.e. rand()
// will now always generate from a different starting point
std::srand(std::time(nullptr));

// Vectors to store the results of win/lose
std::vector<int> won;
std::vector<int> lose;

for (int i = 0; i < 10; i++)
{
int num_throws = 1;

// Roll two dice
double x = random(), y = random();
int die_one = dice(x);
int die_two = dice(y);
int roll = two_dice(die_one, die_two);

std::cout << "\nRoll " << i + 1 << " = " << roll << "\n";

if (roll < 4 || roll == 12) // Instant loss
{
lose.push_back(num_throws);
}
else if (roll == 7 || roll == 11) // Instant win
{
won.push_back(num_throws);
}
else // Begin looping rolls until a 7 or original roll is rolled again
{
int point = roll; // First roll
std::cout << "Point is " << point << "\n";

do {
num_throws++;
double x = random(), y = random();
int die_one = dice(x);
int die_two = dice(y);
int roll = two_dice(die_one, die_two); // Roll dice again
std::cout << "Next roll = " << roll << "\n";
std::cout << "Point is " << point << "\n";
} while (roll != 7 || roll != point);

if (roll == 7)
{
lose.push_back(num_throws);
std::cout << "lose\n";
}
else if (roll == point)
{
won.push_back(num_throws);
std::cout << "win\n";
}
else
{
std::cout << "\n\nERROR\n\n";
}

std::cout << "Number of throws was " << num_throws << "\n";
}
}

std::cout << "Number of throws for each win:\n";
for (auto i: won)
std::cout << i << "\n";

std::cout << "\nNumber of throws for each lose:\n";
for (auto i : lose)
std::cout << i << "\n";

return 0;
}

double random() // Returns random double between 0 and 1
{
double rand_num = rand() % 1000 + 1;
return rand_num / 1000;
}

int dice(double x) // Converts a double between 0 and 1 into an int between 1 and 6
{
double dice_roll = (x * 6) + 1;
return dice_roll;
}

int two_dice(int die_one, int die_two) // Sums 2 ints together, in this case dice rolls
{
return die_one + die_two;
}

运行代码时,永远不会满足 do-while 循环退出条件,即使 roll 实际上 = 7 或 point。它只是不断循环。我也尝试过将 int point 更改为 const int point,但都不起作用。有谁知道为什么会这样?

我知道这可能被过度评论了,但这是为了让任何人都能快速阅读代码,我已经包含了很多打印语句,因此您最好尝试找出不起作用的地方。

编辑:我的问题已经解决,感谢所有的回答!

最佳答案

问题出在这一行 while (roll != 7 || roll != point);

假设 points 不等于 7;如果 roll 等于 7,则 roll != points 计算结果为 true,因此循环继续。反之亦然,如果 roll == pointsroll != 7 为真,循环继续。

既然你是在 while 循环之后测试结果,我建议使用 break 语句:

do {
num_throws++;
double x = random(), y = random();
int die_one = dice(x);
int die_two = dice(y);
int roll = two_dice(die_one, die_two); // Roll dice again
std::cout << "Next roll = " << roll << "\n";
std::cout << "Point is " << point << "\n";
if (roll == 7)
{
lose.push_back(num_throws);
std::cout << "lose\n";
break;
}
else if (roll == point)
{
won.push_back(num_throws);
std::cout << "win\n";
break;
}
} while (true);

关于c++ - 掷骰子游戏做 while 循环不满足退出条件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58611127/

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