Commit 5b6763f5 authored by Eugene Pin's avatar Eugene Pin
Browse files

Changement de notation de coordonnée dans Grid.h (int i, int j vers Coord pos)...

Changement de notation de coordonnée dans Grid.h (int i, int j vers Coord pos) et modification UI (lineEdit vers SpinBox)
parent 3e74229c
Pipeline #77953 passed with stages
in 18 seconds
......@@ -258,19 +258,6 @@ pattern recorded :</string>
</property>
</widget>
</item>
<item row="0" column="1">
<widget class="QLineEdit" name="widthLineEdit">
<property name="sizePolicy">
<sizepolicy hsizetype="Fixed" vsizetype="Fixed">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="text">
<string>5</string>
</property>
</widget>
</item>
<item row="1" column="0">
<widget class="QLabel" name="heightLabel">
<property name="text">
......@@ -278,19 +265,6 @@ pattern recorded :</string>
</property>
</widget>
</item>
<item row="1" column="1">
<widget class="QLineEdit" name="heightLineEdit">
<property name="sizePolicy">
<sizepolicy hsizetype="Fixed" vsizetype="Fixed">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="text">
<string>5</string>
</property>
</widget>
</item>
<item row="2" column="1">
<widget class="QPushButton" name="validateGridDim">
<property name="enabled">
......@@ -301,6 +275,32 @@ pattern recorded :</string>
</property>
</widget>
</item>
<item row="1" column="1">
<widget class="QSpinBox" name="heightSpinBox">
<property name="minimum">
<number>1</number>
</property>
<property name="maximum">
<number>9999</number>
</property>
<property name="value">
<number>10</number>
</property>
</widget>
</item>
<item row="0" column="1">
<widget class="QSpinBox" name="widthSpinBox">
<property name="minimum">
<number>1</number>
</property>
<property name="maximum">
<number>9999</number>
</property>
<property name="value">
<number>10</number>
</property>
</widget>
</item>
</layout>
</item>
<item>
......@@ -522,9 +522,9 @@ pattern recorded :</string>
</widget>
</item>
<item row="2" column="2">
<widget class="QLineEdit" name="radiusLineEdit">
<property name="text">
<string>1</string>
<widget class="QSpinBox" name="radiusSpinBox">
<property name="minimum">
<number>1</number>
</property>
</widget>
</item>
......
......@@ -34,13 +34,17 @@ public:
size_t get_rows() const {return nb_rows;}
size_t get_col() const{return nb_col;}
void set_cell(int i, int j,unsigned int state)
void set_cell(Coord pos, unsigned int state)
{
int i = pos.x;
int j = pos.y;
matrix[((i%nb_rows + nb_rows)%nb_rows)*nb_col
+((j%nb_col+nb_col)%nb_col)]
=state;
}
unsigned int get_state(int i, int j)const{
unsigned int get_state(Coord pos)const{
int i = pos.x;
int j = pos.y;
return matrix[((i%nb_rows + nb_rows)%nb_rows)*nb_col
+((j%nb_col+nb_col)%nb_col)];
}
......
......@@ -37,19 +37,16 @@ private slots:
//! \brief Envoie un signal lors du changement de voisinage. Lorsque Custom est sélectionné, le bouton "Custom Neighborhood" est actif
void on_neighborhoodComboBox_currentTextChanged(const QString &arg1);
//! \brief Active le bouton pour valider la dimension de la grille lors d'une modification dans le Line Edit
void on_heightLineEdit_textChanged(const QString &arg1);
//! \brief Active le bouton pour valider la dimension de la grille lors d'une modification dans le Line Edit
void on_widthLineEdit_textChanged(const QString &arg1);
//! \brief Envoie le signal que la dimension de la grille a été modifié
void on_validateGridDim_clicked();
//! \brief Active le bouton pour valider la dimension de la grille lors d'une modification dans le Line Edit
void on_widthSpinBox_valueChanged(int arg1);
//! \brief Active le bouton pour valider la dimension de la grille lors d'une modification dans le Line Edit
void on_heightSpinBox_valueChanged(int arg1);
private:
QValidator* intValidator;
Ui::MainWindow *ui;
};
#endif // MAINWINDOW_HPP
......@@ -20,7 +20,9 @@ Neighborhood ArbitraryNeighborhoodRule::getNeighborhood(const Grid &grid, Coord
for (const auto& item : neighbors.contents)
{
CoordinateProperty coord = static_cast<CoordinateProperty&>(*item);
n.addNeighbor(coord.c + pos, grid.get_state(coord.c.y, coord.c.x));
// n.addNeighbor(coord.c + pos, grid.get_state(coord.c.y, coord.c.x));
Coord newPos = {coord.c.y, coord.c.x};
n.addNeighbor(coord.c + pos, grid.get_state(newPos));
}
return n;
......
......@@ -21,7 +21,10 @@ ostream& operator<<(ostream& f, const Grid& g){
if(g.get_col() != 0 && g.get_rows() != 0){
for(size_t i=0;i<g.get_rows();i++){
for(size_t j=0;j<g.get_col();j++){
f<<" "<<g.get_state(i,j);
Coord pos;
pos.x = i;
pos.y = j;
f<<" "<<g.get_state(pos);
}
f<<std::endl;
}
......@@ -48,8 +51,12 @@ Structure Grid::to_structure() const
{
std::vector<std::pair<Coord, unsigned>> data;
for (size_t i = 0; i < get_rows(); ++i)
for (size_t j = 0; j < get_col(); ++j)
data.push_back(std::make_pair(Coord{(int)j, (int)i}, get_state(i, j)));
for (size_t j = 0; j < get_col(); ++j) {
Coord pos;
pos.x = i;
pos.y = j;
data.push_back(std::make_pair(Coord{(int)j, (int)i}, get_state(pos)));
}
return Structure{data.begin(), data.end()};
......
......@@ -171,8 +171,10 @@ Grid GridView::get_grid() const
{
const GridItem* item = item_at(Coord{(int)i, (int)j});
assert(item);
grid.set_cell(i, j, item->cell_state);
Coord pos;
pos.x = i;
pos.y = j;
grid.set_cell(pos, item->cell_state);
}
}
......
......@@ -6,12 +6,6 @@ MainWindow::MainWindow(QWidget *parent)
, ui(new Ui::MainWindow)
{
ui->setupUi(this);
// Validator pour entrer la taille de la grille et le rayon
intValidator = new QIntValidator(1, 1000, this);
ui->heightLineEdit->setValidator(intValidator);
ui->widthLineEdit->setValidator(intValidator);
ui->radiusLineEdit->setValidator(intValidator);
}
MainWindow::~MainWindow()
......@@ -64,20 +58,19 @@ void MainWindow::on_neighborhoodComboBox_currentTextChanged(const QString &arg1)
}
}
void MainWindow::on_heightLineEdit_textChanged(const QString &arg1)
void MainWindow::on_widthSpinBox_valueChanged(int arg1)
{
ui->validateGridDim->setEnabled(true);
}
void MainWindow::on_widthLineEdit_textChanged(const QString &arg1)
void MainWindow::on_heightSpinBox_valueChanged(int arg1)
{
ui->validateGridDim->setEnabled(true);
}
void MainWindow::on_validateGridDim_clicked()
{
// Appliquer la dimension à la grille
ui->validateGridDim->setEnabled(false);
}
......@@ -27,7 +27,7 @@ Neighborhood mooreNeighborhoodRule::getNeighborhood(const Grid& grid, Coord pos)
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.x, gridCoord.y));
newNeighborhood.addNeighbor(*it , grid.get_state(gridCoord));
}
return newNeighborhood;
}
......
......@@ -30,7 +30,7 @@ Neighborhood vonNeumannNeighborhoodRule::getNeighborhood(const Grid& grid, Coord
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.x, gridCoord.y));
newNeighborhood.addNeighbor(*it , grid.get_state(gridCoord));
}
return newNeighborhood;
}
......
......@@ -7,13 +7,16 @@ void CellulutTests::test_grid()
{
Grid grid(7,12);
// Test méthode "Set cell" et "Get state"
grid.set_cell(4,4,2);
grid.set_cell(1,1,10);
grid.set_cell(1,2,1);
unsigned int state = grid.get_state(4, 4);
Coord pos1 = {4,4};
grid.set_cell(pos1,2);
Coord pos2 = {1,1};
grid.set_cell(pos1,10);
Coord pos3 = {1,2};
grid.set_cell(pos3,1);
unsigned int state = grid.get_state(pos1);
QVERIFY(state == 2);
QVERIFY(grid.get_state(1, 1) == 10);
QVERIFY(grid.get_state(1, 2) == 1);
QVERIFY(grid.get_state(pos2) == 10);
QVERIFY(grid.get_state(pos3) == 1);
// Test operator=
Grid grid2(5,6);
......@@ -23,7 +26,7 @@ void CellulutTests::test_grid()
grid2 = grid;
QVERIFY(grid2.get_rows() == 7);
QVERIFY(grid2.get_col() == 12);
QVERIFY(grid2.get_state(4,4) == 2);
QVERIFY(grid2.get_state(1, 1) == 10);
QVERIFY(grid2.get_state(1, 2) == 1);
QVERIFY(grid2.get_state(pos1) == 2);
QVERIFY(grid2.get_state(pos2) == 10);
QVERIFY(grid2.get_state(pos3) == 1);
}
......@@ -13,9 +13,12 @@ void CellulutTests::test_history()
Grid gridTest1(10,10);
Grid gridTest2(10,10);
Grid gridTest3(10,10);
gridTest1.set_cell(2,2,2);
gridTest2.set_cell(3,3,3);
gridTest3.set_cell(4,4,4);
Coord pos1 {2 ,2};
Coord pos2 {3 ,3};
Coord pos3 {4 ,4};
gridTest1.set_cell(pos1,2);
gridTest2.set_cell(pos2,3);
gridTest3.set_cell(pos3,4);
// Push de 2 grilles
historyTest.pushGrid(gridTest1);
......@@ -24,12 +27,12 @@ void CellulutTests::test_history()
// On pop la première grille puis la deuxième
Grid popGrid = historyTest.popGrid();
QCOMPARE(popGrid.get_state(3,3), 3);
QCOMPARE(popGrid.get_state(pos2), 3);
Grid topGrid = historyTest.topGrid();
QVERIFY(topGrid.get_state(2,2) == 2);
QVERIFY(topGrid.get_state(pos1) == 2);
popGrid = historyTest.popGrid();
QCOMPARE(popGrid.get_state(2,2), 2);
QCOMPARE(popGrid.get_state(pos1), 2);
QVERIFY(historyTest.isEmpty() == true);
// Push de trois grille (taille max de la pile de l'history = 2)
......@@ -38,11 +41,11 @@ void CellulutTests::test_history()
historyTest.pushGrid(gridTest3); // La première grille est supprimée
popGrid = historyTest.popGrid();
QCOMPARE(popGrid.get_state(4,4), 4);
QCOMPARE(popGrid.get_state(pos3), 4);
topGrid = historyTest.topGrid();
QVERIFY(topGrid.get_state(3,3) == 3);
QVERIFY(topGrid.get_state(pos2) == 3);
popGrid = historyTest.popGrid();
QCOMPARE(popGrid.get_state(3,3), 3);
QCOMPARE(popGrid.get_state(pos2), 3);
QVERIFY(historyTest.isEmpty() == true);
}
Markdown is supported
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