Question about card game

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Sep 2006
Posts: 17
Reputation: mikeallen is an unknown quantity at this point 
Solved Threads: 0
mikeallen mikeallen is offline Offline
Newbie Poster

Question about card game

 
0
  #1
Oct 2nd, 2006
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."

  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. }
Reply With Quote Quick reply to this message  
Join Date: Jul 2005
Posts: 1,221
Reputation: bumsfeld will become famous soon enough bumsfeld will become famous soon enough 
Solved Threads: 138
bumsfeld's Avatar
bumsfeld bumsfeld is offline Offline
Nearly a Posting Virtuoso

Re: Question about card game

 
0
  #2
Oct 2nd, 2006
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?
Reply With Quote Quick reply to this message  
Join Date: Sep 2006
Posts: 17
Reputation: mikeallen is an unknown quantity at this point 
Solved Threads: 0
mikeallen mikeallen is offline Offline
Newbie Poster

Re: Question about card game

 
0
  #3
Oct 2nd, 2006
Originally Posted by bumsfeld View Post
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.
Reply With Quote Quick reply to this message  
Join Date: Jul 2006
Posts: 65
Reputation: GloriousEremite will become famous soon enough GloriousEremite will become famous soon enough 
Solved Threads: 14
GloriousEremite GloriousEremite is offline Offline
Junior Poster in Training

Re: Question about card game

 
0
  #4
Oct 2nd, 2006
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)

  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.
"What are the roots that clutch, what branches grow
out of this stony rubbish?"
Reply With Quote Quick reply to this message  
Join Date: Sep 2006
Posts: 17
Reputation: mikeallen is an unknown quantity at this point 
Solved Threads: 0
mikeallen mikeallen is offline Offline
Newbie Poster

Re: Question about card game

 
0
  #5
Oct 2nd, 2006
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!

  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.
Reply With Quote Quick reply to this message  
Join Date: Jul 2006
Posts: 65
Reputation: GloriousEremite will become famous soon enough GloriousEremite will become famous soon enough 
Solved Threads: 14
GloriousEremite GloriousEremite is offline Offline
Junior Poster in Training

Re: Question about card game

 
0
  #6
Oct 3rd, 2006
One of those little errors, it appears:
  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:
  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]);
"What are the roots that clutch, what branches grow
out of this stony rubbish?"
Reply With Quote Quick reply to this message  
Join Date: May 2006
Posts: 3,117
Reputation: WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of 
Solved Threads: 282
Moderator
WaltP's Avatar
WaltP WaltP is offline Offline
Posting Sensei

Re: Question about card game

 
0
  #7
Oct 3rd, 2006
straight() is a terrible function. What about something like
  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.
The 3 Laws of the Procrastination Society:
1) Never do today that which can be put off until tomorrow
2) Tomorrow never comes
Reply With Quote Quick reply to this message  
Join Date: Sep 2006
Posts: 17
Reputation: mikeallen is an unknown quantity at this point 
Solved Threads: 0
mikeallen mikeallen is offline Offline
Newbie Poster

Re: Question about card game

 
0
  #8
Oct 3rd, 2006
I removed the semicolon, but the sort still will not work. The compiler error is
  1. DeckOfCards.cpp `faceValue' undeclared (first use this function)
  2.  
and it is saying that the error is in this line
  1. sortHand( faceValue ); // Sort the array
.
Reply With Quote Quick reply to this message  
Join Date: Sep 2006
Posts: 17
Reputation: mikeallen is an unknown quantity at this point 
Solved Threads: 0
mikeallen mikeallen is offline Offline
Newbie Poster

Re: Question about card game

 
0
  #9
Oct 3rd, 2006
I tried to fix the problem by changing the arrays to
  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,
  1. sortHand( char const *faceValue ); // Sort the array

but there is still a problem.
Reply With Quote Quick reply to this message  
Join Date: Jun 2006
Posts: 7,642
Reputation: ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of 
Solved Threads: 472
Super Moderator
Featured Poster
~s.o.s~'s Avatar
~s.o.s~ ~s.o.s~ is offline Offline
Failure as a human

Re: Question about card game

 
0
  #10
Oct 4th, 2006
Originally Posted by mikeallen View Post
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,
  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 ?
I don't accept change; I don't deserve to live.

Jo Tujhe Jagaaye, Nindein Teri Udaaye Khwaab Hai Sachcha Wahi.
Nindon Mein Jo Aaye Jise To Bhul Jaaye Khawab Woh Sachcha Nahi.
Khwaab Ko Raag De, Nind Ko Aag De
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the C++ Forum
Thread Tools Search this Thread



Tag cloud for C++
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC