fini PiecesList

This commit is contained in:
2025-03-01 18:24:09 +01:00
parent 13ee43167e
commit 66c2cf1013
4 changed files with 155 additions and 34 deletions

View File

@@ -20,7 +20,7 @@ Bag::Bag(std::shared_ptr<PiecesList> piecesList) :
} }
Piece Bag::lookNext() { Piece Bag::lookNext() {
return *this->piecesList->getPiece(this->next.first, this->next.second); return this->piecesList->getPiece(this->next);
} }
Piece Bag::getNext() { Piece Bag::getNext() {
@@ -31,7 +31,7 @@ Piece Bag::getNext() {
this->prepareNext(); this->prepareNext();
// return the next piece // return the next piece
return *this->piecesList->getPiece(nextIndex.first, nextIndex.second); return this->piecesList->getPiece(nextIndex);
} }
void Bag::prepareNext() { void Bag::prepareNext() {

View File

@@ -5,58 +5,109 @@
#include <vector> #include <vector>
#include <utility> #include <utility>
#include <memory>
PiecesList::PiecesList() { PiecesList::PiecesList() {
this->maximumLoadedSize = 0; this->highestLoadedSize = 0;
this->loadedPieces.clear();
this->loadedPieces.push_back(std::vector<Piece>());
this->selectedPieces.clear(); 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) { bool PiecesList::loadPieces(int size) {
if (size < 1) return false; if (size < 1) return false;
PiecesFiles piecesFiles; PiecesFiles piecesFiles;
std::vector<int> convexPieces; for (int i = this->highestLoadedSize + 1; i <= size; i++) {
std::vector<int> holelessPieces;
std::vector<int> otherPieces;
for (int i = this->maximumLoadedSize + 1; i <= size; i++) { if (!piecesFiles.loadPieces(i, this->loadedPieces.at(i), this->convexPieces.at(i), this->holelessPieces.at(i), this->otherPieces.at(i))) {
std::vector<Piece> pieces;
this->loadedPieces.push_back(pieces);
if (!piecesFiles.loadPieces(i, this->loadedPieces.at(i), convexPieces, holelessPieces, otherPieces)) {
return false; return false;
} }
else { else {
this->maximumLoadedSize++; this->highestLoadedSize++;
this->pushBackEmptyVectors();
} }
} }
return true; return true;
} }
bool PiecesList::selectPieces(int size, int number) { bool PiecesList::selectPiece(int size, int number) {
if (size < 1 || size > this->maximumLoadedSize || number >= this->loadedPieces.at(size).size()) return false; if (size < 1 || size > this->highestLoadedSize || number >= this->loadedPieces.at(size).size()) return false;
this->selectedPieces.push_back(std::pair<int, int>(size, number)); this->selectedPieces.push_back(std::pair<int, int>(size, number));
return true; 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<int, int>(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<int, int>(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<int, int>(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<int, int>(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<std::pair<int, int>> PiecesList::getSelectedPieces() { std::vector<std::pair<int, int>> PiecesList::getSelectedPieces() {
return this->selectedPieces; return this->selectedPieces;
} }
int PiecesList::getNumberOfPiecesOfOneSize(int size) { Piece PiecesList::getPiece(std::pair<int, int> pieceIndex) {
if (size < 1 || size > this->maximumLoadedSize) return 0; return this->loadedPieces.at(pieceIndex.first).at(pieceIndex.second);
return this->loadedPieces.at(size).size();
} }
std::shared_ptr<Piece> PiecesList::getPiece(int size, int number) { void PiecesList::pushBackEmptyVectors() {
if (size < 1 || size > this->maximumLoadedSize || number >= this->loadedPieces.at(size).size()) return nullptr; this->loadedPieces.push_back(std::vector<Piece>());
this->convexPieces.push_back(std::vector<int>());
return std::make_shared<Piece>(this->loadedPieces.at(size).at(number)); this->holelessPieces.push_back(std::vector<int>());
this->otherPieces.push_back(std::vector<int>());
} }

View File

@@ -4,25 +4,91 @@
#include <vector> #include <vector>
#include <utility> #include <utility>
#include <memory>
/**
* 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 { class PiecesList {
private: private:
int maximumLoadedSize; int highestLoadedSize; // the highest size of pieces currently loaded
std::vector<std::vector<Piece>> loadedPieces; std::vector<std::vector<Piece>> loadedPieces; // every loaded pieces by size
std::vector<std::pair<int, int>> selectedPieces; std::vector<std::vector<int>> convexPieces; // the list of convex loaded pieces by size
std::vector<std::vector<int>> holelessPieces; // the list of holeless loaded pieces by size
std::vector<std::vector<int>> otherPieces; // the list of other loaded pieces by size
std::vector<std::pair<int, int>> selectedPieces; // the list of all currently selected pieces
public: public:
/**
* Initializes a list of pieces up to size 0 (so no pieces)
*/
PiecesList(); PiecesList();
/**
* Makes the list load all pieces of the specified size
* @return If it sucessfully loaded the pieces
*/
bool loadPieces(int size); 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<std::pair<int, int>> getSelectedPieces(); std::vector<std::pair<int, int>> getSelectedPieces();
int getNumberOfPiecesOfOneSize(int size); /**
* @return The piece corresponding to the specified index
std::shared_ptr<Piece> getPiece(int size, int number); */
Piece getPiece(std::pair<int, int> pieceIndex);
private:
/**
* Adds empty vectors at the end of every pieces list
*/
void pushBackEmptyVectors();
}; };

View File

@@ -21,6 +21,7 @@ void readStatsFromFilesForAllSizes(int amount);
int main(int argc, char** argv) { int main(int argc, char** argv) {
std::srand(std::time(NULL)); std::srand(std::time(NULL));
/*
int sizeSelected = 3; int sizeSelected = 3;
PiecesList pli; PiecesList pli;
@@ -36,6 +37,9 @@ int main(int argc, char** argv) {
gb.spawnNextPiece(); gb.spawnNextPiece();
std::cout << gb << std::endl; std::cout << gb << std::endl;
} }
*/
testGeneratorForOneSize(11);
return 0; return 0;
} }