initial commit
This commit is contained in:
50
src/Core/Bag.cpp
Normal file
50
src/Core/Bag.cpp
Normal file
@@ -0,0 +1,50 @@
|
||||
#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);
|
||||
}
|
||||
Reference in New Issue
Block a user