CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 14 of 14

Threaded View

  1. #1
    Join Date
    Feb 2009
    Location
    USA
    Posts
    68

    Too many pass by reference functions??

    Hi, I am making a blackjack game. Well it is the 3rd one I have made. And i am using structures to simulate the cards, the deck, the players hand, and the players. My "past" BJ programs did not use structures, instead i used Global Arrays to simulate the deck and players hands. The reason i used global arrays is because almost every function changes something about either the deck or the players hands. So instead of passing arrays as arguments I made them global and let everything have easy access to them.
    Now with the structures i am encountering the same issue. Almost every function needs direct access to the structures. So now i am using pass by reference in almost every function call. I was wondering if it is a bad idea to have so many pass by reference functions.

    Below are my Structures and the Function Prototypes

    Code:
    /* Card represents a playing card.
     */
    struct Card
    {
        Card() : value(0), visual("[X]"){} //Default Constructor
        int value;      //Face Value
        string visual;   //Visual Representation. [A],[4],[10]
    };
    
    /* Hand represents a Hand of Cards.
     */
    struct Hand
    {
        Hand() : value(0), count(0){}  //Default Constructor
        int value;        //Sum of FaceValue of all the cards in Hand.
        int count;        //The # of cards in the hand.
        Card cards[10];   //The actual cards in the hand.
    };
    
    /*Deck represents a Deck of Cards
     */
    struct Deck
    {
        Deck() : count(0),top(0){}  //Default Constructor
        int top;           //Holds the position of the card ontop.
        int count;         //The count of cards.
        Card cards[52];    //The actual cards in the deck.
    };
    
    /* Player acts as a blackjack player or dealer.
     */
    struct Player
    {
        Player() : money(1000), bustStatus(false),bet(0){}
        Hand hand;        //Players actual cards.
        int money;        //The players betting money
        int bet;          //The players actual bet.
        bool bustStatus;  //Indicating whether a player busts. true=bust
    };
    
    //Function Prototypes
    
    
    Deck createDeck();             
    void shuffle(Deck&);          
    Card getCard(Deck&);        
    
    void addCardToHand(Hand&,Card);     
    void clearHand(Hand&);                      
    
    void play(Deck&,Player&,Player&);               
    void playersTurn(Player&,Player&,Deck&);     
    void dealersTurn(Player&,Deck&);                
    void getBet(Player&);                                
    void determineWinner(Player&, Player&);      
    
    void printDeck(Deck);
    void printHand(Hand);                    
    void printTable(Player,Player);
    As you can see almost every function is Pass By Reference. Is this bad? or is it ok? I did not think the entire source was neccessary. I have the game finished and it works correctly as far as i can tell but i am not sure about all the references.Also, before anyone suggests using classes i am having some issues getting a simple class to work for me so i cannot implement that atm. Thanks for any comments or suggestions and etc.

    Gerald
    Last edited by g.eckert; March 14th, 2009 at 07:13 AM.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured