| | |
Question about card game
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Sep 2006
Posts: 17
Reputation:
Solved Threads: 0
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)
DeckOfCards::DeckOfCards() { char *suitValue[ 4 ] = { "Hearts", "Diamonds", "Clubs", "Spades" }; char *faceValue[ 13 ] = { "Ace", "Deuce", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten", "Jack", "Queen", "King" }; // initialize suit for ( int i = 0; i < 4; i++ ) suit[ i ] = suitValue[ i ]; // initialize face for ( int i = 0; i < 13; i++ ) face[ i ] = faceValue[ i ]; // 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 srand( time( 0 ) ); // seed random number generator } // end DeckOfCards default constructor 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 <= 52; card++ ) { do // choose a new random location until unoccupied slot is found { row = rand() % 4; // randomly select the row column = rand() % 13; // randomly select the column } 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 a five card poker hand void DeckOfCards::deal() { // loop to distrubute the cards for ( int card = 1; card < 6; card++ ) for ( int row = 0; row <= 3; row++ ) for ( int column = 0; column <= 12; column++ ) if ( deck[ row ][ column ] == card ) { hand[ handPos ][ 0 ] = row; hand[ handPos ][ 1 ] = column; cout << setw( 5 ) << face[ column ] << (" of ") << setw( 8 ) << left << suit[ row ] << endl; handPos++; } // end if } // end deal void DeckOfCards::straight() { for ( int i = 0; i < 4; i++ ) { if ( hand [ i ] == 1 && hand [ i ] == 2 && hand [i] == 3 && hand [ i ] == 4 && hand [ i ] == 5 ) { cout <<("\n You have a straight"); } else if ( hand [ i ] == 2 && hand [ i ] == 3 && hand [i] == 4 && hand [ i ] == 5 && hand [ i ] == 6 ) { cout <<("\nYou have a straight"); } else if ( hand [ i ] == 3 && hand [ i ] == 4 && hand [i] == 5 && hand [ i ] == 6 && hand [ i ] == 7 ) { cout <<("\nYou have a straight"); } else if ( hand [ i ] == 4 && hand [ i ] == 5 && hand [i] == 6 && hand [ i ] == 7 && hand [ i ] == 8) { cout <<("\nYou have a straight"); } else if ( hand [ i ] == 5 && hand [ i ] == 6 && hand [i] == 7 && hand [ i ] == 8 && hand [ i ] == 9 ) { cout <<("\nYou have a straight"); } else if ( hand [ i ] == 6 && hand [ i ] == 7 && hand [i] == 8 && hand [ i ] == 9 && hand [ i ] == 10 ) { cout <<("\nYou have a straight"); } else if ( hand [ i ] == 7 && hand [ i ] == 8 && hand [i] == 9 && hand [ i ] == 10 && hand [ i ] == 11 ) { cout <<("\nYou have a straight"); } else if ( hand [ i ] == 8 && hand [ i ] == 9 && hand [i] == 10 && hand [ i ] == 11 && hand [ i ] == 12 ) { cout <<("\nYou have a straight"); } else if ( hand [ i ] == 9 && hand [ i ] == 10 && hand [i] == 11 && hand [ i ] == 12 && hand [ i ] == 13 ) { cout <<("\nYou have a straight"); } } }
•
•
Join Date: Sep 2006
Posts: 17
Reputation:
Solved Threads: 0
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.
•
•
Join Date: Jul 2006
Posts: 65
Reputation:
Solved Threads: 14
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)
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.
C++ Syntax (Toggle Plain Text)
if (hand[i][0]==1 && hand[i][1]==2 && /*...*/)
"What are the roots that clutch, what branches grow
out of this stony rubbish?"
out of this stony rubbish?"
•
•
Join Date: Sep 2006
Posts: 17
Reputation:
Solved Threads: 0
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)
void sortHand( char * const); void swap (int * const, int * const); // DeckOfCards default constructor initializes deck DeckOfCards::DeckOfCards() { // initialize suit and face arrays char *suitValue[ 4 ] = { "Hearts", "Diamonds", "Clubs", "Spades" }; char *faceValue[ 13 ] = { "Ace", "Deuce", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten", "Jack", "Queen", "King" }; for ( int i = 0; i < 4; i++ ) suit[ i ] = suitValue[ i ]; for ( int i = 0; i < 13; i++ ) face[ i ] = faceValue[ i ]; // 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 srand( time( 0 ) ); // seed random number generator } // end DeckOfCards default constructor // 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 <= 52; card++ ) { do // choose a new random location until unoccupied slot is found { row = rand() % 4; // randomly select the row column = rand() % 13; // randomly select the column } 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 a five card poker hand. void DeckOfCards::deal() { int handPos = 0; // Hand Position cout << "\nTHE PLAYER'S HAND:\n\n"; sortHand( faceValue ); // Sort the array for ( int card = 1; card < 6; card++ ) { for ( int row = 0; row <= 3; row++ ) { for ( int column = 0; column <= 12; column++ ) { if ( deck[ row ][ column ] == card ) { hand[ handPos ][ 0 ] = row; hand[ handPos ][ 1 ] = column; cout << setw( 5 ) << face[ column ] << (" of ") << setw( 8 ) << left << suit[ row ] << endl; handPos++; } // end if statement } // end for loop } // end for loop } // end for loop } // end deal // See if the deck of cards contains a sraight. void DeckOfCards::straight() { for ( int i = 0; i < 4; i++ ) { if ( hand[i][0] == 1 && hand[i][1] == 2 && hand[i][2] == 3 && hand[i][3] == 4 && hand[i][4] == 5 ) { cout << ("\nThe hand contains a straight.\n"); } else if ( hand[i][0] == 2 && hand[i][1] == 3 && hand[i][2] == 4 && hand[i][3] == 5 && hand[i][4] == 6 ) { cout << ("\nThe hand contains a straight.\n"); } else if ( hand[i][0] == 3 && hand[i][1] == 4 && hand[i][2] == 5 && hand[i][3] == 6 && hand[i][4] == 7 ) { cout << ("\nThe hand contains a straight.\n"); } else if ( hand[i][0] == 4 && hand[i][1] == 5 && hand[i][2] == 6 && hand[i][3] == 7 && hand[i][4] == 8 ) { cout << ("\nThe hand contains a straight.\n"); } else if ( hand[i][0] == 5 && hand[i][1] == 6 && hand[i][2] == 7 && hand[i][3] == 8 && hand[i][4] == 9 ) { cout << ("\nThe hand contains a straight.\n"); } else if ( hand[i][0] == 6 && hand[i][1] == 7 && hand[i][2] == 8 && hand[i][3] == 9 && hand[i][4] == 10 ) { cout << ("\nThe hand contains a straight.\n"); } else if ( hand[i][0] == 7 && hand[i][1] == 8 && hand[i][2] == 9 && hand[i][3] == 10 && hand[i][4] == 11 ) { cout << ("\nThe hand contains a straight.\n"); } else if ( hand[i][0] == 8 && hand[i][1] == 9 && hand[i][2] == 10 && hand[i][3] == 11 && hand[i][4] == 12 ) { cout << ("\nThe hand contains a straight.\n"); } else if ( hand[i][0] == 9 && hand[i][1] == 10 && hand[i][2] == 11 && hand[i][3] == 12 && hand[i][4] == 13 ) { cout << ("\nThe hand contains a straight.\n"); } } } void sortHand( char * const faceValue) { int smallest; // loop over size - 1 elements for ( int i = 0; i < 13; i++ ); { smallest = i; // first index of remaining array // loop to find index of smallest element for ( int index = i + 1; index < 13; index++ ) if ( faceValue [index] < faceValue [smallest] ) smallest = index; swap( &faceValue [ i ], &faceValue[ smallest ] ); } } void swap ( char * const element1Ptr, char * const element2Ptr ) { int hold = *element1Ptr; *element1Ptr = *element2Ptr; *element2Ptr = hold; }
Last edited by mikeallen; Oct 3rd, 2006 at 12:11 am.
•
•
Join Date: Jul 2006
Posts: 65
Reputation:
Solved Threads: 14
One of those little errors, it appears:
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)
for ( int i = 0; i < 13; i++ ); //<--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)
void swap ( char& element1Ptr, char& element2Ptr ) { char hold = element1Ptr; element1Ptr = element2Ptr; element2Ptr = hold; } //...call it like this: swap( faceValue[i], faceValue[smallest]);
"What are the roots that clutch, what branches grow
out of this stony rubbish?"
out of this stony rubbish?"
straight() is a terrible function. What about something like
Of course modified for your design...
C++ Syntax (Toggle Plain Text)
isStraight=true; for (i=0; i<8; i++) { for (j=0; j<5; j++) { if (hand[j+1] != hand[j]) { isStraight = false; break; } if (isStraight == false) { break; } } }
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
1) Never do today that which can be put off until tomorrow
2) Tomorrow never comes
•
•
Join Date: Sep 2006
Posts: 17
Reputation:
Solved Threads: 0
I removed the semicolon, but the sort still will not work. The compiler error is and it is saying that the error is in this line .
C++ Syntax (Toggle Plain Text)
DeckOfCards.cpp `faceValue' undeclared (first use this function)
C++ Syntax (Toggle Plain Text)
sortHand( faceValue ); // Sort the array
•
•
Join Date: Sep 2006
Posts: 17
Reputation:
Solved Threads: 0
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,
but there is still a problem.
C++ Syntax (Toggle Plain Text)
static const char *suitValue[ 4 ] = { "Hearts", "Diamonds", "Clubs", "Spades" }; static const char *faceValue[ 13 ] = { "Ace", "Deuce", "Three", "Four", "Five", "Six", "Seven", "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)
sortHand( char const *faceValue ); // Sort the array
but there is still a problem.
•
•
•
•
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,
but there is still a problem.C++ Syntax (Toggle Plain Text)
sortHand( char const *faceValue ); // Sort the array
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
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
![]() |
Similar Threads
- Monitor or Card? (Monitors, Displays and Video Cards)
- War card game (Java)
- card game (Java)
- Card Game Help (Java)
- Card Game (Java)
- AGP support video card question (Monitors, Displays and Video Cards)
Other Threads in the C++ Forum
- Previous Thread: help with a program
- Next Thread: counting & largest # help...
| Thread Tools | Search this Thread |
Tag cloud for C++
6 add api array arrays beginner binary bmp c++ c/c++ calculator char class classes code compile compiler console conversion convert count data delete desktop directshow dll download dynamic encryption error file forms fstream function functions game givemetehcodez google graph gui iamthwee ifstream input int java lib library lines linkedlist linker linux loop looping loops map math matrix memory microsoft newbie news number output pointer problem program programming project python random read recursion recursive reference return string strings struct studio system temperature template templates test text text-file tree unix url variable vector video visual visualstudio void win32 windows winsock wordfrequency wxwidgets






