Making a Blackjack game..

This is a discussion on Making a Blackjack game.. within the Game Programming forums, part of the General Programming Boards category; You don't need to create the cards dynamically - vector<Card> will make copies anyways. So just have a local Card ...

  1. #16
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    You don't need to create the cards dynamically - vector<Card> will make copies anyways.

    So just have a local Card c variable that you set with the value, then push_back(c).

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  2. #17
    Absent Minded Programmer
    Join Date
    May 2005
    Posts
    964
    Code:
    // This header file declares the deck class and all of its functions,
    // as well as the card structure.
    
    #include <vector>
    #include <iostream>
    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    #include <algorithm>
    
    struct Card
    {
    	int suit;
    	int value;
    };
    
    class Deck
    {
    public:	
    	Deck()
    	{
    		int x = 0;
    		int y = 0;
    		int loopvar = 0;
    
    			for (y = 0; y < 4;  y++)
    			{
    				
    				for (x = 0; x < 13; x++)
    				{
    					Card c;
    					Cards.push_back(c);
    					Cards[loopvar].suit = y;
    					Cards[loopvar].value = x;
    					loopvar++;
    				}
    			}
    		if (loopvar != 52) 
    			std::cout 
    			<< "The correct number of cards" 
    			<< " has not been created!"
    			<< std::endl;
    	}
    
    	void ShuffleDeck()
    	{
    		int random = 0;
    		int loopvar = 0;
    		for (loopvar; loopvar < 52; loopvar++)
    		{
    			random = rand() % 52;
    			std::swap(Cards[loopvar], Cards[random]);
    		}
    	} 
    
    private:
    	std::vector<Card> Cards;
    };
    Woot, mmk no errors and I'm pretty sure it all works, now I can move on I think.
    Sometimes I forget what I am doing when I enter a room, actually, quite often.

  3. #18
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    I was thinking more along the lines of:
    Code:
    					Card c;
    					c.suit = y;
    					c.value = x;
    					Cards.push_back(c);
    A bit neater and simpler, and now you can get rid of loopvar altogether - just check Cards.size() when you are finished.

    By the way, it's a good idea to have consistant naming convention for types vs. variables. All other variables start with a lowercase, but your Cards variable is capitalized - inconsistant. [It just makes life a lot simpler if you have one rule that you follow all the time]

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  4. #19
    Absent Minded Programmer
    Join Date
    May 2005
    Posts
    964
    Baha!!! It works!
    Code:
    	Deck()
    	{
    		int x = 0;
    		int y = 0;
    			for (y = 0; y < 4;  y++)
    			{
    				
    				for (x = 0; x < 13; x++)
    				{
    					Card c;
    					c.suit = y;
    					c.value = x;
    					Cards.push_back(c);
    				}
    			}
    			assert(Cards.size() == 52);
    	}
    Wow, thats like, really refined code compared to what I initially came up with, I must be rusty or something....
    Sometimes I forget what I am doing when I enter a room, actually, quite often.

  5. #20
    Absent Minded Programmer
    Join Date
    May 2005
    Posts
    964
    I think I'm gonna rethink the class structure.

    The way I see it the deck shouldn't be shuffling itself, so maybe the dealer class should actually have the shuffle function...

    As well as functions for dealing cards and taking bets and such...

    Also, I changed up the naming convention only slightly, I made the instance of Cards a capital C

    Code:
    	Deck()
    	{
    		int x = 0;
    		int y = 0;
    			for (y = 0; y < 4;  y++)
    			{
    				
    				for (x = 0; x < 13; x++)
    				{
    					Card C;
    					C.suit = y;
    					C.value = x;
    					Cards.push_back(C);
    				}
    			}
    			assert(Cards.size() == 52);
    	}
    I like to keep my own types capitalized, like classes and structures and such, but I usually leave variables like ints and stuff lowercase.
    Last edited by Shamino; 11-05-2007 at 02:40 PM.
    Sometimes I forget what I am doing when I enter a room, actually, quite often.

  6. #21
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    The common principle is that Types start with capitals and variables start with lower-case. What you don't want is that you change a type from one thing to another, and go through 5000 lines of code and change a variable name.

    But of course, you write your programs, I write mine [and my employer says how _I_ should do it, not me - because I work for a company with hundreds of software engineer producing code that is largely delivered as source-code to big company customers - so style is important].

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  7. #22
    Absent Minded Programmer
    Join Date
    May 2005
    Posts
    964
    yeah i see where you're coming from...

    But.. What exactly do you mean by types? Do you mean classes and structures and stuff?

    And when you say variables, do you mean an instance of a class as a variable? or an int or something like that? Could you be more specific?
    Sometimes I forget what I am doing when I enter a room, actually, quite often.

  8. #23
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Types:
    classes, enums, structs, unions, typedefs and basic types (int, long int, char, unsigned char, float, double, and all the other variations that are part of the language itself).

    Variables:
    Instances of the above including arguments into functions and members of classes and structures.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  9. #24
    Absent Minded Programmer
    Join Date
    May 2005
    Posts
    964
    I have a question, If I want to attach a string to every card value, yknow hearts clubs diamonds spades etc.. as well as king queen and whatnot.. Should I use an enum type? or add more types to the card structure?
    Sometimes I forget what I am doing when I enter a room, actually, quite often.

  10. #25
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    There are two parts to this question:

    1. Internal representation of Suit and Card Value. [This is what happens in the code, and not visible to the user].
    Ok, so first of all, the only part that really cares about what the numbers actually represent is the user. You could use 46 to represent Clubs, 9712 for Hearts, 14312 for Diamonds and 29534 for Spades - the C programming language wouldn't care - you may have a harder time to remember which is which. If you really want to make it hard for yourself, you can also represent the individual card values with whatever number you like. But that makes life utterly miserable and complicated, because you now need translation tables [or lots of code], etc.

    I would think using enums for the suite and card value makes sense. Particularly as [I think] the card values aren't exactly 1..13, but rather 1..10 or 2-11 or some such, right? So you still need a way to convert a card "value" to a numeric "score" [even if some of that can be done "straight" using the right methods.

    2. External represenation of Suite and Card Value. [This is what the user of the program sees].
    This is where it needs to make sense. The most obvious solution is to use standard names and values as strings, so "Queen of Hearts" or "Ace of Spades", "10 of Diamonds" for example.

    I would suggest that the best way to achieve this is to use a fixed set of strings that you get from calling a function, e.g.:
    Code:
    string SuiteStr(int suit) 
    {
        switch(suit)
        {
            case 0: return "Hearts";
            case 1: return "Clubs";
            case 2: return "Clubs";
            case 3: return "Clubs";
           default: return "??? Invalid suit???";
        }
    }
    or:
    Code:
    string SuiteStr(int suit) 
    {
        static const string suites[4] = { "Hearts", "Clubs", "Diamonds", "Spades" };
        if (suit >= 0 && suit < 4) return suites[suit];
        else return "??? Invalid ???";
    }
    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  11. #26
    Absent Minded Programmer
    Join Date
    May 2005
    Posts
    964
    I like the switch method better myself, it looks neater, I'm gonna work on that, but where does the enum come into play? In the switch?

    I figure I also need an enum for the card values 2-A, Two Three Four Jack Queen King Ace, and all that in between.. So I can use something like that switch function to have the dealer class "read" the cards and display it on the screen.
    Sometimes I forget what I am doing when I enter a room, actually, quite often.

  12. #27
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Well, I wrote it without enums because I didn't want to influence your enum design. I'll let you come up with something and then comment if you don't get it right. You'll learn more that way, don't you think?

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  13. #28
    Absent Minded Programmer
    Join Date
    May 2005
    Posts
    964
    I've been looking at this example I found on codeguru http://www.codeguru.com/cpp/cpp/cpp_...cle.php/c4067/

    I'm thinking I'll be able to do something similar with my dealer class...

    The dealer class will be able to initialize an enum for suit and card value and assosiate a string with it. This way I can create a "read cards" function that will spit out a coherant string for certain cards that are currently being used.
    I can create a switch using this enum that will be able to take card information and then output it on the screen in human fashion, rather than computer fashion, sound about right?
    Sometimes I forget what I am doing when I enter a room, actually, quite often.

  14. #29
    Absent Minded Programmer
    Join Date
    May 2005
    Posts
    964
    alluva sudden it seems that an enum might be a waste...............


    I could create a map in the dealer class that connects 0-12 with a string value.
    And another map with 0-3 to a string suit.
    Then just read the card and ouput the connected string, skipping the whole switch, or a big if/else chain.
    Sometimes I forget what I am doing when I enter a room, actually, quite often.

  15. #30
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    You could even get away with something like this for the names, a sort of lookup table:
    Code:
    const char *card[] = {
        "ace", "two", /* ... */ "king"
    };
    const char *suit[] = {
        "clubs", "diamonds", "hearts", "spades"  /* alphabetical order */
    }
    
    std::cout << card[cardnum] << " of " << suit[suitid] << std::endl;
    I just really like lookup tables . . .
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

Page 2 of 4 FirstFirst 1234 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Should I use a game engine in making a RPG game?
    By m3rk in forum Game Programming
    Replies: 6
    Last Post: 01-26-2009, 03:58 AM
  2. beach bar (sims type game)
    By DrKillPatient in forum Game Programming
    Replies: 1
    Last Post: 03-06-2006, 12:32 PM
  3. PC Game project requires c++ programmers
    By drallstars in forum Projects and Job Recruitment
    Replies: 2
    Last Post: 02-21-2006, 11:23 PM
  4. blackjack game Please, please help!!
    By collegegal in forum C Programming
    Replies: 4
    Last Post: 03-11-2002, 07:02 PM
  5. Lets Play Money Making Game
    By ggs in forum A Brief History of Cprogramming.com
    Replies: 1
    Last Post: 09-04-2001, 08:36 PM

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