initial commit

This commit is contained in:
2025-02-25 12:07:16 +01:00
commit 0657bc9b25
34 changed files with 3069 additions and 0 deletions

117
src/Core/Board.cpp Normal file
View File

@@ -0,0 +1,117 @@
#include "Board.h"
#include "../Pieces/Piece.h"
#include <Vector>
#include <Set>
#include <iostream>
Board::Board(int width, int height) : width(width), height(height) {
std::vector<Color> emptyRow;
for (int i = 0; i < width; i ++) {
emptyRow.push_back(NOTHING);
}
// initialize grid
this->grid.clear();
for (int j = 0; j < height; j++) {
this->grid.push_back(emptyRow);
}
}
void Board::addBlock(const Cell& position, Color 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;
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);
}
}
// change the block in the grid
this->grid.at(position.y).at(position.x) = block;
}
int Board::clearRows() {
std::vector<Color> emptyRow;
for (int i = 0; i < width; i ++) {
emptyRow.push_back(NOTHING);
}
// check from top to bottom
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;
for (int i = 0; i < this->width; i++) {
if (this->grid.at(j).at(i) == NOTHING) {
isFull = false;
}
}
// if it has, erase it and add a new row at the top
if (isFull) {
this->grid.erase(this->grid.begin() + j);
if(this->grid.size() < height) this->grid.push_back(emptyRow);
clearedLines++;
}
}
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;
// 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 {
return this->grid;
}
int Board::getGridHeight() const {
return this->grid.size();
}
int Board::getBaseHeight() const {
return this->height;
}
int Board::getWidth() const {
return this->width;
}
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];
if (block != NOTHING) {
os << "*";
}
else {
os << "-";
}
}
os << std::endl;
}
// reset console color
os << COLOR_RESET;
return os;
}