Klinetel - 3 months ago 15
C++ Question

C++ Die Pair Calculation Trouble

This is a simple game I am attempting to write, it's not however, turning out to be as efficient as expected... Here's my objective. The array is not working correctly and the overall logic is incomplete. The objective is: if the sum of the dice on that first roll was 2, 3, or 12, you told the player that he/she lost; if the sum was 7 or 11, you told the player he/she had won; if the first roll was any other number (4,5,6,8,9,10), the player was told that he/she must roll again. Now extend that program as follows – if the player was told to roll again after the first roll (if it was a 4,5,6,8,9,10), save that number, call it “the point”. Now continue rolling until one of two things happen – if the player rolls a 7 before rolling “the point” number again, the player loses and the turn is over; if the player matches “the point” before a 7 is rolled the player wins and the turn is over. If any number besides a 7 or “the point” is rolled nothing happens and the player just keeps rolling. Tell the player if they won or lost, the results of each roll, and how many rolls it took to get the result.

``````#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;
}
``````

￼￼￼￼￼￼

You didn't provide any or enough explanations about your code (like that first for-loop). Because of that, I am unable to help resolve your main issue. However, I was able to to point out some errors within it. Like how you didn't set the size of your dynamic array: roll. And don't forget to delete it after you are done with it!

But, at least you explained your objective. Because of that, I was able to write a class for it. Basically, you are trying to develop your own "A Pair of Dice" game. So, if I read your objective carefully and correctly, my written class for your objective should work correctly. It is provided below. Don't forget to read those useful comments! I hope you can learn something useful from it. Feel free to use and tweak it your needs.

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;
}
};
``````

Tested:

``````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.
``````

If there are any errors, please notify me. Feedbacks and suggestions are not ignored!

Source (Stackoverflow)