Skip to content
Snippets Groups Projects
Commit 404c2d5b authored by Yann Boucher's avatar Yann Boucher
Browse files

Ajout de Margolus

parent fd479439
Branches
No related tags found
No related merge requests found
Pipeline #79630 passed
/**
\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
#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;
}
......@@ -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 \
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment