1

这是我正在尝试编写的一个简单游戏,但结果却并非如预期的那样高效……这是我的目标。阵列工作不正常,整体逻辑不完整。目标是:如果第一次掷骰子的总和是 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;
}



4

4 回答 4

8
  • rolls是一个没有大小的数组,但您尝试在其中粘贴东西。
  • 你期待for (int i=0; i<INT_MAX; i++)做什么?
  • rolls[sum]? 这是一个 NoOP
  • i在循环内部重新定义使用i是令人震惊的做法。

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

于 2013-02-26T00:47:07.160 回答
2

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

但是,至少你解释了你的目标。因此,我能够为它编写一个类。基本上,您正在尝试开发自己的“一对骰子”游戏。因此,如果我仔细正确地阅读了您的目标,那么我为您的目标编写的课程应该可以正常工作。它在下面提供。不要忘记阅读那些有用的评论!我希望你能从中学到一些有用的东西。随意使用和调整您的需求。

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.

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

于 2013-02-26T08:00:23.143 回答
1

您似乎忘记设置卷的大小。您可能忘记的另一项任务是在您完全完成后删除卷。打扫卫生总是一个好习惯!

这部分看起来很可怕:

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;
于 2013-02-26T01:29:35.277 回答
0

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

于 2013-02-26T00:46:43.773 回答