C ++ Darts Game:Loop不会第二次退出

时间:2015-04-26 14:05:11

标签: c++ loops

我目前正在使用C ++编写501飞镖游戏(或多个游戏)的模拟器,并且在我的代码中遇到了问题。

该地区的目的是在两名球员之间模拟世界锦标赛决赛飞镖的10,000场比赛。一场比赛由两名球员组成,最佳的13组(第一组至第7组),一组是5场比赛中最好的(第一场至第3场)。在每个游戏中,两个玩家都以501的分数开始,并尝试在另一个玩家之前将他们的分数降低到0。我正在模拟其中的10,000个比赛,以便获得两个球员之间每个可能结果的频率。

我的球员在他们的技能属性和得分数等等的类中被宣布,方法是throw_quick_set();它模拟了玩家在另一个玩家继续前进之前投掷一组3个飞镖的情况。

我的问题是,在赢得第一场比赛后(该计划看到一个有7套的球员) - 第二场比赛永远持续下去,一名球员赢得了无数的比赛。我之所以看到这一点,是因为我在玩家赢了一场比赛时所包含的输出短语,或者当我注意到某些事情出现问题时都会设置。

也许有人可以帮助我发现我的代码中的错误,我已经盯着它看了很长时间,即使它正盯着我,我也可能不会注意到它。

谢谢你的时间!

int results[14] = {0}; //Stores counters for each of 14 possible outcomes

    for (int gameNum = 0; gameNum < 10000; gameNum++)
    {
        // Calculate results of 10,000 full world championship finals.
        // Final is thirteen sets - each set is a best of 5 games.
        int sidSetWins = 0, joeSetWins = 0;
        int sidGWins = 0, joeGWins = 0;


        do
        { // Play best of 13 sets

            do
            { // Play best of 5 games               

                do
                { // Play single game
                    if (currTurn == sid)
                    {
                        Sid.throw_quick_set(); // Throws a set of three
                        currTurn = joe;
                    }
                    else if (currTurn == joe)
                    {
                        Joe.throw_quick_set(); // Throws a set of three
                        currTurn = sid;
                    }
                }
                while ((Sid.score != 0) && (Joe.score != 0));

                if (Sid.score == 0)
                {
                sidGWins++;
                cout << "Sid wins game" <<  endl;
                Sid.score = 501;
                Joe.score = 501;
                }
                else if (Joe.score == 0)
                {
                joeGWins++;
                cout << "Joe wins game" << endl;
                Sid.score = 501;
                Joe.score = 501;
                }

                Sleep(1000);
            } 
            while ((joeGWins < 3) && (sidGWins < 3));

            if (joeGWins == 3)
            {
                joeSetWins++;
                cout << "Joe wins set" << endl;
                sidGWins = 0; 
                joeGWins = 0; // Reset for each set
            }
            else if (sidGWins == 3)
            {
                sidSetWins++;
                cout << "Sid wins set" << endl;
                sidGWins = 0; 
                joeGWins = 0; // Reset for each set
            }

            Sleep(1000);
        }
         while ((sidSetWins < 7) && (joeSetWins < 7));

         if ((gameNum%1000) == 0)
         {
             cout << "SidSets " << sidSetWins << endl;
             cout << "JoeSets " << joeSetWins << endl;
             sidSetWins = 0;
             joeSetWins = 0; // Reset for each match
         }

编辑:这里是类方法throw_live_set();如果这对你有帮助的话。

    int Darts_501::throw_quick_set() 
{ // Used for quick games with no commentary - for average calculations

    darts = 0;
    int startScore = score;

    quickGame = true; // Disable commentary

    do
    {
        if (score>=100)
            curr_state = fast;
        else if (score > 50)
            curr_state = slow;
        else if ((score <= 50) && (score > 1))
            curr_state = focus;
        else if ((score == 1) || (score < 0))
            curr_state = bust;
        else
            curr_state = out;

        switch (curr_state) 
        {
            case fast:  
                        score -= throw_treble(20, trebPerc);    
                break;

            case slow:  
                        score -= throw_single(20);
                break;

            case focus: 
                        if (score == 50)
                            score -= throw_bull(bullPerc);
                        else if ((score != 0) && (score%2 == 0))
                            score -= throw_double(score/2); // If score is even, look to hit the double of half that value
                        else if ((score != 0) && (score%2 != 0))
                            score -= throw_setup(score); // If odd, look to set-up a good double score (strategic) 
                break;

            case bust:
                        score = startScore;
                break;

            case out:   score = 0;;
        }
        darts++; 

    } while ((darts < 3) && 
        (curr_state != out) && 
            (curr_state != bust));  

        return 0;
}

0 个答案:

没有答案