Imagine you have a class Square in C++, which represents a square on the board. Two options are readily apparent for storage, assuming the grid itself is dense:
class Square
{
public:
Square()
{
// initialize `nearestNeighbors_`
}
const std::array<Square*, 4>& GetNearestNeighbors() const
{
return nearestNeighbors_;
}
private:
std::array<Square*, 4> nearestNeighbors_;
};
OR
class Square
{
public:
Square()
{
// initialize `nearestNeighbors_`
}
std::array<Square*, 4> GetNearestNeighbors() const
{
std::array<Square*, 4> nearestNeighbors = /* ... init .../;
return nearestNeighbors;
}
};
Either solution would work. The first consumes more memory; the second requires more calculations. Which would make more sense for a real game map?
New contributor
csc14us is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.