Commit 882a4c3d authored by Anthony Noir's avatar Anthony Noir
Browse files

ajout de Simulation et accesseurs

parent ed9c113c
......@@ -12,22 +12,29 @@ Cette classe représente un automate cellulaire.
#include "neighborhoodrule.hpp"
#include "transitionrule.hpp"
#include "grid.h"
#include "alphabet.hpp"
class Automaton {
private:
Alphabet alphabet;
Grid grid;
NeighborhoodRule* neighbourhoodRule;
TransitionRule* transitionRule;
Grid grid;
public:
Automaton(NeighborhoodRule*, TransitionRule*, const Grid& = Grid(0,0));
Automaton();
virtual ~Automaton();
//! \brief Fait avancer l'automate à l'état suivant
void runOnce();
void setAlphabet(const Alphabet&);
void setNeighborhoodRule(NeighborhoodRule*);
NeighborhoodRule* getNeighborhoodRule() const;
void setTransitionRule(TransitionRule*);
TransitionRule* getTransitionRule() const;
void setCell(const Coord&, unsigned int);
const Grid& getGrid() const;
void setGrid(const Grid&);
const Grid& getGrid() const {return grid;}
void setGrid(const Grid& g) {grid = g;}
void runOnce();
};
#endif // AUTOMATON_HPP
......@@ -8,6 +8,7 @@
#include <QFileDialog>
#include "property.hpp"
#include "simulation.hpp"
class Structure;
......@@ -66,5 +67,6 @@ private slots:
private:
Ui::MainWindow *ui;
Simulation simulation;
};
#endif // MAINWINDOW_HPP
......@@ -34,6 +34,7 @@ struct NeighborhoodFormat
class NeighborhoodRule
{
public:
virtual ~NeighborhoodRule() = default;
//! \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'
virtual Neighborhood getNeighborhood(const Grid& grid, Coord pos) const = 0;
......
......@@ -15,6 +15,7 @@ Cette classe représente un automate cellulaire dans le temps.
class Simulation {
private:
bool canRun;
int time;
Automaton automaton;
Grid startGrid;
......@@ -23,7 +24,14 @@ private:
public:
Simulation();
virtual ~Simulation();
void setAutomaton(const Automaton&);
void setNeighborhoodRule(NeighborhoodRule*);
void setTransitionRule(TransitionRule*);
void setAlphabet(const Alphabet&);
const Grid& getGrid() const;
void setCell(const Coord&, unsigned int);
void resize(size_t,size_t);
void reset();
void step();
void back();
......
#include "automaton.hpp"
Automaton::Automaton(NeighborhoodRule* n, TransitionRule* t, const Grid& g) : neighbourhoodRule(n), transitionRule(t), grid(Grid(g)) {}
Automaton::Automaton() : alphabet(), grid(0,0), neighbourhoodRule(nullptr), transitionRule(nullptr) {}
Automaton::~Automaton() {
delete neighbourhoodRule;
delete transitionRule;
}
void Automaton::runOnce() {
Grid tempGrid(grid);
for(int i=0; i<grid.get_rows(); ++i) {
for(int j=0; j<grid.get_col(); ++j) {
tempGrid.set_cell({i, j}, transitionRule->getState(grid.get_state({i,j}),neighbourhoodRule->getNeighborhood(grid, {i, j})));
void Automaton::setAlphabet(const Alphabet& A) {
alphabet = A;
for(int i=0; i<static_cast<int>(grid.get_rows()); ++i) {
for(int j=0; j<static_cast<int>(grid.get_col()); ++j) {
grid.set_cell({i,j}, 0);
}
}
neighbourhoodRule->step();
for(int i=0; i<grid.get_rows(); ++i) {
for(int j=0; j<grid.get_col(); ++j) {
grid = tempGrid;
}
void Automaton::setNeighborhoodRule(NeighborhoodRule* NR) {
delete neighbourhoodRule;
neighbourhoodRule = NR;
}
NeighborhoodRule* Automaton::getNeighborhoodRule() const {
return neighbourhoodRule;
}
void Automaton::setTransitionRule(TransitionRule* TR) {
delete transitionRule;
transitionRule = TR;
}
TransitionRule* Automaton::getTransitionRule() const {
return transitionRule;
}
void Automaton::setCell(const Coord& coord, unsigned int val) {
grid.set_cell(coord, val%alphabet.taille());
}
const Grid& Automaton::getGrid() const {
return grid;
}
void Automaton::setGrid(const Grid& G) {
grid = G;
unsigned int state;
for(int i=0; i<static_cast<int>(grid.get_rows()); ++i) {
for(int j=0; j<static_cast<int>(grid.get_col()); ++j) {
state = grid.get_state({i,j});
if(state >= alphabet.taille()) {
grid.set_cell({i,j}, state%alphabet.taille());
}
}
}
}
void Automaton::runOnce() {
Grid tempGrid(grid);
for(int i=0; i<static_cast<int>(grid.get_rows()); ++i) {
for(int j=0; j<static_cast<int>(grid.get_col()); ++j) {
tempGrid.set_cell({i, j}, transitionRule->getState(grid.get_state({i,j}),neighbourhoodRule->getNeighborhood(grid, {i, j}))%alphabet.taille());
}
}
grid = tempGrid;
}
......@@ -10,6 +10,7 @@
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
, simulation()
{
ui->setupUi(this);
......
#include "simulation.hpp"
Simulation::Simulation() : automaton(nullptr, nullptr), hist(10), startGrid(0,0), time(0)
{
//ctor
Simulation::Simulation() : canRun(false), time(0), automaton(), startGrid(0,0), hist(10) {}
Simulation::~Simulation() {}
void Simulation::setNeighborhoodRule(NeighborhoodRule* NR) {
canRun = false;
automaton.setNeighborhoodRule(NR);
}
Simulation::~Simulation()
{
//dtor
void Simulation::setTransitionRule(TransitionRule* TR) {
canRun = false;
automaton.setTransitionRule(TR);
}
void Simulation::setAutomaton(const Automaton& A) {
automaton = A;
startGrid = automaton.getGrid();
time = 0;
hist = History(10);
void Simulation::setAlphabet(const Alphabet& A) {
automaton.setAlphabet(A);
for(int i=0; i<static_cast<int>(startGrid.get_rows()); ++i) {
for(int j=0; j<static_cast<int>(startGrid.get_col()); ++j) {
startGrid.set_cell({i,j}, 0);
}
}
}
const Grid& Simulation::getGrid() const {
return automaton.getGrid();
}
void Simulation::setCell(const Coord& coord, unsigned int val) {
automaton.setCell(coord, val);
startGrid.set_cell(coord, val);
}
void Simulation::resize(size_t l,size_t c) {
Grid newGrid(l, c);
int min = static_cast<int>(startGrid.get_rows()<=l?startGrid.get_rows():l);
int max = static_cast<int>(startGrid.get_col()<=c?startGrid.get_col():c);
for(int i=0; i<min; ++i) {
for(int j=0; j<max; ++j) {
newGrid.set_cell({i,j}, startGrid.get_state({i,j}));
}
}
startGrid = newGrid;
reset();
}
void Simulation::reset() {
......@@ -26,11 +56,15 @@ void Simulation::reset() {
void Simulation::step() {
hist.pushGrid(automaton.getGrid());
automaton.runOnce();
automaton.getNeighborhoodRule()->step();
++time;
}
void Simulation::back() {
automaton.setGrid(hist.topGrid());
hist.popGrid();
--time;
if(hist.isEmpty()) {
automaton.setGrid(hist.topGrid());
//automaton.getNeighborhoodRule()->back();
hist.popGrid();
--time;
}
}
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment