コードに関する説明がまったく、または十分に提供されていません (最初の for ループなど)。そのため、お客様の主な問題を解決するお手伝いをすることができません。ただし、その中でいくつかのエラーを指摘することができました。動的配列のサイズを設定しなかったのと同じように: ロール。使い終わったら、忘れずに削除してください。
しかし、少なくともあなたは目的を説明しました。そのため、そのためのクラスを書くことができました。基本的に、あなたは独自の「A Pair of Dice」ゲームを開発しようとしています。したがって、私があなたの目的を注意深く正確に読んだ場合、あなたの目的のための私の執筆クラスは正しく機能するはずです。以下に記載します。これらの有用なコメントを読むことを忘れないでください! そこから何か役に立つことを学んでいただければ幸いです。自由に使用して、必要に応じて微調整してください。
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.
エラーがある場合は、お知らせください。フィードバックや提案は無視されません!