0

I'm trying to learn how to use classes but I am having a hard time trying to understand it so I tried to create a game.

I want to access my 10 players in the heap and I want to initialize the skills of each 10 players. I really don't know what I'm doing please help me. If you think the structure of my program is garbage please tell me and tell me how to properly do it. Thanks

main.cpp

int main()
{
    Player *p = new Player[10];
    p->createPlayer(&p,10);
}

Header file

class Player
{
    public:
        Player();
        ~Player();

        int genRanNum(int);
        void createPlayer(Player *, int);
    private:
        int plyrSkill1,plyrSkill2,plyrSkill3;
        int plyrId;
};

CPP File

Player::Player()
{
}

int Player::genRanNum(int num)
{
    return 1+(rand()%num);
}

void Player::createPlayer(Player *p, int si)
{

    for(int i = 0; i < si; i++)
    {
        *p->staId = i;
        *p->staSkills1 = genRanNum(10);
        *p->staSkills2 = genRanNum(10);
        *p->staSkills3 = genRanNum(10);
    }
}
Jake lee
  • 61
  • 9
  • 1
    There's no relationship between `Player` and `Stafff`, and `*p->x` is equivalent to `*(p->x)`, not `(*p)->x`. You should look at getting a [book](http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list). – molbdnilo Oct 06 '15 at 05:52
  • sorry that was suppose to be player = new player not staff I tried to staff first then went to do a game ill edit it – Jake lee Oct 06 '15 at 05:58
  • 1
    Ask yourself what each part of your code does, and whether you want to be doing it. Because it makes very little sense. – juanchopanza Oct 06 '15 at 06:09
  • thank you for the reply I am currently updating my code I am using vector now which is very useful – Jake lee Oct 06 '15 at 06:40

2 Answers2

0

I'm trying to learn how to use classes but I am having a hard time trying to understand it so I tried to create a game.

Since you want to create a game, visualize all the characters and scenes or anything that you want.

Model your idea/view of the game..

Example:

game model

Once you have determined all the classes that you need and the relationships between them, you can now start coding them up in any language you want to.

I know this will take time but it is a better way to learn designing and these skills will be helpful in the long run.

basav
  • 1,475
  • 12
  • 20
0

The first line in main Player *p = new Player[10]; is creating an array of 10 players on the heap.

It seems that that you want to 'initialize' these 10 players with the second line p->createPlayer(&p,10); but the code is wrong :

The loop in createPlayer() function always works on the same player (it increments i but p is always the same).

try this :

class Player
{
    public:
        Player(); /// can be private if players can be instantiated only by createPlayer()
        ~Player();

        static int genRanNum(int);
        static Player* createPlayer(int);
    private:
        int plyrSkill1,plyrSkill2,plyrSkill3;
        int plyrId;
};

createPlayer() and getRanNum() are static because they are class method (it's not related to one instance in particular)

Player * Player::createPlayer(int si)
{
    Player *player = new Player();
    player->staId = si;
    player->staSkills1 = genRanNum(10);
    player->staSkills2 = genRanNum(10);
    player->staSkills3 = genRanNum(10);

     return player;   
}

Finally in main :

int main()
{
    std::vector<Player *> p(10); 
    for (int iPlayer=0; iPlayer<10; iPlayer++)
    {
       p[iPlayer] = Player::createPlayer(iPlayer);
    }

    ...
    /// Don't forget to delete players
}
M. Yousfi
  • 578
  • 5
  • 24