From 66c2cf101381a15ebe782a20654cbcf8a3593efb Mon Sep 17 00:00:00 2001 From: zulianc Date: Sat, 1 Mar 2025 18:24:09 +0100 Subject: [PATCH] fini PiecesList --- src/Core/Bag.cpp | 4 +- src/Core/PiecesList.cpp | 97 +++++++++++++++++++++++++++++++---------- src/Core/PiecesList.h | 84 +++++++++++++++++++++++++++++++---- src/Core/main.cpp | 4 ++ 4 files changed, 155 insertions(+), 34 deletions(-) diff --git a/src/Core/Bag.cpp b/src/Core/Bag.cpp index 066b03d..d3fd993 100644 --- a/src/Core/Bag.cpp +++ b/src/Core/Bag.cpp @@ -20,7 +20,7 @@ Bag::Bag(std::shared_ptr piecesList) : } Piece Bag::lookNext() { - return *this->piecesList->getPiece(this->next.first, this->next.second); + return this->piecesList->getPiece(this->next); } Piece Bag::getNext() { @@ -31,7 +31,7 @@ Piece Bag::getNext() { this->prepareNext(); // return the next piece - return *this->piecesList->getPiece(nextIndex.first, nextIndex.second); + return this->piecesList->getPiece(nextIndex); } void Bag::prepareNext() { diff --git a/src/Core/PiecesList.cpp b/src/Core/PiecesList.cpp index b1feab9..059ebc8 100644 --- a/src/Core/PiecesList.cpp +++ b/src/Core/PiecesList.cpp @@ -5,58 +5,109 @@ #include #include -#include PiecesList::PiecesList() { - this->maximumLoadedSize = 0; - this->loadedPieces.clear(); - this->loadedPieces.push_back(std::vector()); + this->highestLoadedSize = 0; this->selectedPieces.clear(); + + // we need to have something at index 0 even if there is no pieces of size 0 + this->loadedPieces.clear(); + this->convexPieces.clear(); + this->holelessPieces.clear(); + this->otherPieces.clear(); + this->pushBackEmptyVectors(); + + // we always prepare vectors of the next size to be generated + this->pushBackEmptyVectors(); } bool PiecesList::loadPieces(int size) { if (size < 1) return false; PiecesFiles piecesFiles; - std::vector convexPieces; - std::vector holelessPieces; - std::vector otherPieces; + for (int i = this->highestLoadedSize + 1; i <= size; i++) { - for (int i = this->maximumLoadedSize + 1; i <= size; i++) { - std::vector pieces; - this->loadedPieces.push_back(pieces); - - if (!piecesFiles.loadPieces(i, this->loadedPieces.at(i), convexPieces, holelessPieces, otherPieces)) { + if (!piecesFiles.loadPieces(i, this->loadedPieces.at(i), this->convexPieces.at(i), this->holelessPieces.at(i), this->otherPieces.at(i))) { return false; } else { - this->maximumLoadedSize++; + this->highestLoadedSize++; + this->pushBackEmptyVectors(); } } return true; } -bool PiecesList::selectPieces(int size, int number) { - if (size < 1 || size > this->maximumLoadedSize || number >= this->loadedPieces.at(size).size()) return false; +bool PiecesList::selectPiece(int size, int number) { + if (size < 1 || size > this->highestLoadedSize || number >= this->loadedPieces.at(size).size()) return false; this->selectedPieces.push_back(std::pair(size, number)); return true; } +bool PiecesList::selectAllPieces(int size) { + if (size < 1 || size > this->highestLoadedSize) return false; + + for (int i = 0; i < this->loadedPieces.at(size).size(); i++) { + this->selectedPieces.push_back(std::pair(size, i)); + } + return true; +} + +bool PiecesList::selectConvexPieces(int size) { + if (size < 1 || size > this->highestLoadedSize) return false; + + for (int index : this->convexPieces.at(size)) { + this->selectedPieces.push_back(std::pair(size, index)); + } + return true; +} + +bool PiecesList::selectHolelessPieces(int size) { + if (size < 1 || size > this->highestLoadedSize) return false; + + for (int index : this->holelessPieces.at(size)) { + this->selectedPieces.push_back(std::pair(size, index)); + } + return true; +} + +bool PiecesList::selectOtherPieces(int size) { + if (size < 1 || size > this->highestLoadedSize) return false; + + for (int index : this->otherPieces.at(size)) { + this->selectedPieces.push_back(std::pair(size, index)); + } + return true; +} + +void PiecesList::unselectAll() { + this->selectedPieces.clear(); +} + +int PiecesList::getHighestLoadedSize() { + return this->highestLoadedSize; +} + +int PiecesList::getNumberOfPieces(int size) { + if (size < 1 || size > this->highestLoadedSize) return 0; + + return this->loadedPieces.at(size).size(); +} + std::vector> PiecesList::getSelectedPieces() { return this->selectedPieces; } -int PiecesList::getNumberOfPiecesOfOneSize(int size) { - if (size < 1 || size > this->maximumLoadedSize) return 0; - - return this->loadedPieces.at(size).size(); +Piece PiecesList::getPiece(std::pair pieceIndex) { + return this->loadedPieces.at(pieceIndex.first).at(pieceIndex.second); } -std::shared_ptr PiecesList::getPiece(int size, int number) { - if (size < 1 || size > this->maximumLoadedSize || number >= this->loadedPieces.at(size).size()) return nullptr; - - return std::make_shared(this->loadedPieces.at(size).at(number)); +void PiecesList::pushBackEmptyVectors() { + this->loadedPieces.push_back(std::vector()); + this->convexPieces.push_back(std::vector()); + this->holelessPieces.push_back(std::vector()); + this->otherPieces.push_back(std::vector()); } diff --git a/src/Core/PiecesList.h b/src/Core/PiecesList.h index 5c817b2..99f7328 100644 --- a/src/Core/PiecesList.h +++ b/src/Core/PiecesList.h @@ -4,25 +4,91 @@ #include #include -#include +/** + * A container for all loaded pieces to prevent loading and copying them multiple times, + * also allows for the player to select a list of pieces to be used in a game + */ class PiecesList { private: - int maximumLoadedSize; - std::vector> loadedPieces; - std::vector> selectedPieces; - + int highestLoadedSize; // the highest size of pieces currently loaded + std::vector> loadedPieces; // every loaded pieces by size + std::vector> convexPieces; // the list of convex loaded pieces by size + std::vector> holelessPieces; // the list of holeless loaded pieces by size + std::vector> otherPieces; // the list of other loaded pieces by size + std::vector> selectedPieces; // the list of all currently selected pieces + public: + /** + * Initializes a list of pieces up to size 0 (so no pieces) + */ PiecesList(); + /** + * Makes the list load all pieces of the specified size + * @return If it sucessfully loaded the pieces + */ bool loadPieces(int size); - bool selectPieces(int size, int numbers); + /** + * Selects the specified piece + * @return If the piece could be selected + */ + bool selectPiece(int size, int number); + /** + * Selects all pieces of the specified size + * @return If the pieces could be selected + */ + bool selectAllPieces(int size); + + /** + * Selects all convex pieces of the specified size + * @return If the pieces could be selected + */ + bool selectConvexPieces(int size); + + /** + * Selects all holeless pieces of the specified size + * @return If the pieces could be selected + */ + bool selectHolelessPieces(int size); + + /** + * Selects all other pieces of the specified size + * @return If the pieces could be selected + */ + bool selectOtherPieces(int size); + + /** + * Unselects all previously selected pieces + */ + void unselectAll(); + + /** + * @return The highest loaded size of pieces + */ + int getHighestLoadedSize(); + + /** + * @return The number of pieces of the specified size + */ + int getNumberOfPieces(int size); + + /** + * @return The indexes of all selected pieces + */ std::vector> getSelectedPieces(); - int getNumberOfPiecesOfOneSize(int size); - - std::shared_ptr getPiece(int size, int number); + /** + * @return The piece corresponding to the specified index + */ + Piece getPiece(std::pair pieceIndex); + + private: + /** + * Adds empty vectors at the end of every pieces list + */ + void pushBackEmptyVectors(); }; diff --git a/src/Core/main.cpp b/src/Core/main.cpp index 56d24d6..6f93e6c 100644 --- a/src/Core/main.cpp +++ b/src/Core/main.cpp @@ -21,6 +21,7 @@ void readStatsFromFilesForAllSizes(int amount); int main(int argc, char** argv) { std::srand(std::time(NULL)); + /* int sizeSelected = 3; PiecesList pli; @@ -36,6 +37,9 @@ int main(int argc, char** argv) { gb.spawnNextPiece(); std::cout << gb << std::endl; } + */ + + testGeneratorForOneSize(11); return 0; }