Probably a vector problem

I'm not sure what is wrong with my vector or my functions,
but at line 30 in GameCapsule.cpp I suspect the function call to SetElementOnField() is going wrong.

The basic problem is that when I look at what the Person[i]'s 'x' and 'y' position and compare it to what PrintBattleField() shows they don't correspond.

ex: (an 'X' is a Person and an 'O' is open space)

Person 1 
x: 1 y: 0

Person 2
x: 3 y: 1

OOOO0
X0000
00000
0X000

The x and y values seem to be switched in the output.


So heres what the output should look like:
Person 1 
x: 1 y: 0

Person 2
x: 3 y: 1

OXOO0
000X0
00000
00000



All the includes and headers guards are there in my files but I excluded them from this snippet.
Here is what I believe to be all of the relevant code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
//GameCapsule.cpp

void GameCapsule::PrintBattleField()
{
    for (int i = 0; i<this->gameBattleField.GetDimension('x'); i++)
    {
        for (int j = 0; j<this->gameBattleField.GetDimension('y'); j++)
        {
            std::cout << this->gameBattleField.GetElementOnField(i,j);
        }
        std::cout << std::endl;
    }
    std::cout << std::endl;
}

  void GameCapsule::GenerateRandomCharacterPosition()
{
    int x;
    int y;
    for(int i = 0; i < numberOfCharacters; i++)
    {
        do
        {
        x = RNG(0,gameBattleField.GetDimension('x'));
        y = RNG(0,gameBattleField.GetDimension('y'));
        }while(gameBattleField.GetElementOnField(x,y) != 'O'); 

        Person[i].SetPosition('y', y);
        Person[i].SetPosition('x', x); 
        gameBattleField.SetElementOnField(x,y,Person[i].GetCharSymbol());
    }
}


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
//Battlefield.cpp

void Battlefield::SetDimension(int x, int y)
{
    this->xDimension = x;
    this->yDimension = y;
    field.resize(x, std::vector<char>(y,'O')); // Changes the vector size to x,y and fills with 'O'
}

short Battlefield::GetDimension(char dimension)
{
    if (dimension == 'x')
    {return this->xDimension;}
    else if (dimension == 'y')
    {return this->yDimension;}
}

char Battlefield::GetElementOnField(int x, int y)
{
    return this->field[x][y];
}

void Battlefield::SetElementOnField(int x, int y, char symbol)
{
    field[x][y] = symbol;
}


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
//Battlefield.h

class Battlefield
{
    public:
        Battlefield();
        void SetDimension(int x ,int y);
        short GetDimension(char dimension);
        char GetElementOnField(int x, int y);
        void SetElementOnField(int x, int y, char symbol);
    private:
        std::vector < std::vector<char> > field; //2d vector
        short xDimension;
        short yDimension;
};

Sorry for my sloppy structuring.

RNG(minimum,maximum) takes a minimum number and a maximum number and returns a random number from the boundaries.

gameBattleField is a member of GameCapsule and a object of BattleField.

Please let me know if you need additional information.
Last edited on
I don't know if this would make a difference or not

but you have this line of code like this

1
2
        Person[i].SetPosition('y', y);
        Person[i].SetPosition('x', x); 


Maybe try with the .setposition('x', x); first?

I don't know how setposition() works but that's the only spot that stands out to my eyes as being backwards.
Person is an array of Character objects

So just this:

1
2
3
4
5
6
7
void Character::SetPosition(char coordinate, int position)
{
    if (coordinate == 'x')
    {this->xPosition = position;}
    if (coordinate == 'y')
    {this->yPosition = position;}
}
I figured out what the problem was, it was a matter of a switching of my x and y earlier in my code that pretty much snow balled upon itself, causing me to switch other x's and y's to get the code to work.
Topic archived. No new replies allowed.