有谁能告诉我为什么这些功能不能给我一个合理的频谱的结果? [英] Can anyone tell me why these functions are not giving me a result in a reasonable spectrum?

查看:141
本文介绍了有谁能告诉我为什么这些功能不能给我一个合理的频谱的结果?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的完整代码如下,它应该模拟一个掷骰子和打印详细信息的游戏给用户,并允许投注如果用户想要它。一切功能除了实际的掷骰游戏。不是循环,而是没有与crapsResult关联的真值,它找到一个真实值和一个负数的不可理解的字符串。任何帮助将不胜感激。

The full code I am using is listed below, it is supposed to simulate a game of craps and print details to the user and allow for betting if the user desires it. Everything functions except for the actual craps game. Instead of looping only while there is not a truth value associated to crapsResult, it finds one real value and an incomprehensible string of a single negative number. Any help would be appreciated.

int main()
{
    //Declare the user input variables
    int gamesPlayed = 0;
    char inputPrint = ' ';
    char isBetting = ' ';
    int startingBet = 0;

    //Declare the variables used by the program
    int endingBet = 0;
    int currentGame = 0;
    bool crapsResult;
    int gamesWon = 0;
    int gamesLost = 0;
    double percentWon = 0;
    bool detailPrint = false;

    //Prompt the user to input their variables
    cout << "Enter the number of games to be played: ";
    cin >> gamesPlayed;
    while(gamesPlayed < 1)
    {
        cout << "   Error: must be greater than 0" << endl;
        cout << "Enter the number of games to be played: ";
        cin >> gamesPlayed;
        cin.clear();
        cin.ignore();
    }

    cout << "Do you wish to print details (Y/N): ";
    cin >> inputPrint;
    if(inputPrint == 'y' || inputPrint == 'Y')
    {
        detailPrint = true;
    }

    cout << "Do you wish to bet (Y/N): ";
    cin >> isBetting;

    if(isBetting == 'y' || isBetting == 'Y')
    {
        cout << "Enter money to start betting with: ";
        cin >> startingBet;
        while(startingBet < 1)
        {
            cout << "   Error: must be greater than 0" << endl;
            cout << "Enter the number of games to be played: ";
            cin >> gamesPlayed;
            cin.clear();
            cin.ignore();
        }
    }
    //Seed the random number generator
    srand(time(NULL));

    //Set a value for ending bet
    if(startingBet == 0)
    {
        endingBet = 1;
    }
    else
    {
        endingBet = startingBet;
    }
    //Call playcraps to simulate the game for as many games as the user input
    for(currentGame = 1; currentGame <= gamesPlayed && endingBet > 0; currentGame += 1)
    {
        crapsResult = NULL;
        crapsResult = playCraps(currentGame, detailPrint, isBetting, startingBet);
        if(crapsResult == true)
        {
            gamesWon += 1;
            endingBet = betting(endingBet, crapsResult);
        }
        if(crapsResult == false)
        {
            gamesLost += 1;
            endingBet = betting(endingBet, crapsResult);
        }
        if((isBetting == 'Y' || isBetting == 'y') && (detailPrint == true))
        {
            cout << "Money left is $" << endingBet << endl;
        }
    }

    //Calculate the percentage of games won
    percentWon = (double(gamesWon) / double(currentGame-1)) * 100.0;

    //Print the results to the user
    if(isBetting == 'Y' || isBetting == 'y')
    {
        cout << "Money at end of games is $" << endingBet << endl;
    }
    cout << "The number of games played is " << currentGame - 1 << endl;
    cout << "The number of games won is " << gamesWon << endl;
    cout << "The number of games lost is " << gamesLost << endl;
    cout << "The percent of games won is " << fixed << showpoint << setprecision(3) << percentWon << endl;
}

//Simulates the roll of a single die and returns the result
int roll()
{
    int rollResult = 0;
    rollResult = rand() % 6 + 1;
    return rollResult;
}

//Calls roll twice and returns the sum of the two results
int roll2Dice()
{
    //Declare variables for this function
    int rollOne = 0;
    int rollTwo = 0;
    int rollSum = 0;

    //Find rollOne and rollTwo
    rollOne = roll();
    rollTwo = roll();

    //Find rollSum
    rollSum = rollOne + rollTwo;

    return rollSum;
}

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;
    }
    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;
}

int betting(int endingBet, bool crapsResult)
{
    if(crapsResult == true)
    {
        endingBet += 1;
    }
    else if(crapsResult == false)
    {
        endingBet -= 1;
    }
    return endingBet;
}


推荐答案

读取所有代码(所以可能还有其他错误),但这一行肯定有问题:

Just skimmed and didn't read all of your code (so there may be other things wrong too), but this line is definitely problematic:

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

逻辑上不可能 crapsResult 同时 true false,因此不会输入循环。

It is logically impossible for crapsResult to simultaneously be both true and false, so that loop will never be entered.

这篇关于有谁能告诉我为什么这些功能不能给我一个合理的频谱的结果?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆