diff --git a/src/Core/Bag.cpp b/src/Core/Bag.cpp index 48de2d9..066b03d 100644 --- a/src/Core/Bag.cpp +++ b/src/Core/Bag.cpp @@ -1,19 +1,18 @@ #include "Bag.h" #include "../Pieces/Piece.h" +#include "PiecesList.h" #include +#include #include -Bag::Bag(const std::vector& pieces) : - pieces(pieces) { +Bag::Bag(std::shared_ptr piecesList) : + piecesList(piecesList) { // initialize bags - this->currentBag.clear(); - for (int i = 0; i < this->pieces.size(); i++) { - this->currentBag.push_back(i); - } + this->currentBag = this->piecesList->getSelectedPieces(); this->nextBag.clear(); // prepare first piece @@ -21,18 +20,18 @@ Bag::Bag(const std::vector& pieces) : } Piece Bag::lookNext() { - return this->pieces.at(this->next); + return *this->piecesList->getPiece(this->next.first, this->next.second); } Piece Bag::getNext() { // get the piece to return - int nextIndex = this->next; + std::pair nextIndex = this->next; // prepare the piece even after the next this->prepareNext(); // return the next piece - return this->pieces.at(nextIndex); + return *this->piecesList->getPiece(nextIndex.first, nextIndex.second); } void Bag::prepareNext() { diff --git a/src/Core/Bag.h b/src/Core/Bag.h index 2d0047c..1d99b27 100644 --- a/src/Core/Bag.h +++ b/src/Core/Bag.h @@ -1,8 +1,11 @@ #pragma once #include "../Pieces/Piece.h" +#include "PiecesList.h" #include +#include +#include /** @@ -10,16 +13,17 @@ */ class Bag { private: - std::vector pieces; // the pieces the bag can dispense - int next; // the next piece to give - std::vector currentBag; // the list of pieces that are still to be taken out before starting a new bag - std::vector nextBag; // the list of pieces that have been taken out of the current bag and have been placed in the next + std::shared_ptr piecesList; // the list of loaded pieces + std::vector> selectedPieces; // the list of pieces that can be given to the player + std::pair next; // the next piece to give + std::vector> currentBag; // the list of pieces that are still to be taken out before starting a new bag + std::vector> nextBag; // the list of pieces that have been taken out of the current bag and have been placed in the next public: /** - * Creates a new bag of the specified list of pieces + * Creates a new bag with the pieces currently selected in the piece list */ - Bag(const std::vector& pieces); + Bag(std::shared_ptr piecesList); /** * Looks at what the next picked piece will be, without removing it from the bag diff --git a/src/Core/Board.cpp b/src/Core/Board.cpp index f03072c..0bdf7d6 100644 --- a/src/Core/Board.cpp +++ b/src/Core/Board.cpp @@ -7,28 +7,19 @@ #include -static constexpr std::vector getEmptyRow(int width) { - std::vector emptyRow; - for (int i = 0; i < width; i ++) { - emptyRow.push_back(NOTHING); - } - return emptyRow; -} - - Board::Board(int width, int height) : width(width), height(height) { - std::vector emptyRow; + this->emptyRow = std::vector(width); for (int i = 0; i < width; i ++) { - emptyRow.push_back(NOTHING); + this->emptyRow.push_back(NOTHING); } // initialize grid this->grid.clear(); for (int j = 0; j < height; j++) { - this->grid.push_back(emptyRow); + this->grid.push_back(this->emptyRow); } } @@ -38,12 +29,8 @@ void Board::addBlock(const Position& position, Block block) { // resize the grid if needed if (position.y >= this->grid.size()) { - std::vector emptyRow; - for (int i = 0; i < width; i ++) { - emptyRow.push_back(NOTHING); - } for (int j = this->grid.size(); j <= position.y; j++) { - this->grid.push_back(emptyRow); + this->grid.push_back(this->emptyRow); } } @@ -51,24 +38,23 @@ void Board::addBlock(const Position& position, Block block) { } int Board::clearRows() { - std::vector emptyRow; - for (int i = 0; i < width; i ++) { - emptyRow.push_back(NOTHING); - } - // check from top to bottom, so that erasing lines don't screw up the looping int clearedLines = 0; for (int j = this->grid.size() - 1; j >= 0; j--) { bool lineIsFull = true; - for (int i = 0; i < this->width; i++) { + int i = 0; + while (lineIsFull && (i < width)) { if (this->grid.at(j).at(i) == NOTHING) { lineIsFull = false; } + i++; } if (lineIsFull) { this->grid.erase(this->grid.begin() + j); - if(this->grid.size() < height) this->grid.push_back(emptyRow); + if(this->grid.size() < height) { + this->grid.push_back(this->emptyRow); + } clearedLines++; } } diff --git a/src/Core/Board.h b/src/Core/Board.h index 9b941df..763673a 100644 --- a/src/Core/Board.h +++ b/src/Core/Board.h @@ -12,6 +12,7 @@ class Board { private: std::vector> grid; // the grid, (0,0) is downleft + std::vector emptyRow; // an empty row of blocks int width; // the width of the grid int height; // the base height of the grid, which can extends indefinitely diff --git a/src/Core/Game.cpp b/src/Core/Game.cpp index 9bdbe5b..cf073b7 100644 --- a/src/Core/Game.cpp +++ b/src/Core/Game.cpp @@ -4,8 +4,9 @@ #include "GameParameters.h" #include "Action.h" -#include +#include #include +#include static const int SUBPX_PER_ROW = 60; // the number of position the active piece can take "between" two rows static const int SOFT_DROP_SCORE = 1; // the score gained by line soft dropped @@ -15,7 +16,7 @@ static const int B2B_SCORE_MULTIPLIER = 2; // by how much havaing B2B on mult static const int B2B_MIN_LINE_NUMBER = 4; // the minimum number of lines needed to be cleared at once to gain B2B (without a spin) -Game::Game(Gamemode gamemode, const Player& controls, int boardWidth, int boardHeight, const std::vector& bag) : +Game::Game(Gamemode gamemode, const Player& controls, int boardWidth, int boardHeight, const std::shared_ptr& bag) : parameters(gamemode, controls), board(boardWidth, boardHeight, bag, parameters.getNextQueueLength()) { diff --git a/src/Core/Game.h b/src/Core/Game.h index 216ec81..d45be1e 100644 --- a/src/Core/Game.h +++ b/src/Core/Game.h @@ -5,6 +5,7 @@ #include "Action.h" #include +#include /** @@ -32,7 +33,7 @@ class Game { /** * Initialize the parameters and creates a new board */ - Game(Gamemode gamemode, const Player& controls, int boardWidth, int boardHeight, const std::vector& bag); + Game(Gamemode gamemode, const Player& controls, int boardWidth, int boardHeight, const std::shared_ptr& bag); /** * Starts the game diff --git a/src/Core/GameBoard.cpp b/src/Core/GameBoard.cpp index a9b514d..b850c80 100644 --- a/src/Core/GameBoard.cpp +++ b/src/Core/GameBoard.cpp @@ -10,9 +10,9 @@ #include -GameBoard::GameBoard(int boardWidth, int boardHeight, const std::vector& bag, int nextQueueLength) : +GameBoard::GameBoard(int boardWidth, int boardHeight, const std::shared_ptr& pieceList, int nextQueueLength) : board(boardWidth, boardHeight), - generator(bag), + generator(pieceList), nextQueueLength(nextQueueLength) { this->nextQueue.clear(); diff --git a/src/Core/GameBoard.h b/src/Core/GameBoard.h index 37ed3b7..b8ba86e 100644 --- a/src/Core/GameBoard.h +++ b/src/Core/GameBoard.h @@ -27,7 +27,7 @@ class GameBoard { /** * Creates a new board, generator, and next queue */ - GameBoard(int boardWidth, int boardHeight, const std::vector& bag, int nextQueueLength); + GameBoard(int boardWidth, int boardHeight, const std::shared_ptr& bag, int nextQueueLength); /** * Tries moving the piece one position to the left diff --git a/src/Core/PiecesList.cpp b/src/Core/PiecesList.cpp new file mode 100644 index 0000000..b1feab9 --- /dev/null +++ b/src/Core/PiecesList.cpp @@ -0,0 +1,62 @@ +#include "PiecesList.h" + +#include "../Pieces/Piece.h" +#include "../Pieces/PiecesFiles.h" + +#include +#include +#include + + +PiecesList::PiecesList() { + this->maximumLoadedSize = 0; + this->loadedPieces.clear(); + this->loadedPieces.push_back(std::vector()); + this->selectedPieces.clear(); +} + +bool PiecesList::loadPieces(int size) { + if (size < 1) return false; + + PiecesFiles piecesFiles; + std::vector convexPieces; + std::vector holelessPieces; + std::vector otherPieces; + + for (int i = this->maximumLoadedSize + 1; i <= size; i++) { + std::vector pieces; + this->loadedPieces.push_back(pieces); + + if (!piecesFiles.loadPieces(i, this->loadedPieces.at(i), convexPieces, holelessPieces, otherPieces)) { + return false; + } + else { + this->maximumLoadedSize++; + } + } + + return true; +} + +bool PiecesList::selectPieces(int size, int number) { + if (size < 1 || size > this->maximumLoadedSize || number >= this->loadedPieces.at(size).size()) return false; + + this->selectedPieces.push_back(std::pair(size, number)); + return true; +} + +std::vector> PiecesList::getSelectedPieces() { + return this->selectedPieces; +} + +int PiecesList::getNumberOfPiecesOfOneSize(int size) { + if (size < 1 || size > this->maximumLoadedSize) return 0; + + return this->loadedPieces.at(size).size(); +} + +std::shared_ptr PiecesList::getPiece(int size, int number) { + if (size < 1 || size > this->maximumLoadedSize || number >= this->loadedPieces.at(size).size()) return nullptr; + + return std::make_shared(this->loadedPieces.at(size).at(number)); +} diff --git a/src/Core/PiecesList.h b/src/Core/PiecesList.h new file mode 100644 index 0000000..5c817b2 --- /dev/null +++ b/src/Core/PiecesList.h @@ -0,0 +1,28 @@ +#pragma once + +#include "../Pieces/Piece.h" + +#include +#include +#include + + +class PiecesList { + private: + int maximumLoadedSize; + std::vector> loadedPieces; + std::vector> selectedPieces; + + public: + PiecesList(); + + bool loadPieces(int size); + + bool selectPieces(int size, int numbers); + + std::vector> getSelectedPieces(); + + int getNumberOfPiecesOfOneSize(int size); + + std::shared_ptr getPiece(int size, int number); +}; diff --git a/src/Core/main.cpp b/src/Core/main.cpp index 655b85d..56d24d6 100644 --- a/src/Core/main.cpp +++ b/src/Core/main.cpp @@ -1,6 +1,7 @@ #include "../Pieces/PiecesFiles.h" #include "../Pieces/Generator.h" #include "GameBoard.h" +#include "PiecesList.h" #include #include @@ -20,6 +21,21 @@ void readStatsFromFilesForAllSizes(int amount); int main(int argc, char** argv) { std::srand(std::time(NULL)); + int sizeSelected = 3; + + PiecesList pli; + std::shared_ptr pl = std::make_shared(pli); + pl->loadPieces(sizeSelected); + for (int i = 0; i < pl->getNumberOfPiecesOfOneSize(sizeSelected); i++) { + pl->selectPieces(sizeSelected, i); + } + + GameBoard gb(10, 4, pl, 1); + + for (int i = 0; i < pl->getNumberOfPiecesOfOneSize(sizeSelected) * 3; i++) { + gb.spawnNextPiece(); + std::cout << gb << std::endl; + } return 0; }