64 lines
1.6 KiB
C++
64 lines
1.6 KiB
C++
#include "Bag.h"
|
|
|
|
#include "../Pieces/Piece.h"
|
|
#include "PiecesList.h"
|
|
|
|
#include <vector>
|
|
#include <utility>
|
|
#include <cstdlib>
|
|
|
|
|
|
Bag::Bag(std::shared_ptr<PiecesList> piecesList) :
|
|
piecesList(piecesList) {
|
|
|
|
// initialize bags
|
|
this->currentBag = this->piecesList->getSelectedPieces();
|
|
this->nextBag.clear();
|
|
|
|
// prepare first piece
|
|
this->prepareNext();
|
|
}
|
|
|
|
void Bag::jumpToNextBag() {
|
|
// if the bag is empty switch to the next bag
|
|
if (this->currentBag.empty()) {
|
|
std::swap(this->currentBag, this->nextBag);
|
|
}
|
|
|
|
// get the already used pieces back to the current bag
|
|
for (const std::pair<int, int>& pieceIndex : this->nextBag) {
|
|
this->currentBag.emplace_back(pieceIndex);
|
|
}
|
|
this->nextBag.clear();
|
|
}
|
|
|
|
Piece Bag::lookNext() {
|
|
return this->piecesList->getPiece(this->next);
|
|
}
|
|
|
|
Piece Bag::getNext() {
|
|
// get the piece to return
|
|
std::pair<int, int> nextIndex = this->next;
|
|
|
|
// prepare the piece even after the next
|
|
this->prepareNext();
|
|
|
|
// return the next piece
|
|
return this->piecesList->getPiece(nextIndex);
|
|
}
|
|
|
|
void Bag::prepareNext() {
|
|
// if the bag is empty switch to the next bag
|
|
if (this->currentBag.empty()) {
|
|
std::swap(this->currentBag, this->nextBag);
|
|
}
|
|
|
|
// pick a random piece from the current bag
|
|
int indexIndex = std::rand() % this->currentBag.size();
|
|
this->next = this->currentBag.at(indexIndex);
|
|
|
|
// move the piece over to the next bag
|
|
this->nextBag.push_back(this->next);
|
|
this->currentBag.erase(this->currentBag.begin() + indexIndex);
|
|
}
|