1

In main i have the following code:

Gameboard gameboard(8, 5);
HumanPlayer hp1('A'), hp2('B');
HumanPlayer players[2];
players[0] = hp1;
players[1] = hp2;
gameboard.show();
//int p = player
int input, p = 0;
while (cin >> input)
{
    switch (input)
    {
    case 1:
        if (players[p].setcoin(gameboard, input))
        .
        .
        .

In class HumanPlayer i call the function of gameboard and return it

bool HumanPlayer::setcoin(Gameboard g, int row)
{
    return g.setstone(name, row);
}

In class Gameboard i set the coin (if it is full i return false)

bool Gameboard::setstone(char player, int row) 
{
    for (int y = height; y >= 0; y--)
    {
        //row-1 da das array bei 0 beginnt
        if (elements[y][row-1] == '.')
        {
            elements[y][row-1] = player;
            return true;
        }
     }
     return false;
}
David Walser
  • 183
  • 20

1 Answers1

2

This function passes the Gameboard by making a copy:

bool HumanPlayer::setcoin(Gameboard g, int row)
{
    return g.setstone(name, row);
}

This means everything you do with g inside the function will not have effects to the original that you passed into it. At the end of the function the copy of g gets destroyed.

If you don't want to do this use:

bool HumanPlayer::setcoin(Gameboard& g, int row)
{
    return g.setstone(name, row);
}

The ampersand means that you want to pass a reference, which will not cause a copy to be made.

Perhaps this question might be useful to you.

Community
  • 1
  • 1
Beginner
  • 5,277
  • 6
  • 34
  • 71