944,048 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 4397
  • C++ RSS
You are currently viewing page 1 of this multi-page discussion thread
Oct 2nd, 2006
0

Question about card game

Expand Post »
I'm trying to write program that evaluates a poker hand and returns whether or not it contains a straight, flush, pair, etc. I've got every method except one to work. I cannot figure out how to correctly write the method that determines whether or not there is a straight. Every time I try to complile the code, I get an error saying "DeckOfCards.cpp ISO C++ forbids comparison between pointer and integer."

C++ Syntax (Toggle Plain Text)
  1. DeckOfCards::DeckOfCards()
  2. {
  3. char *suitValue[ 4 ] = { "Hearts", "Diamonds", "Clubs", "Spades" };
  4. char *faceValue[ 13 ] = { "Ace", "Deuce", "Three", "Four", "Five",
  5. "Six", "Seven", "Eight", "Nine", "Ten", "Jack", "Queen", "King" };
  6.  
  7. // initialize suit
  8. for ( int i = 0; i < 4; i++ )
  9. suit[ i ] = suitValue[ i ];
  10.  
  11. // initialize face
  12. for ( int i = 0; i < 13; i++ )
  13. face[ i ] = faceValue[ i ];
  14.  
  15. // loop through rows of deck
  16. for ( int row = 0; row <= 3; row++ )
  17. {
  18. // loop through columns of deck for current row
  19. for ( int column = 0; column <= 12; column++ )
  20. {
  21. deck[ row ][ column ] = 0; // initialize slot of deck to 0
  22. } // end inner for
  23. } // end outer for
  24.  
  25. srand( time( 0 ) ); // seed random number generator
  26. } // end DeckOfCards default constructor
  27.  
  28. void DeckOfCards::shuffle()
  29. {
  30. int row; // represents suit value of card
  31. int column; // represents face value of card
  32.  
  33. // for each of the 52 cards, choose a slot of the deck randomly
  34. for ( int card = 1; card <= 52; card++ )
  35. {
  36. do // choose a new random location until unoccupied slot is found
  37. {
  38. row = rand() % 4; // randomly select the row
  39. column = rand() % 13; // randomly select the column
  40. } while( deck[ row ][ column ] != 0 ); // end do...while
  41.  
  42. // place card number in chosen slot of deck
  43. deck[ row ][ column ] = card;
  44. } // end for
  45. } // end function shuffle
  46.  
  47. // deal a five card poker hand
  48. void DeckOfCards::deal()
  49. {
  50. // loop to distrubute the cards
  51. for ( int card = 1; card < 6; card++ )
  52.  
  53. for ( int row = 0; row <= 3; row++ )
  54.  
  55. for ( int column = 0; column <= 12; column++ )
  56.  
  57. if ( deck[ row ][ column ] == card )
  58. {
  59. hand[ handPos ][ 0 ] = row;
  60. hand[ handPos ][ 1 ] = column;
  61.  
  62. cout << setw( 5 ) << face[ column ] << (" of ")
  63. << setw( 8 ) << left << suit[ row ] << endl;
  64. handPos++;
  65. } // end if
  66. } // end deal
  67.  
  68. void DeckOfCards::straight()
  69. {
  70. for ( int i = 0; i < 4; i++ )
  71. {
  72. if ( hand [ i ] == 1 && hand [ i ] == 2 && hand [i] == 3 &&
  73. hand [ i ] == 4 && hand [ i ] == 5 )
  74. {
  75. cout <<("\n You have a straight");
  76. }
  77. else if ( hand [ i ] == 2 && hand [ i ] == 3 && hand [i] == 4 &&
  78. hand [ i ] == 5 && hand [ i ] == 6 )
  79. {
  80. cout <<("\nYou have a straight");
  81. }
  82. else if ( hand [ i ] == 3 && hand [ i ] == 4 && hand [i] == 5 &&
  83. hand [ i ] == 6 && hand [ i ] == 7 )
  84. {
  85. cout <<("\nYou have a straight");
  86. }
  87. else if ( hand [ i ] == 4 && hand [ i ] == 5 && hand [i] == 6 &&
  88. hand [ i ] == 7 && hand [ i ] == 8)
  89. {
  90. cout <<("\nYou have a straight");
  91. }
  92. else if ( hand [ i ] == 5 && hand [ i ] == 6 && hand [i] == 7 &&
  93. hand [ i ] == 8 && hand [ i ] == 9 )
  94. {
  95. cout <<("\nYou have a straight");
  96. }
  97. else if ( hand [ i ] == 6 && hand [ i ] == 7 && hand [i] == 8 &&
  98. hand [ i ] == 9 && hand [ i ] == 10 )
  99. {
  100. cout <<("\nYou have a straight");
  101. }
  102. else if ( hand [ i ] == 7 && hand [ i ] == 8 && hand [i] == 9 &&
  103. hand [ i ] == 10 && hand [ i ] == 11 )
  104. {
  105. cout <<("\nYou have a straight");
  106. }
  107. else if ( hand [ i ] == 8 && hand [ i ] == 9 && hand [i] == 10 &&
  108. hand [ i ] == 11 && hand [ i ] == 12 )
  109. {
  110. cout <<("\nYou have a straight");
  111. }
  112. else if ( hand [ i ] == 9 && hand [ i ] == 10 && hand [i] == 11 &&
  113. hand [ i ] == 12 && hand [ i ] == 13 )
  114. {
  115. cout <<("\nYou have a straight");
  116. }
  117. }
  118. }
Similar Threads
Reputation Points: 17
Solved Threads: 0
Newbie Poster
mikeallen is offline Offline
17 posts
since Sep 2006
Oct 2nd, 2006
0

Re: Question about card game

Looks like whatever is item in hand[i] is not an integer. I also fail to see how hand[i] can be 1, 2, 3 , 4, ... all at the same time?
Reputation Points: 404
Solved Threads: 180
Nearly a Posting Virtuoso
bumsfeld is offline Offline
1,422 posts
since Jul 2005
Oct 2nd, 2006
0

Re: Question about card game

Click to Expand / Collapse  Quote originally posted by bumsfeld ...
Looks like whatever is item in hand[i] is not an integer. I also fail to see how hand[i] can be 1, 2, 3 , 4, ... all at the same time?
Well, I thougt it was an integer. The char array assigns values to each card. I think part of my problem is hand[i]. I'm trying to see if the hand contains all of the values, but the values don't have to be in any order. To do that, I need to advance 'i' to check each hand. I'm just not sure how to do that.
Reputation Points: 17
Solved Threads: 0
Newbie Poster
mikeallen is offline Offline
17 posts
since Sep 2006
Oct 2nd, 2006
0

Re: Question about card game

hand appears to be a 2-dimensional array, in which case you need to compare each element (if you use only one subscript, you're comparing a pointer to an integer)

C++ Syntax (Toggle Plain Text)
  1. if (hand[i][0]==1 && hand[i][1]==2 && /*...*/)
Of course, the problem here is that the cards may not be in order, so my code won't work correctly unless the array is sorted.
Reputation Points: 108
Solved Threads: 14
Junior Poster in Training
GloriousEremite is offline Offline
65 posts
since Jul 2006
Oct 2nd, 2006
0

Re: Question about card game

Thank you so much for helping me!!!! I added a sort method (a simple selection sort) to my code and fixed the straight method, but something still is not working. I think something is wrong with my sort method. Any ideas as to what I am doing wrong? Thanks in advance!

C++ Syntax (Toggle Plain Text)
  1. void sortHand( char * const);
  2. void swap (int * const, int * const);
  3.  
  4. // DeckOfCards default constructor initializes deck
  5. DeckOfCards::DeckOfCards()
  6. {
  7. // initialize suit and face arrays
  8. char *suitValue[ 4 ] = { "Hearts", "Diamonds", "Clubs", "Spades" };
  9. char *faceValue[ 13 ] = { "Ace", "Deuce", "Three", "Four", "Five",
  10. "Six", "Seven", "Eight", "Nine", "Ten", "Jack", "Queen", "King" };
  11. for ( int i = 0; i < 4; i++ )
  12. suit[ i ] = suitValue[ i ];
  13. for ( int i = 0; i < 13; i++ )
  14. face[ i ] = faceValue[ i ];
  15. // loop through rows of deck
  16. for ( int row = 0; row <= 3; row++ )
  17. {
  18. // loop through columns of deck for current row
  19. for ( int column = 0; column <= 12; column++ )
  20. {
  21. deck[ row ][ column ] = 0; // initialize slot of deck to 0
  22. } // end inner for
  23. } // end outer for
  24.  
  25. srand( time( 0 ) ); // seed random number generator
  26. } // end DeckOfCards default constructor
  27.  
  28. // shuffle cards in deck
  29. void DeckOfCards::shuffle()
  30. {
  31. int row; // represents suit value of card
  32. int column; // represents face value of card
  33. // for each of the 52 cards, choose a slot of the deck randomly
  34. for ( int card = 1; card <= 52; card++ )
  35. {
  36. do // choose a new random location until unoccupied slot is found
  37. {
  38. row = rand() % 4; // randomly select the row
  39. column = rand() % 13; // randomly select the column
  40. }
  41. while( deck[ row ][ column ] != 0 ); // end do...while
  42. // place card number in chosen slot of deck
  43. deck[ row ][ column ] = card;
  44. } // end for
  45. } // end function shuffle
  46.  
  47. // Deal a five card poker hand.
  48. void DeckOfCards::deal()
  49. {
  50. int handPos = 0; // Hand Position
  51. cout << "\nTHE PLAYER'S HAND:\n\n";
  52.  
  53. sortHand( faceValue ); // Sort the array
  54.  
  55. for ( int card = 1; card < 6; card++ )
  56. {
  57. for ( int row = 0; row <= 3; row++ )
  58. {
  59. for ( int column = 0; column <= 12; column++ )
  60. {
  61. if ( deck[ row ][ column ] == card )
  62. {
  63. hand[ handPos ][ 0 ] = row;
  64. hand[ handPos ][ 1 ] = column;
  65.  
  66. cout << setw( 5 ) << face[ column ] << (" of ")
  67. << setw( 8 ) << left << suit[ row ] << endl;
  68. handPos++;
  69. } // end if statement
  70. } // end for loop
  71. } // end for loop
  72. } // end for loop
  73. } // end deal
  74.  
  75. // See if the deck of cards contains a sraight.
  76. void DeckOfCards::straight()
  77. {
  78. for ( int i = 0; i < 4; i++ )
  79. {
  80. if ( hand[i][0] == 1 && hand[i][1] == 2 && hand[i][2] == 3 && hand[i][3] == 4 && hand[i][4] == 5 )
  81. {
  82. cout << ("\nThe hand contains a straight.\n");
  83. }
  84. else if ( hand[i][0] == 2 && hand[i][1] == 3 && hand[i][2] == 4 && hand[i][3] == 5 && hand[i][4] == 6 )
  85. {
  86. cout << ("\nThe hand contains a straight.\n");
  87. }
  88. else if ( hand[i][0] == 3 && hand[i][1] == 4 && hand[i][2] == 5 && hand[i][3] == 6 && hand[i][4] == 7 )
  89. {
  90. cout << ("\nThe hand contains a straight.\n");
  91. }
  92. else if ( hand[i][0] == 4 && hand[i][1] == 5 && hand[i][2] == 6 && hand[i][3] == 7 && hand[i][4] == 8 )
  93. {
  94. cout << ("\nThe hand contains a straight.\n");
  95. }
  96. else if ( hand[i][0] == 5 && hand[i][1] == 6 && hand[i][2] == 7 && hand[i][3] == 8 && hand[i][4] == 9 )
  97. {
  98. cout << ("\nThe hand contains a straight.\n");
  99. }
  100. else if ( hand[i][0] == 6 && hand[i][1] == 7 && hand[i][2] == 8 && hand[i][3] == 9 && hand[i][4] == 10 )
  101. {
  102. cout << ("\nThe hand contains a straight.\n");
  103. }
  104. else if ( hand[i][0] == 7 && hand[i][1] == 8 && hand[i][2] == 9 && hand[i][3] == 10 && hand[i][4] == 11 )
  105. {
  106. cout << ("\nThe hand contains a straight.\n");
  107. }
  108. else if ( hand[i][0] == 8 && hand[i][1] == 9 && hand[i][2] == 10 && hand[i][3] == 11 && hand[i][4] == 12 )
  109. {
  110. cout << ("\nThe hand contains a straight.\n");
  111. }
  112. else if ( hand[i][0] == 9 && hand[i][1] == 10 && hand[i][2] == 11 && hand[i][3] == 12 && hand[i][4] == 13 )
  113. {
  114. cout << ("\nThe hand contains a straight.\n");
  115. }
  116. }
  117. }
  118.  
  119. void sortHand( char * const faceValue)
  120. {
  121. int smallest;
  122.  
  123. // loop over size - 1 elements
  124. for ( int i = 0; i < 13; i++ );
  125. {
  126. smallest = i; // first index of remaining array
  127.  
  128. // loop to find index of smallest element
  129. for ( int index = i + 1; index < 13; index++ )
  130.  
  131. if ( faceValue [index] < faceValue [smallest] )
  132. smallest = index;
  133. swap( &faceValue [ i ], &faceValue[ smallest ] );
  134. }
  135. }
  136.  
  137. void swap ( char * const element1Ptr, char * const element2Ptr )
  138. {
  139. int hold = *element1Ptr;
  140. *element1Ptr = *element2Ptr;
  141. *element2Ptr = hold;
  142. }
Last edited by mikeallen; Oct 3rd, 2006 at 12:11 am.
Reputation Points: 17
Solved Threads: 0
Newbie Poster
mikeallen is offline Offline
17 posts
since Sep 2006
Oct 3rd, 2006
0

Re: Question about card game

One of those little errors, it appears:
C++ Syntax (Toggle Plain Text)
  1. for ( int i = 0; i < 13; i++ ); //<--semicolon
See if that works when you remove the trailing semicolon.

As a side note, you should probably prefer passing by reference to functions like your swap function, that is:
C++ Syntax (Toggle Plain Text)
  1. void swap ( char& element1Ptr, char& element2Ptr )
  2. {
  3. char hold = element1Ptr;
  4. element1Ptr = element2Ptr;
  5. element2Ptr = hold;
  6. }
  7. //...call it like this:
  8. swap( faceValue[i], faceValue[smallest]);
Reputation Points: 108
Solved Threads: 14
Junior Poster in Training
GloriousEremite is offline Offline
65 posts
since Jul 2006
Oct 3rd, 2006
0

Re: Question about card game

straight() is a terrible function. What about something like
C++ Syntax (Toggle Plain Text)
  1. isStraight=true;
  2. for (i=0; i<8; i++)
  3. {
  4. for (j=0; j<5; j++)
  5. {
  6. if (hand[j+1] != hand[j])
  7. {
  8. isStraight = false;
  9. break;
  10. }
  11. if (isStraight == false)
  12. {
  13. break;
  14. }
  15. }
  16. }
Of course modified for your design...
Last edited by WaltP; Oct 3rd, 2006 at 2:35 am.
Moderator
Reputation Points: 3278
Solved Threads: 894
Posting Sage
WaltP is offline Offline
7,747 posts
since May 2006
Oct 3rd, 2006
0

Re: Question about card game

I removed the semicolon, but the sort still will not work. The compiler error is
C++ Syntax (Toggle Plain Text)
  1. DeckOfCards.cpp `faceValue' undeclared (first use this function)
  2.  
and it is saying that the error is in this line
C++ Syntax (Toggle Plain Text)
  1. sortHand( faceValue ); // Sort the array
.
Reputation Points: 17
Solved Threads: 0
Newbie Poster
mikeallen is offline Offline
17 posts
since Sep 2006
Oct 3rd, 2006
0

Re: Question about card game

I tried to fix the problem by changing the arrays to
C++ Syntax (Toggle Plain Text)
  1. static const char *suitValue[ 4 ] = { "Hearts", "Diamonds", "Clubs", "Spades" };
  2. static const char *faceValue[ 13 ] = { "Ace", "Deuce", "Three", "Four", "Five", "Six", "Seven",
  3. "Eight", "Nine", "Ten", "Jack", "Queen", "King" };

I tried to call the sort method to sort the array by doing this,
C++ Syntax (Toggle Plain Text)
  1. sortHand( char const *faceValue ); // Sort the array

but there is still a problem.
Reputation Points: 17
Solved Threads: 0
Newbie Poster
mikeallen is offline Offline
17 posts
since Sep 2006
Oct 4th, 2006
0

Re: Question about card game

Click to Expand / Collapse  Quote originally posted by mikeallen ...
I tried to fix the problem by changing the arrays to
I tried to call the sort method to sort the array by doing this,
C++ Syntax (Toggle Plain Text)
  1. sortHand( char const *faceValue ); // Sort the array
but there is still a problem.
I dont if if its just me but how can you expect a procedure to sort a data item for you which involves manipulating teh data by passing the argument as constant ??? In you function prototype the const qualifier applies to the char array data and not the pointer if thats what you wnated to do ?
Super Moderator
Featured Poster
Reputation Points: 3241
Solved Threads: 720
Failure as a human
~s.o.s~ is offline Offline
8,873 posts
since Jun 2006

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: help with a program
Next Thread in C++ Forum Timeline: counting & largest # help...





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC