début de la classe PiecesFile

This commit is contained in:
2025-02-28 22:32:51 +01:00
parent 26f501f7e8
commit 13ee43167e
11 changed files with 143 additions and 45 deletions

View File

@@ -1,19 +1,18 @@
#include "Bag.h"
#include "../Pieces/Piece.h"
#include "PiecesList.h"
#include <vector>
#include <utility>
#include <cstdlib>
Bag::Bag(const std::vector<Piece>& pieces) :
pieces(pieces) {
Bag::Bag(std::shared_ptr<PiecesList> 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<Piece>& 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<int, int> 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() {

View File

@@ -1,8 +1,11 @@
#pragma once
#include "../Pieces/Piece.h"
#include "PiecesList.h"
#include <vector>
#include <memory>
#include <utility>
/**
@@ -10,16 +13,17 @@
*/
class Bag {
private:
std::vector<Piece> pieces; // the pieces the bag can dispense
int next; // the next piece to give
std::vector<int> currentBag; // the list of pieces that are still to be taken out before starting a new bag
std::vector<int> 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> piecesList; // the list of loaded pieces
std::vector<std::pair<int, int>> selectedPieces; // the list of pieces that can be given to the player
std::pair<int, int> next; // the next piece to give
std::vector<std::pair<int, int>> currentBag; // the list of pieces that are still to be taken out before starting a new bag
std::vector<std::pair<int, int>> 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<Piece>& pieces);
Bag(std::shared_ptr<PiecesList> piecesList);
/**
* Looks at what the next picked piece will be, without removing it from the bag

View File

@@ -7,28 +7,19 @@
#include <iostream>
static constexpr std::vector<Block> getEmptyRow(int width) {
std::vector<Block> 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<Block> emptyRow;
this->emptyRow = std::vector<Block>(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<Block> 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<Block> 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++;
}
}

View File

@@ -12,6 +12,7 @@
class Board {
private:
std::vector<std::vector<Block>> grid; // the grid, (0,0) is downleft
std::vector<Block> 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

View File

@@ -4,8 +4,9 @@
#include "GameParameters.h"
#include "Action.h"
#include <vector>
#include <set>
#include <algorithm>
#include <memory>
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<Piece>& bag) :
Game::Game(Gamemode gamemode, const Player& controls, int boardWidth, int boardHeight, const std::shared_ptr<PiecesList>& bag) :
parameters(gamemode, controls),
board(boardWidth, boardHeight, bag, parameters.getNextQueueLength()) {

View File

@@ -5,6 +5,7 @@
#include "Action.h"
#include <vector>
#include <memory>
/**
@@ -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<Piece>& bag);
Game(Gamemode gamemode, const Player& controls, int boardWidth, int boardHeight, const std::shared_ptr<PiecesList>& bag);
/**
* Starts the game

View File

@@ -10,9 +10,9 @@
#include <memory>
GameBoard::GameBoard(int boardWidth, int boardHeight, const std::vector<Piece>& bag, int nextQueueLength) :
GameBoard::GameBoard(int boardWidth, int boardHeight, const std::shared_ptr<PiecesList>& pieceList, int nextQueueLength) :
board(boardWidth, boardHeight),
generator(bag),
generator(pieceList),
nextQueueLength(nextQueueLength) {
this->nextQueue.clear();

View File

@@ -27,7 +27,7 @@ class GameBoard {
/**
* Creates a new board, generator, and next queue
*/
GameBoard(int boardWidth, int boardHeight, const std::vector<Piece>& bag, int nextQueueLength);
GameBoard(int boardWidth, int boardHeight, const std::shared_ptr<PiecesList>& bag, int nextQueueLength);
/**
* Tries moving the piece one position to the left

62
src/Core/PiecesList.cpp Normal file
View File

@@ -0,0 +1,62 @@
#include "PiecesList.h"
#include "../Pieces/Piece.h"
#include "../Pieces/PiecesFiles.h"
#include <vector>
#include <utility>
#include <memory>
PiecesList::PiecesList() {
this->maximumLoadedSize = 0;
this->loadedPieces.clear();
this->loadedPieces.push_back(std::vector<Piece>());
this->selectedPieces.clear();
}
bool PiecesList::loadPieces(int size) {
if (size < 1) return false;
PiecesFiles piecesFiles;
std::vector<int> convexPieces;
std::vector<int> holelessPieces;
std::vector<int> otherPieces;
for (int i = this->maximumLoadedSize + 1; i <= size; i++) {
std::vector<Piece> 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<int, int>(size, number));
return true;
}
std::vector<std::pair<int, int>> 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<Piece> PiecesList::getPiece(int size, int number) {
if (size < 1 || size > this->maximumLoadedSize || number >= this->loadedPieces.at(size).size()) return nullptr;
return std::make_shared<Piece>(this->loadedPieces.at(size).at(number));
}

28
src/Core/PiecesList.h Normal file
View File

@@ -0,0 +1,28 @@
#pragma once
#include "../Pieces/Piece.h"
#include <vector>
#include <utility>
#include <memory>
class PiecesList {
private:
int maximumLoadedSize;
std::vector<std::vector<Piece>> loadedPieces;
std::vector<std::pair<int, int>> selectedPieces;
public:
PiecesList();
bool loadPieces(int size);
bool selectPieces(int size, int numbers);
std::vector<std::pair<int, int>> getSelectedPieces();
int getNumberOfPiecesOfOneSize(int size);
std::shared_ptr<Piece> getPiece(int size, int number);
};

View File

@@ -1,6 +1,7 @@
#include "../Pieces/PiecesFiles.h"
#include "../Pieces/Generator.h"
#include "GameBoard.h"
#include "PiecesList.h"
#include <chrono>
#include <string>
@@ -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<PiecesList> pl = std::make_shared<PiecesList>(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;
}