From 404c2d5b3fb5f9ce665b6a01b00d47aa698f73e1 Mon Sep 17 00:00:00 2001 From: yboucher <yann.boucher@etu.utc.fr> Date: Fri, 11 Jun 2021 16:03:27 +0200 Subject: [PATCH] Ajout de Margolus --- .../margolusNeighborhoodRule.hpp | 39 ++++++++++++++++++ .../margolusNeighborhoodRule.cpp | 40 +++++++++++++++++++ src/src.pro | 2 + 3 files changed, 81 insertions(+) create mode 100644 include/neighborhood_rules/margolusNeighborhoodRule.hpp create mode 100644 src/neighborhood_rules/margolusNeighborhoodRule.cpp diff --git a/include/neighborhood_rules/margolusNeighborhoodRule.hpp b/include/neighborhood_rules/margolusNeighborhoodRule.hpp new file mode 100644 index 0000000..c25ce97 --- /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 0000000..06d5eee --- /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 85e2b3d..c33c558 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 \ -- GitLab