掷骰子游戏在循环不符合退出条件时执行

时间:2019-10-29 15:59:24

标签: c++ do-while

我正在尝试使用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;
}

运行代码时,即使回滚实际上等于7或点,也永远不会满足do-while循环退出条件。它只是不断循环。我也尝试将int点更改为const int点,但均无效。有人知道为什么会这样吗?

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

编辑:我的查询已解决,感谢您的回答!

1 个答案:

答案 0 :(得分:3)

问题来自此行while (roll != 7 || roll != point);

假设points不等于7;如果roll等于7,则roll != points评估为true,因此循环继续。反之亦然,如果roll == points为真,则roll != 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);