-1

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!

Jorge
  • 11
  • 2
  • Possible duplicate of [How to use std::find/std::find\_if with a vector of custom class objects?](https://stackoverflow.com/questions/6939129/how-to-use-stdfind-stdfind-if-with-a-vector-of-custom-class-objects) – Frank May 27 '19 at 16:54
  • A generally better approach to provide coordinate oriented matrixes (2D) like yours, is probably to wrap a simple `std::vector boxes2dVector(rows * cols)` into a separate class and manage this matrix using a puplic interface (including an `const operator()(size_t row, size_t col)` overload maybe). – πάντα ῥεῖ May 27 '19 at 17:08
  • @πάνταῥεῖῖ , many thanks. I agree that the solution I am looking for may not be the ideal one...would you be able to give me any tips or links on how to implement this? – Jorge May 27 '19 at 17:24

2 Answers2

0

You could try using an std::unordered_map where the key is the box and the value is it's position in the array. Otherwise, I'd recommend storing its coordinate in the box itself. Iterating through the entire array each time would give O(N) performance. As long as you update the std::unordered_map appropriately, I believe lookup would be O(1).

Michael Bianconi
  • 4,757
  • 1
  • 6
  • 20
  • Thanks Michael. I agree, storing the position on the box object itself would be the ideal solution. The problem is how to update that position when it moves inside the vector...it there any way to do that that I am not seeing? Probably there is but it's escaping me .. – Jorge May 27 '19 at 17:00
  • Somehow I douibt implementing a custom hash function is simpler than implementing an `operator ==` overload. – WhozCraig May 27 '19 at 17:01
0

I think what you are looking for is here : How to use std::find/std::find_if with a vector of custom class objects?

with that in place it's quite easy use this : How to get position of a certain element in strings vector, to use it as an index in ints vector?

Verthais
  • 139
  • 1
  • 10