mis en place la PR de simon sur les couleurs

This commit is contained in:
2025-02-28 18:42:04 +01:00
parent f0f391ade6
commit 26f501f7e8
29 changed files with 713 additions and 665 deletions

View File

@@ -2,13 +2,25 @@
#include "../Pieces/Piece.h"
#include <Vector>
#include <Set>
#include <vector>
#include <set>
#include <iostream>
Board::Board(int width, int height) : width(width), height(height) {
std::vector<Color> emptyRow;
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;
for (int i = 0; i < width; i ++) {
emptyRow.push_back(NOTHING);
}
@@ -20,13 +32,13 @@ Board::Board(int width, int height) : width(width), height(height) {
}
}
void Board::addBlock(const Cell& position, Color block) {
void Board::addBlock(const Position& position, Block block) {
// if the block is out of bounds we discard it
if (position.x < 0 || position.x >= this->width || position.y < 0) return;
// resize the grid if needed
if (position.y >= this->grid.size()) {
std::vector<Color> emptyRow;
std::vector<Block> emptyRow;
for (int i = 0; i < width; i ++) {
emptyRow.push_back(NOTHING);
}
@@ -35,29 +47,26 @@ void Board::addBlock(const Cell& position, Color block) {
}
}
// change the block in the grid
this->grid.at(position.y).at(position.x) = block;
}
int Board::clearRows() {
std::vector<Color> emptyRow;
std::vector<Block> emptyRow;
for (int i = 0; i < width; i ++) {
emptyRow.push_back(NOTHING);
}
// check from top to bottom
// 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--) {
// check if a line has a block on every column
bool isFull = true;
bool lineIsFull = true;
for (int i = 0; i < this->width; i++) {
if (this->grid.at(j).at(i) == NOTHING) {
isFull = false;
lineIsFull = false;
}
}
// if it has, erase it and add a new row at the top
if (isFull) {
if (lineIsFull) {
this->grid.erase(this->grid.begin() + j);
if(this->grid.size() < height) this->grid.push_back(emptyRow);
clearedLines++;
@@ -67,18 +76,15 @@ int Board::clearRows() {
return clearedLines;
}
Color Board::getBlock(const Cell& position) const {
// if the block is out of bounds
if (position.x < 0 || position.x >= this->width || position.y < 0) return OUT_OF_BOUND;
Block Board::getBlock(const Position& position) const {
if (position.x < 0 || position.x >= this->width || position.y < 0) return OUT_OF_BOUNDS;
// if the block is higher than the current grid, since it can grow indefinitely we do as if it was there but empty
if (position.y >= this->grid.size()) return NOTHING;
// else get the color in the grid
return this->grid.at(position.y).at(position.x);
}
std::vector<std::vector<Color>> Board::getBlocks() const {
std::vector<std::vector<Block>> Board::getBlocks() const {
return this->grid;
}
@@ -95,11 +101,10 @@ int Board::getWidth() const {
}
std::ostream& operator<<(std::ostream& os, const Board& board) {
// print the board
for (int y = board.grid.size() - 1; y >= 0; y--) {
for (int x = 0; x < board.width; x++) {
Color block = board.grid.at(y).at(x);
os << COLOR_CODES[block];
Block block = board.grid.at(y).at(x);
os << getConsoleColorCode(block);
if (block != NOTHING) {
os << "*";
}
@@ -110,8 +115,7 @@ std::ostream& operator<<(std::ostream& os, const Board& board) {
os << std::endl;
}
// reset console color
os << COLOR_RESET;
os << getResetConsoleColorCode();
return os;
}