0

这是我目前正在使用的代码:

bool playCraps(int currentGame, bool detailPrint, char isBetting, int startingBet)
{
    bool crapsResult = NULL;
    int currentGameStorage[100];
    int currentRoll = 1;
    int point = roll2Dice();
    int printingNumber = 0;
    currentGameStorage[0] = point;
    if(point == 7 || point == 11)
    {
        crapsResult = true;
    }
    else if(point == 2 || point == 3 || point == 12)
    {
        crapsResult = false;
    }
    else
    {
        crapsResult = NULL;
    }
    while(crapsResult != true || crapsResult != false)
    {
        currentGameStorage[currentRoll] = roll2Dice();
        if(currentGameStorage[currentRoll] == point)
        {
            crapsResult = true;
        }
        else if(currentGameStorage[currentRoll] == 7)
        {
            crapsResult = false;
        }
        currentRoll += 1;
    }
    currentRoll -= 1;
    if(detailPrint == true)
    {
        cout << "Game " << currentGame << ": ";
        for(printingNumber = 0; printingNumber <= currentRoll; printingNumber += 1)
        {
            cout << currentGameStorage[printingNumber] << " ";
        }
        if(crapsResult == true)
        {
            cout << "win";
        }
        else if(crapsResult == false)
        {
            cout << "lose";
        }
        cout << endl;
    }
    return crapsResult;
}

每当我运行它时,它都会创建一个 emdless 循环,终端中没有出现任何文本。函数 roll2Dice() 使用 rand() 函数模拟两个六面骰子的掷骰,并将两个结果相加。任何帮助,将不胜感激。

4

4 回答 4

2

您的 while 测试crapsResult != true || crapsResult != false必然为真,因此不会终止。该crapsResult值是真或假,这将使表达式的两半之一为真,另一半为假。两者true || falsefalse || true评估为 true

于 2013-11-01T01:08:22.513 回答
1

正如其他人所指出的,很明显为什么循环永远不会结束。

您对 NULL 的使用让我觉得您希望您可以拥有一个可以保存三个值(未设置、真和假)的布尔值。您可以通过使用指向 bool 的指针以最小的更改来实现这一点,但这非常令人作呕。枚举是您真正需要的:

enum CrapsResult
{
    unrolled,
    true_result,
    false_result
};

然后相关代码变为*:

CrapsResult crapsResult = unrolled;
int currentGameStorage[100];
int currentRoll = 1;
int point = roll2Dice();
int printingNumber = 0;
currentGameStorage[0] = point;
if(point == 7 || point == 11)
{
    crapsResult = true_result;
}
else if(point == 2 || point == 3 || point == 12)
{
    crapsResult = false_result;
}
else
{
    crapsResult = unrolled;
}

while(crapsResult == unrolled)
{
    currentGameStorage[currentRoll] = roll2Dice();
    if(currentGameStorage[currentRoll] == point)
    {
        crapsResult = true_result;
    }
    else if(currentGameStorage[currentRoll] == 7)
    {
        crapsResult = false_result;
    }
    currentRoll += 1;
}

*我实际上并没有编译这个。

于 2013-11-01T01:14:58.603 回答
0

在 C 和 C++ 中,表达式可以根据以下条件解析为真或假:0 是false,其他都是true。C 的宏 FALSE 和 TRUE 的正式定义是#define FALSE 0and #define TRUE (!(FALSE))

bool 类型的变量可以是truefalse。您crapsResult使用 NULL 的值初始化您的。NULL 是定义为0ULor的宏0ULL,其计算结果为 false。

bool crapsResult = NULL;

相当于

bool crapsResult = false;

因此,当您的代码执行以下操作时:

while (crapsResult != true || crapsResult != false)

总是满足这些条件之一,因此循环总是重复。

您将需要第二个变量,大概是 bool 类型,以确定您是否有可接受的答案。

bool crapsResult = false;
bool haveCrapsResult = false;
int currentGameStorage[100];
int currentRoll = 1;
int point = roll2Dice();
int printingNumber = 0;
currentGameStorage[0] = point;
if(point == 7 || point == 11)
{
    crapsResult = true;
    haveCrapsResult = true;
}
else if(point == 2 || point == 3 || point == 12)
{
    crapsResult = false;
    haveCrapsResult = false;
}

while(!haveCrapsResult)
{
    currentGameStorage[currentRoll] = roll2Dice();
    if(currentGameStorage[currentRoll] == point)
    {
        crapsResult = true;
        haveCrapsResult = true;
    }
    else if(currentGameStorage[currentRoll] == 7)
    {
        crapsResult = false;
        haveCrapsResult = true;
    }
    currentRoll += 1;
}
currentRoll -= 1;
if(detailPrint == true)
{
    cout << "Game " << currentGame << ": ";
    for(printingNumber = 0; printingNumber <= currentRoll; printingNumber += 1)
    {
        cout << currentGameStorage[printingNumber] << " ";
    }
    if(crapsResult == true)
    {
        cout << "win";
    }
    else /*if(crapsResult == false) is redundant */
    {
        cout << "lose";
    }
    cout << endl;
}
return crapsResult;
于 2013-11-01T01:21:42.977 回答
0
while(crapsResult != true || crapsResult != false)

等于

while(crapsResult == false || crapsResult == true)

.

//while(crapsResult == (false || true)) // Don´t matter

所以 boolean 只有 false 或 true

它总是评估为并等于这个表达式

while(true)
于 2013-11-01T01:08:59.100 回答