Originally Posted by
Imanuel
Thank you. I am still wrapping my mind around class design and have yet to read books on c++ related to good design. Grid won't be the same as Actor and it will not be a templated class either as generics are not needed here.
Thank you again for pulling my head out of the clouds.
I must simply be way out of my league to think I know c++ enough to program this simple project. I posted my code below and I appreciate being notified about my severe shortcomings and gaps in knowledge.
Code:
#include<cstdlib>
#include <memory>
#include "grid.h"
using namespace std;
int main(int argc, char *argv[]){
Bug bug(1, 3);
Ant ant(1, 2);
std::shared_ptr<Actor> pbug(new Bug(1, 3));
std::shared_ptr<Actor> pant(new Ant(1, 2));
Grid grid;
grid.populate(pbug, 1, 3);
grid.populate(pant, 1, 2);
grid.move(pbug);
return EXIT_SUCCESS;
}
#ifndef GRID_H_
#define GRID_H_
#include<vector>
#include<memory>
#include<random>
#include<ctime>
#include "bug.h"
#include "ant.h"
class Grid{
std::vector<std::vector<std::shared_ptr<Actor>>> cell_grid;
std::vector<std::pair<int, int>> *used_indices;
bool move_north(std::shared_ptr<Actor> &object){
// Move north
if(object->get_y_position() - 1 >= 0 &&
cell_grid[object->get_x_position()][object->get_y_position() - 1] == nullptr){
north_helper(object);
return true;
}else if (typeid(Ant) ==
typeid(cell_grid[object->get_x_position()][object->get_y_position() - 1])){
cell_grid[object->get_x_position()][object->get_y_position() - 1].reset();
north_helper(object);
return true;
}else{
return false;
}
}
void north_helper( std::shared_ptr<Actor> & object )
{
object->set_y_position(object->get_y_position() - 1);
cell_grid[object->get_x_position()][object->get_y_position()] = object;
cell_grid[object->get_x_position()][object->get_y_position() + 1].reset();
cell_grid[object->get_x_position()][object->get_y_position() + 1] = nullptr;
}
bool move_east(std::shared_ptr<Actor> &object){
// Move east
if(object->get_x_position() + 1 <= 19 &&
cell_grid[object->get_x_position() + 1][object->get_y_position()] == nullptr){
east_helper(object);
return true;
}else if (typeid(Ant) ==
typeid(cell_grid[object->get_x_position() + 1][object->get_y_position()])){
cell_grid[object->get_x_position() + 1][object->get_y_position()].reset();
east_helper(object);
return true;
}else{
return false;
}
}
void east_helper( std::shared_ptr<Actor> & object )
{
object->set_x_position(object->get_x_position() + 1);
cell_grid[object->get_x_position()][object->get_y_position()] = object;
cell_grid[object->get_x_position() - 1][object->get_y_position()] = nullptr;
}
bool move_south(std::shared_ptr<Actor> &object){
// Move south
if(object->get_y_position() + 1 <= 19 &&
cell_grid[object->get_x_position()][object->get_y_position() + 1] == nullptr){
south_helper(object);
return true;
}else if(typeid(Ant) ==
typeid(cell_grid[object->get_x_position()][object->get_y_position() + 1] )) {
cell_grid[object->get_x_position()][object->get_y_position() + 1].reset();
south_helper(object);
return true;
}else{
return false;
}
}
void south_helper( std::shared_ptr<Actor> & object )
{
object->set_y_position(object->get_y_position() + 1);
cell_grid[object->get_x_position()][object->get_y_position()] = object;
cell_grid[object->get_x_position()][object->get_y_position() - 1] = nullptr;
}
bool move_west(std::shared_ptr<Actor> &object){
// Move west
if(object->get_x_position() - 1 >= 0 &&
cell_grid[object->get_x_position() - 1][object->get_y_position()] == nullptr){
west(object);
return true;
}else if(typeid(Ant) ==
typeid(cell_grid[object->get_x_position() - 1][object->get_y_position()])){
cell_grid[object->get_x_position() - 1][object->get_y_position()].reset();
west(object);
return true;
}else{
return false;
}
}
void west( std::shared_ptr<Actor> & object )
{
object->set_x_position(object->get_x_position() - 1);
cell_grid[object->get_x_position()][object->get_y_position()] = object;
cell_grid[object->get_x_position() + 1][object->get_y_position()] = nullptr;
}
bool random_move_north(std::shared_ptr<Actor> &object){
if(move_north(object)){
return true;
}else if (move_east(object)){
return true;
}else if(move_south(object)){
return true;
}else if (move_west(object)){
return true;
}else{
return false;
}
}
bool random_move_east(std::shared_ptr<Actor> &object){
if(move_east(object)){
return true;
}else if (move_south(object)){
return true;
}else if(move_west(object)){
return true;
}else if (move_north(object)){
return true;
}else{
return false;
}
}
bool random_move_south(std::shared_ptr<Actor> &object){
if(move_south(object)){
return true;
}else if (move_west(object)){
return true;
}else if(move_north(object)){
return true;
}else if (move_east(object)){
return true;
}else{
return false;
}
}
bool random_move_west(std::shared_ptr<Actor> &object){
if(move_west(object)){
return true;
}else if (move_north(object)){
return true;
}else if(move_east(object)){
return true;
}else if (move_south(object)){
return true;
}else{
return false;
}
}
public:
Grid() {
cell_grid = std::vector<std::vector<std::shared_ptr<Actor>>>
(20, std::vector<std::shared_ptr<Actor>>(20, nullptr));
used_indices = new std::vector<std::pair<int, int>> (10);
}
virtual ~Grid() {}
void populate(std::shared_ptr<Actor> &object, const int &x_pos, const int &y_pos){
cell_grid[x_pos][y_pos] = object;
}
bool move(std::shared_ptr<Actor> &object){
return random_move_north(object);
/*
srand((unsigned)time(NULL));
default_random_engine engine(rand() % 1000);
uniform_int_distribution<unsigned> distro(0, 3);
switch(distro(engine)){
case 0: return random_move_north(object);
case 1: return random_move_east(object);
case 2: return random_move_south(object);
case 3: return random_move_west(object);
default:
return false;
}
*/
}
};
#endif
#ifndef ACTOR_H_
#define ACTOR_H_
class Actor{
int x_position, y_position;
public:
virtual int get_y_position() const;
virtual void set_y_position(int val);
virtual int get_x_position() const;
virtual void set_x_position(int val);
};
#endif
#include "actor.h"
#include<memory>
class Bug : public Actor{
int x_position, y_position;
public:
Bug() : x_position(0), y_position(0) {}
Bug(const int &x_pos, const int &y_pos){
this->x_position = x_pos;
this->y_position = y_pos;
}
~Bug() {}
int get_y_position() const override { return y_position; }
void set_y_position(int val) override { y_position = val; }
int get_x_position() const override { return x_position; }
void set_x_position(int val) override { x_position = val; }
};
#endif
#ifndef ANT_H_
#define ANT_H_
#include "actor.h"
#include<memory>
class Ant : public Actor{
int x_position, y_position;
public:
Ant() : x_position(0), y_position(0) {}
Ant(const int &x_pos, const int &y_pos){
this->x_position = x_pos;
this->y_position = y_pos;
}
~Ant() {}
int get_y_position() const override { return y_position; }
void set_y_position(int val) override { y_position = val; }
int get_x_position() const override { return x_position; }
void set_x_position(int val) override { x_position = val; }
};
#endif