51 lines
1.2 KiB
C++
51 lines
1.2 KiB
C++
#include "Bag.h"
|
|
|
|
#include "../Pieces/Piece.h"
|
|
|
|
#include <vector>
|
|
#include <cstdlib>
|
|
|
|
|
|
Bag::Bag(const std::vector<Piece>& pieces) : pieces(pieces) {
|
|
// initialize bags
|
|
this->currentBag.clear();
|
|
for (int i = 0; i < this->pieces.size(); i++) {
|
|
this->currentBag.push_back(i);
|
|
}
|
|
this->nextBag.clear();
|
|
|
|
// prepare first piece
|
|
this->prepareNext();
|
|
}
|
|
|
|
Piece Bag::lookNext() {
|
|
// return the next piece
|
|
return this->pieces.at(this->next);
|
|
}
|
|
|
|
Piece Bag::getNext() {
|
|
// get the piece to return
|
|
int nextIndex = this->next;
|
|
|
|
// prepare the piece even after the next
|
|
this->prepareNext();
|
|
|
|
// return the next piece
|
|
return this->pieces.at(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);
|
|
}
|