#include "Board.
h"
#include "Brick.h"
#include <vector>
namespace Tetris {
Board::Board(const unsigned &height, const unsigned &width , const bool
partially_filled) : height(height), width(width) {
board = std::vector<std::vector<Cell>>();
for (int i = 0; i < height; ++i) {
board.push_back(std::vector<Cell>(width)); //les element sont initialisés
par le constructeur par défaut de Cell
}
if(partially_filled) partiallyFill();
}
const bool Board::isPositionOccupied(const Position &pos) const{
if (pos.x() >= 0 && pos.x() < height && pos.y() >= 0 && pos.y() < width) {
return board[pos.x()][pos.y()].isOccupiedCell();
} else {
throw std::out_of_range("Position is outside the board bounds.");
}
}
const bool Board::isPositionCurrent(const Position &pos)const{
if (pos.x() >= 0 && pos.x() < height && pos.y() >= 0 && pos.y() < width) {
return board[pos.x()][pos.y()].isCurrentBrickCell();
} else {
throw std::out_of_range("Position is outside the board bounds.");
}
}
bool Board::checkCompleteRow(const unsigned& rowIndex) {
if(rowIndex >= height || rowIndex <0){
throw std::out_of_range("Position is outside the board bounds.");
}
for (unsigned col = 0; col < width; ++col) {
if (!board[rowIndex][col].isOccupiedCell()) {
return false;
}
}
return true;
}
bool Board::randomBoolean(int probabilityNumerator, int probabilityDenominator) {
return (std::rand() % probabilityDenominator < probabilityNumerator);
}
void Board::deleteRow(unsigned int rowIndex) {
if(rowIndex >= height || rowIndex <0){
throw std::out_of_range("Position is outside the board bounds.");
}
for (unsigned row = rowIndex; row > 0; --row) {
for (unsigned col = 0; col < width; ++col) {
board[row][col] = board[row-1][col];
}
}
for(unsigned col = 0; col < width; ++col){
board[0][col].reset();
}
}
void Board::setCellOccupancy(const Position &pos, const bool &status){
if (pos.x() >= 0 && pos.x() < height && pos.y() >= 0 && pos.y() < width) {
board[pos.x()][pos.y()].setOccupancyStatus(status);
} else {
throw std::out_of_range("Position is outside the board bounds.");
}
}
void Board::setCellCurrent(const Position &pos, const bool &status){
if (pos.x() >= 0 && pos.x() < height && pos.y() >= 0 && pos.y() < width) {
if(status) board[pos.x()][pos.y()].setAsCurrentBrick();
else board[pos.x()][pos.y()].setCurrentStatus(status);
} else {
throw std::out_of_range("Position is outside the board bounds.");
}
}
bool Board::placeBrick(const Brick &brick) {
for (const Position& pos : [Link]()) {
if(isPositionOccupied(pos)) return false;
setCellCurrent(pos,true);
}
return true;
}
void Board::resetCells(const vector<Position>& positions){
for(const Position& pos : positions){
if(isPositionOccupied(pos)) board[pos.x()][pos.y()].reset();
}
}
void Board::partiallyFill(){
unsigned filled_rows = height/3;
unsigned filled_cells_in_row = 0;
for (int i = 0; i < filled_rows; ++i) {
filled_cells_in_row = 0;
for (int cols = 0; cols < width; ++cols) {
if (randomBoolean(8, 10) && (filled_cells_in_row < (width-1))) {
board[height-1-i][cols].setOccupancyStatus(true);
filled_cells_in_row++;
}
}
}
}