3 Replies - 138 Views - Last Post: 07 February 2012 - 10:58 PM Rate Topic: -----

Topic Sponsor:

#1 IngeniousHax  Icon User is offline

  • |>|20-514<|{3|2

Reputation: 62
  • View blog
  • Posts: 1,206
  • Joined: 28-March 09

repeated array output with class

Posted 07 February 2012 - 05:19 PM

For some reason my class will print out different hands for roughly the first 10 deals and after that will repeat the same hand about 40 times before switching to a new hand and repeat 40 times.

I need a little help with this one...

DOC.cpp
void DeckOfCards::findPair(const char *pairCheck[])
{
    const char *pairEval[5] = {0};
    int fourOfKind = 0;
	int fourCount = 1;

    for(int index = 0; index < 5; index++)  // Make a copy of pairCheck for evaluation
    {
        pairEval[index] = pairCheck[index]; // Copying pairCheck elements to pairEval
    }

    cout << "\n\n";

    for(int index = 0; index < 4; index++)
    {
        for(int index2 = (index + 1); index2 < 5; index2++) // Since index2 needs to be one location
                                                            // ahead of index at all times, index2 = index+1
        {
            if(strcmp(pairCheck[index2], pairEval[index]) == 0) // if the indexes are a match, increase match counter.
            {
				fourCount++;
            }
        }
    }
	
	for(int x = 0; x < 4; x++)
	{

		if(strcmp(pairEval[x], pairEval[x + 1]) == 0)
			fourOfKind++;
	}

	if(fourOfKind >= 4)
		setCounter(fourOfKind);	// pass fourOfKind by value to the member variable
	else
	{
		fourOfKind = 0;
		setCounter(fourOfKind);
	}

}



main.cpp
int main()
{
	long int timesRan = 0;	// Amt of times the cards were dealt until a 4 of a kind was discovered.
	int fourFound = 0; // hopefuly find a four of a kind of stop the loop

	do
	{
		++timesRan;	// Count the amount of times the loop is ran
		DeckOfCards deckOfCards; // create DeckOfCards object in loop for continuous play
        deckOfCards.shuffle(); // shuffle the cards in the deck
        deckOfCards.deal(); // deal the cards

		fourFound = deckOfCards.getCounter(); // Retrieve the four of a kind counter using getCounter() (member variable)
	}while(fourFound < 4);	// while fourFound is not > 4, loop

	cout << "A four of a kind was found after " << timesRan << " hands." << endl;	// Print the amt of loops ran before 4 of a kind was found
		
	cin.get();

    return 0; // indicates successful termination
} // end main



Is This A Good Question/Topic? 0
  • +

Replies To: repeated array output with class

#2 #define  Icon User is offline

  • Programmer
  • member icon

Reputation: 565
  • View blog
  • Posts: 2,084
  • Joined: 19-February 09

Re: repeated array output with class

Posted 07 February 2012 - 06:05 PM

The problem doesn't appear to be in the code provided, perhaps it is in the shuffle or deal functions.
Was This Post Helpful? 0
  • +
  • -

#3 IngeniousHax  Icon User is offline

  • |>|20-514<|{3|2

Reputation: 62
  • View blog
  • Posts: 1,206
  • Joined: 28-March 09

Re: repeated array output with class

Posted 07 February 2012 - 08:00 PM

Well that's good, ill post the rest of the code when I get home from work. Thank you.
Was This Post Helpful? 0
  • +
  • -

#4 IngeniousHax  Icon User is offline

  • |>|20-514<|{3|2

Reputation: 62
  • View blog
  • Posts: 1,206
  • Joined: 28-March 09

Re: repeated array output with class

Posted 07 February 2012 - 10:58 PM

// DeckOfCards default constructor initializes deck
DeckOfCards::DeckOfCards()
{
	cardCounter = 0;
   // loop through rows of deck
   for ( int row = 0; row <= 3; row++ )
   {
      // loop through columns of deck for current row
      for ( int column = 0; column <= 12; column++ )
      {
         deck[ row ][ column ] = 0; // initialize slot of deck to 0
      } // end inner for
   } // end outer for

   // seed random number generator
   srand( time(NULL) );
} // end DeckOfCards default constructor

DeckOfCards::~DeckOfCards()     // explicitly declared destructor for later possible usage.
{

}


// shuffle cards in deck
void DeckOfCards::shuffle()
{
   int row; // represents suit value of card
   int column; // represents face value of card

   // for each of the 52 cards, choose a slot of the deck randomly
   for ( int card = 1; card <= 5; card++ )
   {
      do // choose a new random location until unoccupied slot is found
      {
         row = rand() % 4; // randomly select the row (0 to 3)
         column = rand() % 13; // randomly select the column (0 to 12)
      } while( deck[ row ][ column ] != 0 ); // end do...while

      // place card number in chosen slot of deck
      deck[ row ][ column ] = card;
   } // end for
} // end function shuffle



// deal cards in deck
void DeckOfCards::deal()
{
    int index = 0;  // to index the faceCheck array
    const char *faceCheck[5] = {0}; // const char pointer to assign class member for later transfers.

   // initialize suit array
   static const char *suit[ 4 ] =
      { "Hearts", "Diamonds", "Clubs", "Spades" };

   // initialize face array
    static const char *face[ 13 ] =
    { "Ace", "Deuce", "Three", "Four", "Five", "Six", "Seven",
    "Eight", "Nine", "Ten", "Jack", "Queen", "King" };

   // for each of the 52 cards
   for ( int card = 1; card <= 5; card++ )
   {
      // loop through rows of deck
      for ( int row = 0; row <= 3; row++ )
      {
         // loop through columns of deck for current row
         for ( int column = 0; column <= 12; column++ )
         {
            // if slot contains current card, display card
            if ( deck[ row ][ column ] == card )
            {
               faceCheck[index] = face[column]; // Assign face[column] to faceCheck[index], probably deprecated.
               index++; // Increase the index to the next empty array spot
               cout << right << face[ column ] << " of " << suit[ row ] << endl;
            } // end if
         } // end innermost for
      } // end inner for
   } // end outer for

    findPair(faceCheck);  // Call the findPair function to determine if there is a pair in the hand.
} // end function deal


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1