In a snake game, I have two classes defined in seperate files: Snake
and Map
(a game map, not the usual container).
This is a simplified Map
implementation:
#include "Snake.hpp"
using Coordinates = std::pair<std::size_t, std::size_t>;
class Map
{
public:
Map(Coordinates dimensions);
void generateFruit(const Snake &);
private:
friend class Snake;
const Coordinates m_dimensions;
std::unordered_set<Coordinates, boost::hash<Coordinates>> m_fruit_positions;
};
generateFruit
generates a fruit at a random position in the map, and it can't place a fruit where the snake exists, which means it needs to know where the snake is, hence the argument Snake
.
And this is a simplified Snake
implementation:
#include "Map.hpp"
class Snake
{
public:
enum class Orientation
{
Up,
Down,
Left,
Right
};
Snake(const Map &);
bool isAlive(const Map &) const;
void move(Map &);
void turn(Orientation);
private:
std::deque<Coordinates> s_snake;
Orientation s_orientation;
};
Most methods in Snake
require a Map
for bounds checking.
What's the best way to resolve this dependency?
EDIT: Forward declaring any class causes incomplete type errors.