diff --git a/include/neighborhood_rules/margolusNeighborhoodRule.hpp b/include/neighborhood_rules/margolusNeighborhoodRule.hpp new file mode 100644 index 0000000000000000000000000000000000000000..c25ce970d8f08ce84e7a303e26e6157a794c075f --- /dev/null +++ b/include/neighborhood_rules/margolusNeighborhoodRule.hpp @@ -0,0 +1,39 @@ +/** +\file vonNeumannNeighborhoodRule.hpp +\date 13/05/2021 +\author Eugene Pin +\version 1 +\brief vonNeumannNeighborhoodRule + +Contient la classe de la règle de Von Neumann. +**/ + +#ifndef MARGOLUSNEIGHBORHOODRULE_HPP +#define MARGOLUSNEIGHBORHOODRULE_HPP + +#include "neighborhoodrule.hpp" +#include "neighborhood.hpp" + +//! \brief Voisinage de Margolus. +class MargolusNeighborhoodRule : public NeighborhoodRule +{ +public: + MargolusNeighborhoodRule(); + + //! \brief Retourne l'ensemble des voisins d'une cellule de position 'pos' sur la grille 'grid' sous la forme d'une structure 'Neighborhood' + //! \return L'ensemble des voisins de la cellule à 'pos' + Neighborhood getNeighborhood(const Grid& grid, Coord pos) const; + + //! \brief Retourne 'format' de VonNeumann dans un vecteur (méthode dérivée) + //! \return Retourne les formats de voisinage possible dans un std::vector. + std::vector<NeighborhoodFormat> getFormats() const; + + virtual void update_generation(unsigned gen) + { parity = gen%2; } + +private: + std::vector<NeighborhoodFormat> formats; + unsigned parity; +}; + +#endif // MARGOLUSNEIGHBORHOODRULE_HPP diff --git a/src/neighborhood_rules/margolusNeighborhoodRule.cpp b/src/neighborhood_rules/margolusNeighborhoodRule.cpp new file mode 100644 index 0000000000000000000000000000000000000000..06d5eee3c1c501d74800e4e9172b3ef6dd3c4122 --- /dev/null +++ b/src/neighborhood_rules/margolusNeighborhoodRule.cpp @@ -0,0 +1,40 @@ +#include "margolusNeighborhoodRule.hpp" + +REGISTER_FACTORY_ENTRY(NeighborhoodRule, MargolusNeighborhoodRule, "Margolus"); + +MargolusNeighborhoodRule::MargolusNeighborhoodRule() : + parity(0) +{ + formats.resize(2); + + formats[0].positions.push_back({-1, -1}); + formats[0].positions.push_back({-1, 0}); + formats[0].positions.push_back({0, -1}); + + NeighborhoodFormat format_2; + formats[1].positions.push_back({+1, +1}); + formats[1].positions.push_back({+1, 0}); + formats[1].positions.push_back({0, +1}); +} + +Neighborhood MargolusNeighborhoodRule::getNeighborhood(const Grid& grid, Coord pos) const +{ + Neighborhood newNeighborhood; + // Coordonnées des voisins dans la grille + Coord gridCoord; + const NeighborhoodFormat& format = formats[parity]; + std::vector<Coord>::const_iterator it = format.positions.begin(); + for (it = format.positions.begin() ; it != format.positions.end(); ++it) { + // Calcul des coordonnées du voisin sur la grille + gridCoord.x = pos.x + it->x; + gridCoord.y = pos.y + it->y; + // Ajout du nouveau voisin <Coordonnée_relative, état> + newNeighborhood.addNeighbor(*it , grid.get_state(gridCoord)); + } + return newNeighborhood; +} + +std::vector<NeighborhoodFormat> MargolusNeighborhoodRule::getFormats() const +{ + return formats; +} diff --git a/src/src.pro b/src/src.pro index 85e2b3dad06903a987071b682c2229968377d76c..c33c558a7617b06c6b53b3b7cc08a32d94d11e34 100644 --- a/src/src.pro +++ b/src/src.pro @@ -25,6 +25,7 @@ SOURCES += \ alphabet.cpp \ mathexpr.cpp \ neighborhood_rules/arbitraryneighborhoodrule.cpp \ + neighborhood_rules/margolusNeighborhoodRule.cpp \ automaton.cpp \ gridview.cpp \ neighborhoodDialog.cpp \ @@ -55,6 +56,7 @@ HEADERS += \ ../include/automaton.hpp \ ../include/coord.hpp \ ../include/neighborhood_rules/mooreNeighborhoodRule.hpp \ + ../include/neighborhood_rules/margolusNeighborhoodRule.hpp \ ../include/savingdialog.hpp \ ../include/transition_rules/lifegametransition.h \ ../include/neighborhoodrule.hpp \