2

I am just trying to learn C++ OOP by making a game where I have an Entity class and a Board class, both included in main. How do i write a method in Entity class that can access the Board private member, specifically the tiles near it? I want to write the function feed() that gives energy to an instance of Entity depending on how many tiles near it are free.

Entity.h

class Entity{
    int x;
    int y;
    char type;
    double energy;
public:
    void eat();
... * it has all the constructoros and other stuff but its not relevant
}

Board.h

#include "Entity.h"

class Board{
    Entity matrix[20][70];
public:
    void printBoard();
    Entity getEntity(int i, int j);

... * other functions and contructors. not relevant
}


main.cpp

#include <iostream>
#include "Entity.h"
#include "Board.h"

int main(){
Board* board = new Board();

// here I initialize the board with random entities

}

How do I write a function in Entity that can get the tiles near it and check them? I can't figure out a way to interact with other classes from Entity. Note: It can take arguments. its not mandatory to be exactly void feed();

Iustin
  • 70
  • 5
  • You mentioned "tile" but it's not defined in the questoin. Do you want to feed an Entity (0, 0) and expect Entity(0,1) will get engery as well? Requirement is important to the implementation. Also does the index of `matrix` is the x, y of an `Entity`? – Louis Go Mar 17 '23 at 09:39

3 Answers3

3

It is a matter of design. You have basically two choices:

  1. the Board is the object that contains the logic of the neighborhood,
  2. or, an Entity knows its neighbors.

In the first case you need to have a back pointer to the board from any entity:

class Entity; // forward declaration
class Board {
    Entity *matrix;
    Entity *neighborOf(Entity *);
    
};

class Entity {
    Board *theBoard;
    Entity *myNeighbor() { theBoard->neighBorOf(this); }
};

In the second case, at construction entities are linked to their neighbors:

class Entity;
class Board {
    Entity *matrix;
    
};
class Entity {
    Entity *neighbor;
    Entity *myNeighbor() { return neighbor; }
};

The first case is prefered (IMHO) as entities can then be reused easily in other board systems.

Jean-Baptiste Yunès
  • 34,548
  • 4
  • 48
  • 69
0

There must be a role (or class) should have access to the position of every entity. For OOP, i suggest to add Entity::isNeighbor(int x, int y) or Entity::isNeighbor(const Entity& other) and let Board does the iteration.

class Entity{
    int x;
    int y;
    char type;
    double energy;
public:
    void eat();

    bool isNeighbor(const Entity& other) const {
        // Yes, different instance in of the same class 
        //   may access each other's private members!
        // Refer to https://stackoverflow.com/q/6921185/4123703
        return (x == other.x) && (y == other.y);
    }
//... 
}

Note it might cause O(N^2) for a overall lookup. You may want to sort them beforehand or use some data structure like graph or adjacency matrix. In the future you might want to study algorithm like Nearest Neighbor.

Louis Go
  • 2,213
  • 2
  • 16
  • 29
0

You could have a reference to the board within the entity class which you set in a different function e.g.

class Entity{
    int x;
    int y;
    char type;
    double energy;
    shared_ptr<Board> board;
public:
    void setBoard(shared_ptr<Board> board) {
        this->board = board;
    }
    void eat();
... * it has all the constructoros and other stuff but its not relevant
}
Richard Bamford
  • 1,835
  • 3
  • 15
  • 19