Blackjack

This is a discussion on Blackjack within the C++ Programming forums, part of the General Programming Boards category; Got an error with me' code.... was wondering if someone could take a look at the method behind my madness... ...

  1. #1
    VA National Guard The Brain's Avatar
    Join Date
    May 2004
    Location
    Manassas, VA USA
    Posts
    903

    Thumbs up Blackjack

    Got an error with me' code.... was wondering if someone could take a look at the method behind my madness...


    Here is the code so far.. This code is a prototype for a function that I hope to use in my blackjack program. Hopefully, this will allow me to do something like...
    cout << deck[1][5] to display "7 of Diamonds" for example.



    Code:
    #include<cstring>	//provides strcat()
    #include<string>	//provides string class
    
    
    using namespace std;
    
    
    int main()
    {
        
    string deck[3][12]; //create a deck of cards, 4 suits, 13 cards per suit (2 thru Ace)
    string suite, face_value, card;
    
    for (int i = 0; i < 4; i++)  //Use the outter FOR loop to determine the suite of deck[suite][face_value]
    {	
    	switch(i)
    	{
    
    		case 0: suite = "Hearts";
    			    break;
    
    		case 1: suite = "Diamonds";
    			    break;
    
    		case 2: suite = "Clubs";
    			    break;
    
    		case 3: suite = "Spades";
    			    break;
    
    	}//end switch
    
    		for (int j = 0; j < 13; j++)    //The Inner FOR loop will assign card face values
    		{
    			int k = j;   //this allows me to add "2" to the j counter without influencing the FOR loop.
    
    			if (j < 9)	 //elements [0] thru [8] can be populated with numbers 2 thru 10 (numeric card face values)
    
    				face_value = (k + 2); //this allow element zero to contain face value "2"
    
    
    			else
    			{
    				switch(j)
    				{
    
    					case 9:  face_value = "Jack";
    						     break;
    
    					case 10: face_value = "Queen";
    						     break;
    
    					case 11: face_value = "King";
    						     break;
    
    					case 12: face_value = "Ace";
    						     break;
    
    				}//end switch
    				
    			}//end else                
    			
    
    			//This block will create a "card" to be assigned to the deck
    			card = face_value; 
    			strcat(card, " of ");
    			card += suite;
    		 
    			deck[i][j] = card; 
    
    		}//end INNER FOR
    			
    
    }//end OUTTER FOR
    
    return 0;
    
    }//end MAIN()

    Here is the error I be gettin'... using MS visual C++



    --------------------Configuration: bj - Win32 Debug--------------------
    Compiling...
    shuffletest.cpp
    C:\MySource\shuffletest.cpp(67) : error C2664: 'strcat' : cannot convert parameter 1 from 'class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >' to 'char *'
    No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called
    Error executing cl.exe.

    shuffletest.obj - 1 error(s), 0 warning(s)




    Please help if you can
    Last edited by The Brain; 08-21-2004 at 06:48 PM.
    • "Problem Solving C++, The Object of Programming" -Walter Savitch
    • "Data Structures and Other Objects using C++" -Walter Savitch
    • "Assembly Language for Intel-Based Computers" -Kip Irvine
    • "Programming Windows, 5th edition" -Charles Petzold
    • "Visual C++ MFC Programming by Example" -John E. Swanke
    • "Network Programming Windows" -Jones/Ohlund
    • "Sams Teach Yourself Game Programming in 24 Hours" -Michael Morrison
    • "Mathmatics for 3D Game Programming & Computer Graphics" -Eric Lengyel

  2. #2
    VA National Guard The Brain's Avatar
    Join Date
    May 2004
    Location
    Manassas, VA USA
    Posts
    903

    Question

    face_value = (k + 2);


    Do I have to cast (k+2) to a string before assigning to 'face_value'?

    If so.. how would I do this...??

    Last edited by The Brain; 08-21-2004 at 05:40 PM.
    • "Problem Solving C++, The Object of Programming" -Walter Savitch
    • "Data Structures and Other Objects using C++" -Walter Savitch
    • "Assembly Language for Intel-Based Computers" -Kip Irvine
    • "Programming Windows, 5th edition" -Charles Petzold
    • "Visual C++ MFC Programming by Example" -John E. Swanke
    • "Network Programming Windows" -Jones/Ohlund
    • "Sams Teach Yourself Game Programming in 24 Hours" -Michael Morrison
    • "Mathmatics for 3D Game Programming & Computer Graphics" -Eric Lengyel

  3. #3
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    string deck[3][12];

    That is a 3 by 12 2D array. You want a 4 by 13 so you want this:

    string deck[4][13];

    Valid elements range from 0-3 and 0-12 respectively.

    Code:
    You don't need k. 
     
    switch(j)
    {
      case 0:
    	 cout << "Ace";
    	 break;
      case 1:
    	 face_value = "1";
    	break
      //etc;
      case 10:
    	 face_value =  "Jack";
    	 break;
      etc;
     }
    will do. You may be able to assign an int directly to a string, but I tend to doubt it. You could change the int to a C style string using one of the following:

    sprintf();
    stringstream;
    itoa(); //non-standard

    and then assign the C style string to face_value, but for my money I'd just do the 13 level switch.
    Last edited by elad; 08-21-2004 at 06:13 PM.

  4. #4
    Cheesy Poofs! PJYelton's Avatar
    Join Date
    Sep 2002
    Location
    Boulder
    Posts
    1,728
    Yeah, I don't think assigning an int to a string will work, probably better off using the big switch statement or something like this:
    Code:
    string values[13]={"Ace", "Two", etc etc}
    
    face_value=values[j];
    The error is from the fact that you are supplying the strcat function with an argument defined as a string when it takes a char array instead.

  5. #5
    VA National Guard The Brain's Avatar
    Join Date
    May 2004
    Location
    Manassas, VA USA
    Posts
    903

    Thumbs up

    You are right.. I tried to use strcat( ) with a string instead of a character array..

    This is all I had to do to fix me' problem:

    Code:
    //This block will create a "card" to be assigned to the deck
    			card = face_value; 
    			card += " of ";
    			card += suite;


    I was looking forward to using cstring library functions.. but I guess it won't work this time around.

    and I forgot that the '/0' null character is assigned after the last used element of string arrays.. so good call by elad.

    Thanks to elad and PJYelton

    I haven't casted (k+2) to a 'string' yet.. so far everything compiles fine


    --------------------Configuration: bj - Win32 Debug--------------------
    Compiling...
    shuffletest.cpp

    shuffletest.obj - 0 error(s), 0 warning(s)
    Last edited by The Brain; 08-22-2004 at 08:01 AM.
    • "Problem Solving C++, The Object of Programming" -Walter Savitch
    • "Data Structures and Other Objects using C++" -Walter Savitch
    • "Assembly Language for Intel-Based Computers" -Kip Irvine
    • "Programming Windows, 5th edition" -Charles Petzold
    • "Visual C++ MFC Programming by Example" -John E. Swanke
    • "Network Programming Windows" -Jones/Ohlund
    • "Sams Teach Yourself Game Programming in 24 Hours" -Michael Morrison
    • "Mathmatics for 3D Game Programming & Computer Graphics" -Eric Lengyel

  6. #6
    VA National Guard The Brain's Avatar
    Join Date
    May 2004
    Location
    Manassas, VA USA
    Posts
    903

    Unhappy

    Yikes... got the dreaded, "segmentation error"... Hard to debug..

    Just trying to cout the deck[][] to see if it was initialized properly.



    Code:
    #include<cstring>	//provides strcat()
    #include<string>	//provides string class
    #include<iostream>
    #include<cstdlib>
    
    
    using namespace std;
    
    
    int main()
    {
        
    string deck[4][13]; //create a deck of cards, 4 suits, 13 cards per suit
    string suite, face_value, card;
    
    for (int i = 0; i < 4; i++)  //Use the outter FOR loop to determine the suite of deck[suite][face_value]
    {	
    	switch(i)
    	{
    
    		case 0: suite = "Hearts";
    			    break;
    
    		case 1: suite = "Diamonds";
    			    break;
    
    		case 2: suite = "Clubs";
    			    break;
    
    		case 3: suite = "Spades";
    			    break;
    
    	}//end switch
    
    		for (int j = 0; j < 13; j++)    //The Inner FOR loop will assign card face values
    		{	
    			
    			int k = j;   //this allows me to add 2 to the j counter without influencing the for loop.
    
    
    			if (j < 9)	 //elements [0] thru [8] can be populated with numbers 2 thru 10 (numeric card face values)
    
    				face_value = (k + 2); //this allow element zero to contain face value "2"
    
    
    			else
    			{
    				switch(j)
    				{
    
    					case 9:  face_value = "Jack";
    						     break;
    
    					case 10: face_value = "Queen";
    						     break;
    
    					case 11: face_value = "King";
    						     break;
    
    					case 12: face_value = "Ace";
    						     break;
    
    				}//end switch
    				
    			}//end else                 
    			
    
    			//This block will create a "card" to be assigned to the deck
    			card = face_value; 
    			card += " of ";
    			card += suite;
    		 
    			deck[i][j] = card; 
    
    		}//end INNER FOR
    			
    
    }//end OUTTER FOR
    
    
    system("cls");
    
    
    for (i = 0; i < 4; i++)  //MS visual c++ wants "i" redefined, bloodshed doesn't
    	for (int j = 0; j < 53; j++)
    
    		cout << endl << deck[i][j] << endl;
    
    
    return 0;
    
    }//end MAIN()
    Last edited by The Brain; 08-21-2004 at 08:43 PM.
    • "Problem Solving C++, The Object of Programming" -Walter Savitch
    • "Data Structures and Other Objects using C++" -Walter Savitch
    • "Assembly Language for Intel-Based Computers" -Kip Irvine
    • "Programming Windows, 5th edition" -Charles Petzold
    • "Visual C++ MFC Programming by Example" -John E. Swanke
    • "Network Programming Windows" -Jones/Ohlund
    • "Sams Teach Yourself Game Programming in 24 Hours" -Michael Morrison
    • "Mathmatics for 3D Game Programming & Computer Graphics" -Eric Lengyel

  7. #7
    carry on JaWiB's Avatar
    Join Date
    Feb 2003
    Location
    Seattle, WA
    Posts
    1,972
    >>for (int j = 0; j < 53; j++)

    I think that might be a typo
    "Think not but that I know these things; or think
    I know them not: not therefore am I short
    Of knowing what I ought."
    -John Milton, Paradise Regained (1671)

    "Work hard and it might happen."
    -XSquared

  8. #8
    VA National Guard The Brain's Avatar
    Join Date
    May 2004
    Location
    Manassas, VA USA
    Posts
    903
    You guys are awesome....


    I found out.. that although you can cast strings to integers ( atoi() for example).... there is nothing to cast from integers to strings... ( I tried card = static_cast<string>(k+2) but that didn't jive) so... went with declaring everything as a string... and using the 13 level switch case....


    Here it is...


    Tada...


    Code:
    #include<string>	//provides string class
    #include<iostream>      //provides 'cin' and 'cout'
    #include<cstdlib>       //provides system( )
    
    
    using namespace std;
    
    
    int main()
    {
        
    string deck[4][13]; //create a deck of cards, 4 suits, 13 cards per suit
    string suite, face_value, card;
    
    for (int i = 0; i < 4; i++)  //Use the outter FOR loop to determine the suite of deck[suite][face_value]
    {	
    	switch(i)
    	{
    
    		case 0: suite = "Hearts";
    			    break;
    
    		case 1: suite = "Diamonds";
    			    break;
    
    		case 2: suite = "Clubs";
    			    break;
    
    		case 3: suite = "Spades";
    			    break;
    			    
                    default:  cout << "\n\nsuite loop overflow\n";
    
    	}//end switch
    
    		for (int j = 0; j < 13; j++)    //The Inner FOR loop will assign card face values
    		{	
    		  switch(j)
    		  {
    		    case 0:  face_value = "2";
    		                  break;
    				             
                        case 1:  face_value = "3";
                                 break;
                                 
                        case 2:  face_value = "4";
                                 break;
                                 
                        case 3:  face_value = "5";
                                 break;
                        
                        case 4:  face_value = "6";
                                 break;
                                 
                        case 5:  face_value = "7";
                                 break;
                        
                        case 6:  face_value = "8";
                                 break;
                                 
                        case 7:  face_value = "9";
                                 break;
                                 
                        case 8:  face_value = "10";
                                 break;                   
    
    	            case 9:  face_value = "Jack";
    		             break;
    
    	            case 10: face_value = "Queen";
    		             break;
    
    	            case 11: face_value = "King";
    		              break;
    
    	            case 12: face_value = "Ace";
    		              break;
    						     
                        default: cout << "\n\nface_value loop overflow\n";
    
    				}//end switch		              
    			
    
    			//This block will create a "card" to be assigned to the deck
    			card = face_value; 
    			card += " of ";
    			card += suite;
    		 
    			deck[i][j] = card; 
    
    		}//end INNER FOR
    			
    
    }//end OUTTER FOR
    
    
    system("cls");
    
    //Display the deck
    for (int p = 0; p < 4; p++)
    	for (int q = 0; q < 13 ; q++)
    
    		cout << endl << deck[p][q] << endl;
    
    
    return 0;
    
    }//end MAIN()


    thanks again everyone
    Last edited by The Brain; 08-21-2004 at 11:52 PM.
    • "Problem Solving C++, The Object of Programming" -Walter Savitch
    • "Data Structures and Other Objects using C++" -Walter Savitch
    • "Assembly Language for Intel-Based Computers" -Kip Irvine
    • "Programming Windows, 5th edition" -Charles Petzold
    • "Visual C++ MFC Programming by Example" -John E. Swanke
    • "Network Programming Windows" -Jones/Ohlund
    • "Sams Teach Yourself Game Programming in 24 Hours" -Michael Morrison
    • "Mathmatics for 3D Game Programming & Computer Graphics" -Eric Lengyel

  9. #9
    Registered User manofsteel972's Avatar
    Join Date
    Mar 2004
    Posts
    317
    Just curious how do you plan to shuffle your deck?
    "Knowledge is proud that she knows so much; Wisdom is humble that she knows no more."
    -- Cowper

    Operating Systems=Slackware Linux 9.1,Windows 98/Xp
    Compilers=gcc 3.2.3, Visual C++ 6.0, DevC++(Mingw)

    You may teach a person from now until doom's day, but that person will only know what he learns himself.

    Now I know what doesn't work.

    A problem is understood by solving it, not by pondering it.

    For a bit of humor check out xkcd web comic http://xkcd.com/235/

  10. #10
    carry on JaWiB's Avatar
    Join Date
    Feb 2003
    Location
    Seattle, WA
    Posts
    1,972
    Actually you can convert (technically not a cast) an integer to a std::string and a std::string to an integer. Check out the faq, I know it's in there and it involves using streams.
    "Think not but that I know these things; or think
    I know them not: not therefore am I short
    Of knowing what I ought."
    -John Milton, Paradise Regained (1671)

    "Work hard and it might happen."
    -XSquared

  11. #11
    VA National Guard The Brain's Avatar
    Join Date
    May 2004
    Location
    Manassas, VA USA
    Posts
    903
    I'm glad you asked...

    I thought about this.. and came up with two ways to approach this problem:


    The first way.. would use a one dimensional array deck[52] for example. The advantage with using a single dimensional array would be that I could shuffle the deck.. and draw from the top of the stack just like you would in real life.


    so I was thinking something like...
    Code:
    void shuffle()
    {
         for (int i = 0; i < 52: i++)
    
              deck[i] = deck[rand()%52];  
    }
    This algorithm by itself is not perfect (you could assign many dupicates of the same card to the deck for example) To get around this.. you make a camparison of "card_drawn" to "cards_in_play". If there is a duplication, simply make another function call to shuffle().



    With using a 2D deck however, there is no luxury of shuffling the deck to remove a card from the top of the stack... so the way you get around this.. is to draw random cards from deck[][]... kinda like how a magician would ask you to, "pick a card.. any card... "

    Code:
    string get_card()
    {
    	int suite = 0,
            face_value = 0;
    
    	suite = rand()%4;
    
    	face_value = rand()%13;
    	
    
    	string card_drawn = deck[suite][face_value];
    
            return card_drawn;
    
    }
    Again.. with this method, there is a possibility to draw duplicate cards.. so simply make a comparison if (card_drawn == cards_in_play) then get_card().

    Maybe there are better ways to go about this... so if you got any better suggestions.. just let me know... and maybe others can learn as well
    Last edited by The Brain; 08-21-2004 at 11:01 PM.
    • "Problem Solving C++, The Object of Programming" -Walter Savitch
    • "Data Structures and Other Objects using C++" -Walter Savitch
    • "Assembly Language for Intel-Based Computers" -Kip Irvine
    • "Programming Windows, 5th edition" -Charles Petzold
    • "Visual C++ MFC Programming by Example" -John E. Swanke
    • "Network Programming Windows" -Jones/Ohlund
    • "Sams Teach Yourself Game Programming in 24 Hours" -Michael Morrison
    • "Mathmatics for 3D Game Programming & Computer Graphics" -Eric Lengyel

  12. #12
    Anti-Poster
    Join Date
    Feb 2002
    Posts
    1,399
    I prefer the one-dimensional deck with a shuffle function. I made a Deck class way back in the day and used it for several different card games (including blackjack). If I find it and you want to take a peek, I can send it to you.
    If I did your homework for you, then you might pass your class without learning how to write a program like this. Then you might graduate and get your degree without learning how to write a program like this. You might become a professional programmer without knowing how to write a program like this. Someday you might work on a project with me without knowing how to write a program like this. Then I would have to do you serious bodily harm. - Jack Klein

  13. #13
    VA National Guard The Brain's Avatar
    Join Date
    May 2004
    Location
    Manassas, VA USA
    Posts
    903

    Thumbs up :)

    that would be very cool


    email addy is in me' profile if you want to send it my way
    • "Problem Solving C++, The Object of Programming" -Walter Savitch
    • "Data Structures and Other Objects using C++" -Walter Savitch
    • "Assembly Language for Intel-Based Computers" -Kip Irvine
    • "Programming Windows, 5th edition" -Charles Petzold
    • "Visual C++ MFC Programming by Example" -John E. Swanke
    • "Network Programming Windows" -Jones/Ohlund
    • "Sams Teach Yourself Game Programming in 24 Hours" -Michael Morrison
    • "Mathmatics for 3D Game Programming & Computer Graphics" -Eric Lengyel

  14. #14
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    Code:
    int main()
    {
    int deck[52];
    /* assign inital values to deck */
    for (int count=0; count<100; count++)
      swap(deck, random(52), random(52));
    
    for (int count=0; count<sizeof deck / sizeof deck[0]; count++)
      std::cout<<deck[count]<<endl;
    
    }
    
    void swap(int *arr, int x, int y) 
    {
      int temp = arr[x];
      arr[x] = arr[y];
      arr[y] = temp;
    }
    Note: There is also a swap function in <algorithm> and you should really use that one instead.

    By doing it this way you can ensure that you won't have duplicates entires in the array.

  15. #15
    Cheesy Poofs! PJYelton's Avatar
    Join Date
    Sep 2002
    Location
    Boulder
    Posts
    1,728
    Another similar way to shuffle a deck which gives a perfectly random deck is:
    Code:
    for (int x=0; x<52; x++)
        swap (deck[x], deck[random()%52]);
    The advantage to this is that you don't need to worry about what number (100 in thantos's example) is the best number to use.

Page 1 of 5 12345 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Simple Blackjack Program
    By saber1357 in forum C Programming
    Replies: 1
    Last Post: 03-28-2009, 04:19 PM
  2. Help with Blackjack program
    By sugie in forum C++ Programming
    Replies: 1
    Last Post: 04-30-2005, 01:30 AM
  3. Blackjack!
    By Dr. Bebop in forum Game Programming
    Replies: 1
    Last Post: 10-03-2002, 09:58 PM
  4. Blackjack
    By the_head in forum C Programming
    Replies: 1
    Last Post: 08-03-2002, 09:57 AM
  5. BlackJack Program...
    By 67stangman in forum C++ Programming
    Replies: 3
    Last Post: 05-06-2002, 11:44 PM

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