【问题标题】:C++ Die Pair Calculation Trouble [closed]C ++模具对计算麻烦[关闭]
【发布时间】:2013-02-11 07:49:41
【问题描述】:

这是一个我正在尝试编写的简单游戏,但结果却并非如预期的那样高效......这是我的目标。阵列工作不正常,整体逻辑不完整。目标是:如果第一次掷出的骰子总和是 2、3 或 12,则您告诉玩家他/她输了;如果总和是 7 或 11,您告诉玩家他/她赢了;如果第一次滚动是任何其他数字(4,5,6,8,9,10),玩家被告知他/她必须再次滚动。现在扩展该程序如下 - 如果玩家被告知在第一次滚动后再次滚动(如果它是 4、5、6、8、9、10),则保存该数字,称其为“点”。现在继续滚动,直到发生以下两种情况之一 - 如果玩家在再次滚动“点”数字之前掷出 7,则玩家输掉并且回合结束;如果玩家在掷出 7 之前匹配“点”,则玩家获胜并且转牌结束。如果掷出 7 或“点”以外的任何数字,则什么都不会发生,玩家只会继续滚动。告诉玩家他们是赢还是输、每次掷骰的结果以及获得该结果需要多少次掷骰。

#include <iostream>
#include <ctime>

using namespace std;

int main()
{
    int die1, 
        die2, 
        sum, 
        point,
        rollChoice;
    static int rollCount;

    int *rolls = new int[];

    rollCount=1;
    point=0;

    srand(time(0));

    cout<<"Enter 1 to roll: ";
    cin>>rollChoice;

    if(rollChoice==1)
    {

        for (int i=0; i<INT_MAX; i++)
        {
            die1=rand()%10;
            die2=rand()%10;

            sum=die1+die2;

            rolls[sum];

            if(rolls[i] == 2 || rolls[i] == 3 || rolls[i] == 12)
            {
                cout<<"\nYou have lost!"<<endl;

                rollCount++;

                cout<<"\n";
                cin>>rollChoice;
            }

            else if(rolls[i] == 7 || rolls[i] == 11)
            {
                cout<<"\nYou have won!"<<endl;

                rollCount++;

                cout<<"\n";
                cin>>rollChoice;
            }
            else if(rolls[i] == 4 || rolls[i] == 5 || rolls[i] == 6 || rolls[i] == 8 || rolls[i] == 9 || rolls[i] == 10)
            {
                cout<<"\nYou have lost, roll again.";

                point=rolls[i];

                cout<<"\n";
                cin>>rollChoice;

                rollCount++;

                if(rollCount==7)
                {
                    for(int i=0; i<7; i++)
                    {
                        cout<<"\nRoll "<<i<<". "<<rolls[i];
                    }
                }
            }
        }
    }

    cin.get();
    cin.get();

    return 0;
}



【问题讨论】:

  • 我最关心的是用 7 卷值填充一个数组,然后如果玩家被告知在第一次滚动后再次滚动(如果是 4,5,6,8,9 ,10),保存那个数字,称之为“点”。这是最大的问题。如上所示打印出来。

标签: c++ arrays loops visual-c++


【解决方案1】:
  • rolls 是一个没有大小的数组,但你尝试往里面塞东西。
  • 您希望for (int i=0; i&lt;INT_MAX; i++) 做什么?
  • rolls[sum] ?这是一个 NoOP
  • 在循环内使用i 重新定义i 是令人震惊的做法。

我建议你翻开你的 C/C++ 书,重新审视一些基础知识,然后在遇到问题时提出一些具体问题!

【讨论】:

  • 您的答案可能的补充:10 面骰子的值从 1 到 10,而不是 0 到 9...此外,该程序的逻辑表明作者打算使用 6 面骰子。
  • 公平点,但是当它显然是作业时,我通常会尝试指出问题,而不是给出全部答案。偶尔我会在我感兴趣的时候走得更远;-)
  • 让我回到“过去的美好时光”,我们不得不亲自作弊。我有点惊讶有人使用 c++ 作为教学语言,Java 似乎更适合这个目的。
  • 我们的大问题是打印室中的打印输出丢失。必须让一个伙伴站在打印机旁边(当你在终端等待时 - 通常是 VT52 或 VT100 “兼容”),直到你的学生号码出现,他可以为你拿走它。
【解决方案2】:

您没有对您的代码提供任何或足够的解释(例如第一个 for 循环)。 因此,我无法帮助解决您的主要问题。但是,我能够 指出其中的一些错误。就像你没有设置你的大小一样 动态数组:roll.完成后不要忘记删除它!

但是,至少你解释了你的目标。正因为如此,我才能写 为它上一堂课。基本上,您正在尝试开发自己的“一对骰子” 游戏。所以,如果我仔细正确地阅读了你的目标,我的书面课 因为您的目标应该正常工作。它在下面提供。不要忘记 阅读那些有用的 cmets!我希望你能从中学到一些有用的东西。感觉 免费使用并根据需要进行调整。

APairOfDice.h

#ifndef APAIROFDICE_H
#define APAIROFDICE_H

#include <string>
#include <iostream>
#include <ctime>

#define A_PAIR_OF_DICE_LOST     0
#define A_PAIR_OF_DICE_WON      1
#define A_PAIR_OF_DICE_RETRY    2

class APairOfDice
{
public:
    void Play( void );
private:
    ::UINT Roll( void );
    void Reset( void );
    void PrintHistory( void );
private:
    ::UINT m_nCount;
    ::UINT m_nValue;
    ::UINT m_nPoint;
    std::vector< ::UINT >m_vnHistory;
    bool bWin;
public:
    APairOfDice( void ) : m_nCount( 0 ), m_nValue( 0 ), m_nPoint( 0 ), bWin( false ) { }
    ~APairOfDice( void ) { }
};

#endif // APAIROFDICE_H

APairOfDice.cpp

void APairOfDice::Play( void )
{
    // DECLARATION
    ::UINT nRollResult = Roll( );

    // DO WHATEVER
    // *This is the main loop of the game:
    while( true )
    {
        //* Is this is first roll? If so, do this:
        if( m_nCount == 1 )
        {
            // *If m_nValue is equal to either of the following
            // values: 2, 3, and 12, the player has lost.
            // So, bWin is set to false and the loop will break.
            if( nRollResult == A_PAIR_OF_DICE_LOST ) {
                bWin = false;
                break;
            }

            // *Else if m_nValue is equal to either 7 or 11,
            // the player has won. Therefore, bWin is set as
            // true and the loop will break.
            else if( nRollResult == A_PAIR_OF_DICE_WON ) {
                bWin = true;
                break;
            }

            // *Else m_nValue is equal to either of the following
            // numbers: 1, 4, 5, 6, 8, 9, 10
            // change the value of m_nPoint to m_nValue (saved it).
            else
                m_nPoint = m_nValue;
        }
        //* If not, do this:
        else
        {
            // *If m_nValue is equal to 7, the player will
            // lose. If so, set bWin as false and break
            // the loop. Game over.
            if( m_nValue == 7 ) {
                bWin = false;
                break;
            }

            // *Else if m_nValue is equal to m_nPoint, the player
            // will win. If so, set bWin as true and break
            // the loop. Game over. Good game.          
            else if( m_nValue == m_nPoint ) {
                bWin = true;
                break;
            }
        }

        //* Remove the next two lines to prevent the loop from pausing.
        std::cout << "Enter any key to roll again!\n";
        ::getchar( );

        // Roll again.
        Roll( );
    }

    // PRINT
    PrintHistory( );

    // ASK FOR REPLAY
    std::cout << "Would you like to play again? Enter 'y' for yes and any other key is for no.\n";
    if( ::getchar( ) == 'y' || ::getchar( ) == 'Y' )
    {
        Reset( );
        Play( );
    }
};
::UINT APairOfDice::Roll( void )
{
    // DECLARATION

    // INITIALIZATION
    // *Increase the value of m_nCount.
    m_nCount ++;

    // *Initialize random number generator
    // with std::time as the seed.
    std::srand( ( ::UINT )std::time( 0 ) );

    // *Set the value of m_nValue as the result of
    // std::rand % highest value + lowest value.
    // *This isn't the best way to generate a value.
    m_nValue = std::rand( ) % 12 + 1;

    // *Save the old m_nValue.
    m_vnHistory.push_back( m_nValue );

    // *Check and return values.
    if( m_nValue == 2 || m_nValue == 3 || m_nValue == 12 )
        return( A_PAIR_OF_DICE_LOST );
    else if( m_nValue == 7 || m_nValue == 11 )
        return( A_PAIR_OF_DICE_WON );
    else
        return( A_PAIR_OF_DICE_RETRY );
};

void APairOfDice::Reset( void )
{
    //* Reset all class variables and members.
    this->m_nCount = 0;
    this->m_nValue = 0;
    this->m_nPoint = 0;
    this->m_vnHistory.clear( );
    this->bWin = false;
};

void APairOfDice::PrintHistory( void )
{
    // DECLARATION
    ::UINT nRollCount = 2;

    // *Print win or lose message.
    if( bWin )
        std::cout << "Yay! You've won!!!\n";
    else
        std::cout << "Aw! You've lost...\n";

    // *Print the value of m_nPoint.
    std::cout << "Roll 1 - " << "your \"point\" is " << m_nPoint << std::endl;

    // *Print each value within m_vnHistory except for the first one
    // why not the first one? Well, because thats the value of m_nPoint.
    for( std::vector< ::UINT >::const_iterator nIndex = m_vnHistory.begin( ) + 1;
         nIndex != m_vnHistory.end( );
         ++ nIndex, ++ nRollCount )
    {
        std::cout << "Roll " << nRollCount << ". " << "\tRolled: " << *nIndex << std::endl;
    }
};

已测试:

Enter any key to roll again!

Enter any key to roll again!

Enter any key to roll again!

Enter any key to roll again!

Enter any key to roll again!

Enter any key to roll again!

Enter any key to roll again!

Enter any key to roll again!

Enter any key to roll again!

Aw! You've lost...
Roll 1 - your "point" is 6
Roll 2.         Rolled: 10
Roll 3.         Rolled: 10
Roll 4.         Rolled: 10
Roll 5.         Rolled: 1
Roll 6.         Rolled: 1
Roll 7.         Rolled: 1
Roll 8.         Rolled: 4
Roll 9.         Rolled: 4
Roll 10.        Rolled: 7
Would you like to play again? Enter 'y' for yes and any other key is for no.

如果有任何错误,请通知我。反馈和建议不容忽视!

【讨论】:

    【解决方案3】:

    您似乎忘记设置卷的大小。 您可能忘记的另一项任务是删除卷后 你已经完全完成了。清理总是一个好习惯!

    这部分看起来很可怕:

    else if(rolls[i] == 4 || rolls[i] == 5 || rolls[i] == 6 || rolls[i] == 8 || rolls[i] == 9 || rolls[i] == 10)
    {
        // ...
    }
    

    把那部分改成这样:

    else if( rolls[ i ] => 4 && rolls[ i ] <= 10 && rolls[ i ] != 7 )
    {
        // ...
    }
    

    您应该在声明时明确分配局部变量的值。 改变这个:

    int die1, 
            die2, 
            sum, 
            point,
            rollChoice;
        static int rollCount;
    
        // Why initialize the value of rollCount and point now?
        rollCount=1;
        point=0;
    

    到这里:

    int die1 = 0;
    int die2 = 0;
    int sum = 0;
    int point = 0;
    int rollChoice = 0;
    static int rollCount = 1;
    

    【讨论】:

      【解决方案4】:

      它们真的是 10 面骰子吗? 您没有使用rolls[sum] 将任何数据放入卷中;你的意思是rolls[i]=sum;

      【讨论】:

        猜你喜欢
        • 2017-10-13
        • 2019-03-30
        • 2016-08-06
        • 1970-01-01
        • 1970-01-01
        • 2019-02-14
        • 2010-09-26
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多