56 lines
1.2 KiB
C++
56 lines
1.2 KiB
C++
#include "Bag.h"
|
|
|
|
#include "../Pieces/Piece.h"
|
|
#include "PiecesList.h"
|
|
|
|
#include <vector>
|
|
#include <utility>
|
|
#include <cstdlib>
|
|
|
|
|
|
Bag::Bag(const std::shared_ptr<PiecesList>& piecesList) :
|
|
piecesList(piecesList) {
|
|
|
|
this->currentBag = this->piecesList->getSelectedPieces();
|
|
this->nextBag.clear();
|
|
|
|
this->prepareNext();
|
|
}
|
|
|
|
void Bag::jumpToNextBag() {
|
|
if (this->currentBag.size() < this->nextBag.size()) {
|
|
std::swap(this->currentBag, this->nextBag);
|
|
}
|
|
|
|
for (const std::pair<int, int>& pieceIndex : this->nextBag) {
|
|
this->currentBag.push_back(pieceIndex);
|
|
}
|
|
this->nextBag.clear();
|
|
|
|
this->prepareNext();
|
|
}
|
|
|
|
Piece Bag::lookNext() {
|
|
return this->piecesList->getPiece(this->next);
|
|
}
|
|
|
|
Piece Bag::getNext() {
|
|
std::pair<int, int> nextIndex = this->next;
|
|
|
|
this->prepareNext();
|
|
|
|
return this->piecesList->getPiece(nextIndex);
|
|
}
|
|
|
|
void Bag::prepareNext() {
|
|
if (this->currentBag.empty()) {
|
|
std::swap(this->currentBag, this->nextBag);
|
|
}
|
|
|
|
int indexIndex = std::rand() % this->currentBag.size();
|
|
this->next = this->currentBag.at(indexIndex);
|
|
|
|
this->nextBag.push_back(this->next);
|
|
this->currentBag.erase(this->currentBag.begin() + indexIndex);
|
|
}
|