From a14931e8666cfbf40c7a731c0f1954728c582f33 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Timoth=C3=A9e=20Leclaire-Fournier?= Date: Wed, 7 Feb 2024 17:51:02 -0500 Subject: [PATCH] Repare les allocations de memoire --- GPA675Lab1GOL/GOLTeamH.cpp | 24 ++++++++++-------------- GPA675Lab1GOL/GOLTeamH.h | 3 +-- 2 files changed, 11 insertions(+), 16 deletions(-) diff --git a/GPA675Lab1GOL/GOLTeamH.cpp b/GPA675Lab1GOL/GOLTeamH.cpp index da535a8..ec5bd3b 100644 --- a/GPA675Lab1GOL/GOLTeamH.cpp +++ b/GPA675Lab1GOL/GOLTeamH.cpp @@ -324,7 +324,7 @@ bool GOLTeamH::setFromPattern(std::string const& pattern, int centerX, int cente if (!sq.has_value()) return false; - fillDataFromPattern(pattern, sq.value(), centerX+1, centerY+1); + fillDataFromPattern(sq.value(), centerX + 1, centerY + 1); mIteration = 0; countLifeStatusCells(); @@ -349,11 +349,7 @@ bool GOLTeamH::setFromPattern(std::string const& pattern) if (!sq.has_value()) return false; - // Offset pour ajuster le centrage du pattern - int centerX = (mData.width() - sq.value().width)/2; - int centerY = (mData.height() - sq.value().height)/2; - - fillDataFromPattern(pattern, sq.value(),centerX+1, centerY+1); + fillDataFromPattern(sq.value(), (mData.width() / 2) + 1, (mData.height() / 2) + 1); mIteration = 0; countLifeStatusCells(); @@ -553,19 +549,19 @@ std::optional GOLTeamH::parsePattern(std::string const& p return std::nullopt; } -void GOLTeamH::fillDataFromPattern(std::string const& pattern, sizeQueried& sq, - int centerX, int centerY) +void GOLTeamH::fillDataFromPattern(sizeQueried& sq, int centerX, int centerY) { mData.fill(State::dead, true); + // Remplissage de la grille aux positions spécifiées par le patron for (size_t y = 0; y < sq.height; ++y) { for (size_t x = 0; x < sq.width; ++x) { // Coordonnées dans la grille de destination - int destX = centerX + x; - int destY = centerY + y; - + int destX = (centerX - ((sq.width + 1) / 2)) + x; + int destY = (centerY - ((sq.height + 1) / 2)) + y; + // Vérifier si les coordonnées sont dans la grille - if (destX >= 0 || destX < mData.width() || destY >= 0 || destY < mData.height()) { + if (destX > 0 && destX <= mData.width() && destY > 0 && destY <= mData.height()) { // On remplit la grille avec les valeurs du pattern State cellState = (sq.pos[(y * sq.width) + x] == '0') ? State::dead : State::alive; mData.setAt(destX, destY, cellState); @@ -573,10 +569,10 @@ void GOLTeamH::fillDataFromPattern(std::string const& pattern, sizeQueried& sq, else { continue; } - + } } - + } void GOLTeamH::countLifeStatusCells() diff --git a/GPA675Lab1GOL/GOLTeamH.h b/GPA675Lab1GOL/GOLTeamH.h index 6597e2a..338287c 100644 --- a/GPA675Lab1GOL/GOLTeamH.h +++ b/GPA675Lab1GOL/GOLTeamH.h @@ -94,8 +94,7 @@ private: // Fonctions utilisées à l'interne. unsigned char convertCharToNumber(const char c); std::optional parsePattern(std::string const& pattern); - void fillDataFromPattern(std::string const& pattern, sizeQueried& sq, - int centerX, int centerY); + void fillDataFromPattern(sizeQueried& sq, int centerX, int centerY); void countLifeStatusCells(); // Fonction qui modifie le border selon la règle