Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6926469
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T10:55:20+00:00 2026-05-27T10:55:20+00:00

Ignore the fact that I using raw pointers/arrays. I am making a card game

  • 0

Ignore the fact that I using raw pointers/arrays.

I am making a card game in C++ that has a Player abstract class that is extended by other classes. I need to make an array of pointers to these derived classes. This works, but is there a better way?

class Player{
   public:
        Player();
        Player(const Player&);
        Player & operator=(const Player &);

        virtual void sort() = 0;
        virtual Card play(Pile*) = 0;

        void confirmPlay();

        void giveHand(Hand&);
        void giveCard(Card);
        bool hasCards();

        void won();
        void resetWins();
        int getWins();

        virtual ~Player();
    protected:
        int cardsLeft;
        Hand hand;
        int cardPlayed;
        int wins;
    private:
};

class DumbPlayer : public Player
{
    public:
        DumbPlayer();
        Card play(Pile*);
        void sort();
        virtual ~DumbPlayer();
        DumbPlayer & operator=(const DumbPlayer &);
        DumbPlayer(const DumbPlayer&);
    protected:
    private:
};

class Game{
    public:
        Game(int,  Player**&);
        void playX(int);
        void playOne();
        virtual ~Game();
    protected:
    private:
        bool done();
        Game & operator=(const Game &);
        Game(const Game&);
        Pile * piles;
        Deck deck;
        int playerCount;
        Player ** players;
};

//implementation not shown to save space, tell me if you would like to see anything.
//I think that all of the other class/function names should be good enough to not show.

Game::Game(const int pplayerCount, Player**& pplayers) : 
                                   piles(new Pile[4]), 
                                   playerCount(pplayerCount), 
                                   deck(), 
                                   players(new Player*[playerCount]){
    for(int i = 0; i < 4; i++){
        piles[i].setSuit(i);
    }

    for(int i = 0; i < playerCount; i++){
        players[i] = pplayers[i];
    }
}

void Game::playOne(){
    deck.shuffle();  //shuffle deck
    for(int i = 0; i < 4; i++){
        piles[i].reset();  //reset piles
    }
    Hand hands[playerCount];  //create hands
    Hand leftovers;
    deck.dealAll(playerCount, hands, leftovers); //deal the deck
    int cardsLeftover = leftovers.getSize();     //there are leftover cards, 
                                                 //52/3 has a remainder
    for(int playerIdx = 0; playerIdx < playerCount; playerIdx++){
        (*players[playerIdx]).giveHand(hands[playerIdx]); //this is what 
                                                          //i am unsure about.
        (*players[playerIdx]).sort();
    }
    int winner = -1;
    while(!done()){
        for(int playerIdx = 0; playerIdx < playerCount; playerIdx++){
            Card play = (*players[playerIdx]).play(piles);
            if(piles[play.getSuit()].canPut(play)){
                (*players[playerIdx]).confirmPlay();
                piles[play.getSuit()].put(play);
                if(!(*players[playerIdx]).hasCards()){
                    winner = playerIdx;
                    break;
                }
            } else {
                if(cardsLeftover > 0){
                    (*players[playerIdx]).giveCard(leftovers.popCard(--cardsLeftover));
                }
            }
        }
        if(winner != -1){
            (*players[winner]).won();
            break;
        }
    }
}

I know that it is a ton of code (for this site)… i am unsure about the game constructor/class and the lines including (*players[i]).x()

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T10:55:21+00:00Added an answer on May 27, 2026 at 10:55 am

    Nowadays, an array of pointers is spelled:

    std::vector<boost::shared_ptr<Player> > players;
    

    This takes full of the Resource Acquisition Is Initialization (RAII) idiom to ensure destructors are called properly and memory is reclaimed in the face of exceptions and unforeseen code paths.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

How can I get v2 of protobuf-net to ignore the fact that my class
Okay I'm using XHTML1.1 due to the fact that HTML5...doesn't behave as I expect,
How can I ignore directories or folders in Git using msysgit on Windows?
I'm using a framework called Processing which is basically a Java applet. It has
So, I am working on a Linux based command line utility, that has to
I can't seem to execute SQL that creates a database using a DbCommand object.
I am having issues attempting to replicate a chart that has been generated in
Is the Strategy Pattern based largely on the fact that there will be change
I'm creating an object with: NSMenu *appMainMenu = [[NSMenu alloc] initWithTitle:@MyApp]; (Ignore the fact
I have some text content (read in from the HTML using jQuery) that looks

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.