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() {
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() {

View File

@@ -5,58 +5,109 @@
#include <vector>
#include <utility>
#include <memory>
PiecesList::PiecesList() {
this->maximumLoadedSize = 0;
this->loadedPieces.clear();
this->loadedPieces.push_back(std::vector<Piece>());
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<int> convexPieces;
std::vector<int> holelessPieces;
std::vector<int> otherPieces;
for (int i = this->highestLoadedSize + 1; i <= size; i++) {
for (int i = this->maximumLoadedSize + 1; i <= size; i++) {
std::vector<Piece> 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<int, int>(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<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() {
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<int, int> pieceIndex) {
return this->loadedPieces.at(pieceIndex.first).at(pieceIndex.second);
}
std::shared_ptr<Piece> PiecesList::getPiece(int size, int number) {
if (size < 1 || size > this->maximumLoadedSize || number >= this->loadedPieces.at(size).size()) return nullptr;
return std::make_shared<Piece>(this->loadedPieces.at(size).at(number));
void PiecesList::pushBackEmptyVectors() {
this->loadedPieces.push_back(std::vector<Piece>());
this->convexPieces.push_back(std::vector<int>());
this->holelessPieces.push_back(std::vector<int>());
this->otherPieces.push_back(std::vector<int>());
}

View File

@@ -4,25 +4,91 @@
#include <vector>
#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 {
private:
int maximumLoadedSize;
std::vector<std::vector<Piece>> loadedPieces;
std::vector<std::pair<int, int>> selectedPieces;
int highestLoadedSize; // the highest size of pieces currently loaded
std::vector<std::vector<Piece>> loadedPieces; // every loaded pieces by size
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:
/**
* 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<std::pair<int, int>> getSelectedPieces();
int getNumberOfPiecesOfOneSize(int size);
std::shared_ptr<Piece> getPiece(int size, int number);
/**
* @return The piece corresponding to the specified index
*/
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) {
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;
}