pointers and objects

This is a discussion on pointers and objects within the C++ Programming forums, part of the General Programming Boards category; So, I'm writing a Texas Hold'em Poker game for Uni, and I've run into a bit of a problem. I've ...

  1. #1
    Registered User
    Join Date
    Aug 2006
    Posts
    163

    pointers and objects

    So, I'm writing a Texas Hold'em Poker game for Uni, and I've run into a bit of a problem. I've got an object called Game that handles all the things specific to this particular game(checking handranks, calculating strength of computer hand, etc...). Game also handles the deal function. It takes two CardStack objects as it's parameters(which I created, as well as the Card object).
    I can create a deck of 52 cards (2-Ace, all suit) and shuffle them. But when I try to deal from the Deck to the playerHand I run into a problem. It took me several hours, but I figured out that I need to use pointers, but I also figured out that I don't know exactly how to go about it in this context. Here's the code:

    mainline code
    Code:
    #include "Game.h"
    #include "Gamble.h"
    #include "Player.h"
    using namespace std;
    
    int main()
    {
        Player newPlayer;
        newPlayer.set_Name();
        cout << "In the mainline! \n creating newGame\n";
        Game newGame;
        cout << "creating new CardStacks\n";
        CardStack newDeck, playerHand, computerHand, community;
        cout << "Creating new Card object.\n";
        Card newCard;
        Gamble newGamble;
        cout << "Loading the deck\n";
        cout << "Number of cards = " << newDeck.get_numCards() << "\n";
        for (int suit=3; suit >= 0; suit--)
    	{
    		for (int p_value=12; p_value >= 0; p_value--)
    		{
                newCard.set_rank(p_value);
                newCard.set_suit(suit);
    			newDeck.add_card(newCard);
    
    		}//!End for loop
    	}//! End for loop */
    
        cout << "Deck is loaded, display the deck!\n";
        //newCard.make_card();
    
    	for(int i = 0; i < 52; i++)
    	{
                newDeck.get_card(i).display();
                cout << ",";
                if(i == 13 || i == 26 || i == 39)
                {
                     cout << endl;
                }
        }//!End for loop*/
    
        cout << "Deck displayed, now shuffle.\n";
    
        newDeck.shuffle();
        newDeck.shuffle();
        cout << "Number of cards: " << newDeck.get_numCards();
        cout << "\nShuffled cards:\n";
        //newCard = newDeck.get_card(23);
        //newCard.display();
    
    
        for(int i = 0; i < 52; i++)
    	{
                newCard = newDeck.get_card(i);
                newCard.display();
                cout << ",";
                if(i == 13 || i == 26 || i == 39)
                {
                     cout << endl;
                }
        }//!End for loop*/
    
    
        //This is where I run into the problem!!!
    
        newGame.deal(newDeck, playerHand);
        newGame.deal(newDeck, computerHand);
        newGame.deal(newDeck, playerHand);
        newGame.deal(newDeck, computerHand);
    Game class, deal function
    Code:
    void Game::deal(CardStack stackOne, CardStack stackTwo)
    {
             if(!stackOne.is_empty() && !stackTwo.is_full())
             {
                 cout << "\nIn the deal function!";
                 cout << "\nNumber of cards in deck = " << stackOne.get_numCards();
                 Card newCard;
                 newCard = stackOne.get_card(stackOne.get_numCards()-1);
                 newCard.display();
                 stackTwo.add_card(newCard);
                  //stackTwo.add_card(stackOne.get_card(stackOne.get_numCards()-1));
                  cout << "\nNumber of cards in new stack " << stackTwo.get_numCards() <<"\n";
                stackOne.remove();
    
             }//!End if stackone isn't empty, and stacktwo isn't full
             else
             {
                 cout << "Oh snap! Something went wrong!\n";
                 //Do something for if stackOne is empty or if stackTwo is full
             }
    
    }//!End deal(CardStack, CardStack)
    The problem is(no I see it clear as day) that the CardStacks that are being passed to it aren't being changed, just the new cardstacks get changed, then don't get sent anywhere. So I'm lost as to how to go about setting everything up to use pointers. We never really covered pointers very much in class(and even then it was just like pointers to integers). If any of you could point me in the right direction, I'd very much appreciate it.

    and if you need to see anymore of the code, just let me know. Thanks.

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    7,340
    >> the CardStacks that are being passed to it aren't being changed
    Just pass them by reference:
    Code:
    void Game::deal(CardStack& stackOne, CardStack& stackTwo)

  3. #3
    Registered User
    Join Date
    Aug 2006
    Posts
    163
    in the mainline, what will have to change though? Anything?

  4. #4
    Registered User
    Join Date
    Aug 2006
    Posts
    163
    Quote Originally Posted by System_159
    in the mainline, what will have to change though? Anything?
    well, I went ahead and tried doing just what you had posted, and lo and behold... it works! Awesome! Now I just have to figure out why my getBestHand algorithm isn't working correctly...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Returning an Array of Pointers to Objects
    By randomalias in forum C++ Programming
    Replies: 4
    Last Post: 04-29-2006, 02:45 PM
  2. pointers
    By InvariantLoop in forum C Programming
    Replies: 13
    Last Post: 02-04-2005, 08:32 AM
  3. Replies: 4
    Last Post: 10-16-2003, 11:26 AM
  4. Vectors of pointers to objects
    By Myownworstenemy in forum C++ Programming
    Replies: 3
    Last Post: 09-01-2003, 11:23 PM
  5. Objects, or pointers to objects?
    By Unregistered in forum C++ Programming
    Replies: 5
    Last Post: 12-17-2001, 11:57 PM

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