I need to track the position of objects (Boxes) in a grid for a candycrush-like game I am developing.
The easiest way I found to keep track of their positions in the grid would be to add them to a 2d vector, move them inside the vector according to the game logic. That is easy.
The hard part, and where I’m actually stuck is to get their current position on the grid at any given time, or in this case, the position inside the 2d vector. In short, if I had and easy way to periodically check the position of my Box object inside its 2d array, that would solve the problem.
I've been messing around with std:find and std: count, but I am a bit of a newbie and I’m having a hard time. Same of the code below:
#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
struct position
{
int x;
int y;
};
class Box
{
public:
position currentPosition;
};
int main(int argc, const char * argv[])
{
Box box1 = Box();
Box box2 = Box();
Box box3 = Box();
Box box4 = Box();
Box box5 = Box();
Box box6 = Box();
Box box7 = Box();
Box box8 = Box();
Box box9 = Box();
// vector<vector<Box>> boxes2dVector;
//
vector<vector<Box>> boxes2dVector =
{
{box1, box2,box3},
{box4, box5,box6},
{box7, box8,box9}
};
return 0;
}
position getPositionOfBox (Box box, vector<vector<Box>> vct)
{
position resultPositon;
/*
This is what I need...some code that finds the position in the of a Box object in side a 2d array
*/
return resultPositon;
};
Any ideas or help, anyone?
Many thanks!