if statement with arrays help

This is a discussion on if statement with arrays help within the C++ Programming forums, part of the General Programming Boards category; hello Im making a card game using arrays to setup the deck and produce cards. i've got everything done except ...

  1. #1
    new to c++
    Join Date
    Feb 2009
    Posts
    53

    if statement with arrays help

    hello

    Im making a card game using arrays to setup the deck and produce cards. i've got everything done except for scoring both the players hand and dealers hand, to see who wins. I dont know how I would setup an if statement for seeing what you have in a hand once all cards are flipped.
    this is what I have so far for dealing the cards and scoring the hand:
    Code:
    void shuffle(int wdeck[][13])
    {
    
    	int row;
    	int column;
    	int card;
    
    	for(card = 1; card <= 52; card++) {
    		do {
    			row = rand() % 4;
    			column = rand() % 13;
    		}while(wdeck [row] [column] !=0);
    
    		wdeck [row] [column] = card;
    	}
    }
    
    void deal2(const int wdeck[][13], const char *wface[], 
    		  const char *wsuit[])
    {
    	int card;
    	int row;
    	int column;
    	
    	for ( card = 1; card <= 2; card++ ) {
    		
    		for ( row = 0; row <= 3; row++ ){
    
    			for ( column = 0; column <=12; column++ ){
    
    				if ( wdeck[row][column] == card ) {
    
    					printf("%5s%-8s%\n", wface[column], wsuit[row],
    					card % 52 == 0 );
    				
    					
    					
    				}		
    			}
    		}
    	}
    }
    void dealcommunity(const int wdeck[][13], const char *wface[], 
    		  const char *wsuit[])
    {
    	int card;
    	int row;
    	int column;
    
    	
    
    	
    	for ( card = 3; card <= 3; card++ ) {
    		
    		for ( row = 0; row <= 3; row++ ){
    
    			for ( column = 0; column <=12; column++ ){
    
    				if ( wdeck[row][column] == card ) {
    
    					printf("%5s%-8s%\n", wface[column], wsuit[row],
    					card % 52 == 0 );
    					
    					
    				}		
    			}
    		}
    	}
    }
    void dealcommunity2(const int wdeck[][13], const char *wface[], 
    		  const char *wsuit[])
    {
    	int card;
    	int row;
    	int column;
    
    	
    
    	
    	for ( card = 3; card <= 4; card++ ) {
    		
    		for ( row = 0; row <= 3; row++ ){
    
    			for ( column = 0; column <=12; column++ ){
    
    				if ( wdeck[row][column] == card ) {
    
    					printf("%5s%-8s%\n", wface[column], wsuit[row],
    					card % 52 == 0 );
    					
    					
    				}		
    			}
    		}
    	}
    }
    void dealcommunity3(const int wdeck[][13], const char *wface[], 
    		  const char *wsuit[])
    {
    	int card;
    	int row;
    	int column;
    
    	
    
    	
    	for ( card = 3; card <= 5; card++ ) {
    		
    		for ( row = 0; row <= 3; row++ ){
    
    			for ( column = 0; column <=12; column++ ){
    
    				if ( wdeck[row][column] == card ) {
    
    					printf("%5s%-8s%\n", wface[column], wsuit[row],
    					card % 52 == 0 );
    					
    					
    				}		
    			}
    		}
    	}
    }
    void dealdealer( const int wdeck[][13], const char *wface[],const char *wsuit[])
    {
    	int dcard;
    	int drow;
    	int dcolumn;
    	
    	for ( dcard = 6; dcard <= 7; dcard++ ) {
    		
    		for ( drow = 0; drow <= 3; drow++ ){
    
    			for ( dcolumn = 0; dcolumn <=12; dcolumn++ ){
    
    				if ( wdeck[drow][dcolumn] == dcard ) {
    					
    
    					printf("%5s%-8s%\n", wface[dcolumn], wsuit[drow],
    					dcard % 52 == 0 );
    				
    					
    					
    				}		
    			}
    		}
    	}
    }
    void playerhandscore(int& playerpoint)
    {
    	if()//An Ace-High straight of one suit.
    	{
    		cout<<"You have a Royal Flush\n";
    		playerpoint=9;
    	}
    	if()//A straight of entirely one suit.
    	{
    		cout<<"You have a Straight Flush\n";
    			playerpoint=8;
    	}
    	if()//Four cards of the same rank
    	{
    		cout<<"You have Four of a Kind\n";
    		playerpoint=7;
    	}
    	if()//Three-of-a-kind and a pair. 
    	{
    		cout<<"You have a Full House";
    		playerpoint=6;
    	}
    	if()//Five cards of the same suit.
    	{
    		cout<<"You have a Flush\n";
    		playerpoint=5;
    	}
    	if()//Five cards of sequential rank
    	{
    		cout<<"You have a Straight\n";
    		playerpoint=4;
    	}
    	if()//Three cards of the same rank
    	{
    		cout<<"You have Three of a Kind\n";
    		playerpoint=3;
    	}
    	if()//Two cards of the same rank and another two cards of the same rank. 
    	{
    		cout<<"You have Two Pair\n";
    		playerpoint=2;
    	}
    	if()//Two cards of the same rank.
    	{
    		cout<<"You have a Pair\n";
    		playerpoint=1;
    	}
    	if()//When you don't have any of the above, your highest card determines your hand.
    	{
    		cout<<"You have the High card\n";
    		playerpoint=1;
    
    	}
    }
    void dplayerhandscore(int& dplayerpoint)
    {
    	if()//An Ace-High straight of one suit.
    	{
    		cout<<"You have a Royal Flush\n";
    		dplayerpoint=9;
    	}
    	if()//A straight of entirely one suit.
    	{
    		cout<<"You have a Straight Flush\n";
    		dplayerpoint=8;
    	}
    	if()//Four cards of the same rank
    	{
    		cout<<"You have Four of a Kind\n";
    		dplayerpoint=7;
    	}
    	if()//Three-of-a-kind and a pair. 
    	{
    		cout<<"You have a Full House";
    		dplayerpoint=6;
    	}
    	if()//Five cards of the same suit.
    	{
    		cout<<"You have a Flush\n";
    		dplayerpoint=5;
    	}
    	if()//Five cards of sequential rank
    	{
    		cout<<"You have a Straight\n";
    		dplayerpoint=4;
    	}
    	if()//Three cards of the same rank
    	{
    		cout<<"You have Three of a Kind\n";
    		dplayerpoint=3;
    	}
    	if()//Two cards of the same rank and another two cards of the same rank. 
    	{
    		cout<<"You have Two Pair\n";
    		dplayerpoint=2;
    	}
    	if()//Two cards of the same rank.
    	{
    		cout<<"You have a Pair\n";
    		dplayerpoint=1;
    	}
    	if()//When you don't have any of the above, your highest card determines your hand.
    	{
    		cout<<"You have the High card\n";
    		dplayerpoint=1;
    
    	}
    }

  2. #2
    Registered User rogster001's Avatar
    Join Date
    Aug 2006
    Location
    Liverpool UK
    Posts
    1,403
    Well if you think about it, it's clear there is a fair bit of logical statement checking to do, so you are going to be left with a lot of messy code if you try and handle it within each of those if statements.
    You will need to create further functions that take the current hand and go off and do some counting.

    I would use an enum for convenience and have your checking functions return a value appropriate to the players hand value.

    Code:
    enum hand_values {
    
    	ROYAL,
    	FHOUSE,
    	//etc
    	//etc
    };
    You are probably going to require, or should use, more than one function to accomplish this in a well structured way. Also don't forget to always record any loose high card values anyway (if the player does not have a hand that scores all of the cards), in case of ties...
    Last edited by rogster001; 12-14-2010 at 05:42 AM.
    Thought for the day:
    "Are you sure your sanity chip is fully screwed in sir?" (Kryten)
    FLTK: "The most fun you can have with your clothes on."

    Stroustrup:
    "If I had thought of it and had some marketing sense every computer and just about any gadget would have had a little 'C++ Inside' sticker on it'"

  3. #3
    new to c++
    Join Date
    Feb 2009
    Posts
    53
    I tried this, but i keep getting error messages saying that face and suit are undeclared identifiers. could i get some help please?
    Code:
    void checkroyal(int& playerpoint,const char *wsuit[],const char *wface[])
    {
    	if( (strcmp(*suit,"A")==0)&& strcmp(*face," of Hearts")==0)||
    		(strcmp(*suit,"J")==0 && strcmp(*face," of Hearts")==0)||
    		(strcmp(*suit,"Q")==0 && strcmp(*face," of Hearts")==0)||
    		(strcmp(*suit,"K")==0 && strcmp(*face," of Hearts")==0)||
    		(strcmp(*suit,"10")==0 && strcmp(*face," of Hearts")==0)		)//An Ace-High straight of one suit.
    	{
    		cout<<"You have a Royal Flush\n";
    		playerpoint=9;
    	}
    
    	if(
    		(strcmp(suit,"A")==0)&& strcmp(face," of Diamonds")==0)||
    		(strcmp(suit,"J")==0 && strcmp(face," of Diamonds")==0)||
    		(strcmp(suit,"Q")==0 && strcmp(face," of Diamonds")==0)||
    		(strcmp(suit,"K")==0 && strcmp(face," of Diamonds")==0)||
    		(strcmp(suit,"10")==0 && strcmp(face," of Diamonds")==0)
    		)//An Ace-High straight of one suit.
    	{
    		cout<<"You have a Royal Flush\n";
    		playerpoint=9;
    	}
    	if(
    		(strcmp(suit,"A")==0)&& strcmp(face," of Clubs")==0)||
    		(strcmp(suit,"J")==0 && strcmp(face," of Clubs")==0)||
    		(strcmp(suit,"Q")==0 && strcmp(face," of Clubs")==0)||
    		(strcmp(suit,"K")==0 && strcmp(face," of Clubs")==0)||
    		(strcmp(suit,"10")==0 && strcmp(face," of Clubs")==0)
    		)//An Ace-High straight of one suit.
    	{
    		cout<<"You have a Royal Flush\n";
    		playerpoint=9;
    	}
    
    	if(
    		(strcmp(suit,"A")==0)&& strcmp(face," of Spades")==0)||
    		(strcmp(suit,"J")==0 && strcmp(face," of Spades")==0)||
    		(strcmp(suit,"Q")==0 && strcmp(face," of Spades")==0)||
    		(strcmp(suit,"K")==0 && strcmp(face," of Spades")==0)||
    		(strcmp(suit,"10")==0 && strcmp(face," of Spades")==0)
    		)//An Ace-High straight of one suit.
    	{
    		cout<<"You have a Royal Flush\n";
    		playerpoint=9;
    	}
    	else
    	{
    		cout<<"Its not a royal flush"<<endl;
    	}
    }
    Last edited by rfoor; 12-14-2010 at 02:59 PM.

  4. #4
    Registered User
    Join Date
    Dec 2010
    Location
    Australia
    Posts
    8
    An undeclared identifier error occurs when the compiler can not recognise an identifier within your code; be it a sequence of characters representing: an object or variable name, class name etc. In your case we are looking at the error with regards to two variable names that have stumped the compiler.

    If you have a look at your void function "checkroyal", there are no declarations for the variables with character sequences "face" and "suit". Instead, the closest thing that we can find, are the parameters residing with the function, named "wface" and "wsuit".

    Therefore, I think you have just made a simple typo when trying to use the variables passed to the function. Try renaming the actual parameter variables to "face" and "suit" (also changing them in the function declaration for consistency), or renaming each of the references to "face" and "suit" to "wface" and "wsuit".

    In future, try to learn the meanings of some common errors, such as this one. In the long run it will save you a lot of time, instead of having to wait for forum replies

    Hope I was able to help!

  5. #5
    Registered User rogster001's Avatar
    Join Date
    Aug 2006
    Location
    Liverpool UK
    Posts
    1,403
    before you go any further with this i suggest you save yourself some pain and learn how to write your own 'struct' data type to represent the cards. each card has a suit and a value at least, as well as other potential attributes like 'is_dealt' , is_face, point_val, etc depending on your game and how you want to do things. So I would personally stop now and go back and rewrite, then when you come to calculate your hands you should find it easier as all the information required for each card is located within it's datatype and the function calls will be simpler also.
    Thought for the day:
    "Are you sure your sanity chip is fully screwed in sir?" (Kryten)
    FLTK: "The most fun you can have with your clothes on."

    Stroustrup:
    "If I had thought of it and had some marketing sense every computer and just about any gadget would have had a little 'C++ Inside' sticker on it'"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Grading Program using multidimensional arrays
    By HBlakeH in forum C Programming
    Replies: 15
    Last Post: 11-17-2010, 11:37 PM
  2. How to create and manipulate Terabyte size Arrays with Win32API
    By KrishnaPG in forum Windows Programming
    Replies: 1
    Last Post: 11-05-2009, 03:08 AM
  3. Crazy memory problem with arrays
    By fusikon in forum C++ Programming
    Replies: 9
    Last Post: 01-15-2003, 08:24 PM
  4. OpenGL drawing w/ Arrays or not?
    By tegwin in forum Game Programming
    Replies: 2
    Last Post: 01-14-2003, 02:31 PM
  5. sorting arrays
    By Unregistered in forum C++ Programming
    Replies: 3
    Last Post: 10-13-2001, 05:39 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21