您没有对您的代码提供任何或足够的解释(例如第一个 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.
如果有任何错误,请通知我。反馈和建议不容忽视!