0% found this document useful (0 votes)
3 views

Brick CPP

Uploaded by

wijifal264
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

Brick CPP

Uploaded by

wijifal264
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

#include "Brick.

h"
#include "Position.h"
#include "Enums.h"

namespace Tetris {

const vector<vector<Position>> RelativesPos::defaultRelativespos = {


{{-1, 0}, {0, 0}, {1, 0}, {2, 0}}, // ShapeI
{{0, 0}, {-1, 0}, {0, 1}, {-1, 1}}, // ShapeO
{{0, -1}, {0, 0}, {0, 1}, {-1, 0}}, // ShapeT
{{-1, 0}, {0, 0}, {1, 0}, {1, 1}}, // ShapeL
{{-1, 0}, {0, 0}, {1, 0}, {1, -1}}, // ShapeJ
{{0, 0}, {-1, 0}, {-1, 1}, {0, -1}}, // ShapeS
{{0, 0}, {-1, 0}, {-1, -1}, {0, 1}} // ShapeZ
};

Brick::Brick(const BrickShape &shape, Board& board) : shape(shape) ,


board(board){
absolutePosition = Position(2,(board.getWidth()-1)/2);
cellsPositions = (*this).calculateCellsPositions();
}
vector<Position> Brick::calculateCellsPositions() {
const vector<Position>& relativesPos =
RelativesPos::defaultRelativespos[static_cast<int>(shape) ];
vector<Position> Cellspos {};
for (const Position& pos : relativesPos) {
Cellspos.push_back(pos+absolutePosition);
}
return Cellspos;
}
bool Brick::moveBrick(const Direction& dir){
const Position dec = Position(dir);
vector<Position> newVectorPos {};
for(const Position& pos : cellsPositions){
Position newpos = pos + dec;
if(board.getHeight() <= newpos.x() ||board.getWidth() <= newpos.y() ||
newpos.x() <0 || newpos.y() <0 ||
(board.isPositionOccupied(newpos) && !
board.isPositionCurrent(newpos))){
return false;
}
newVectorPos.push_back(newpos);
}
board.resetCells(cellsPositions);
for(const Position& pos : newVectorPos){
board.setCellCurrent(pos, true);
}
cellsPositions = newVectorPos;
absolutePosition += dec;
return true;
}

bool Brick::rotateBrick(const Direction& dir){


if(dir == Direction::DOWN){
throw invalid_argument("Rotation::DOWN is not supported in this
implementation");
}
vector<Position> newVectorPos ={};
for(const Position& pos : cellsPositions){
Position newpos = (pos - absolutePosition).rotate(dir) +
absolutePosition;
if(board.getHeight() <= newpos.x() ||board.getWidth() <= newpos.y() ||
newpos.x() <0 || newpos.y() <0 ||
(board.isPositionOccupied(newpos) && !
board.isPositionCurrent(newpos))){

return false;
}
newVectorPos.push_back(newpos);
}
board.resetCells(cellsPositions);
for(const Position& pos : newVectorPos){
board.setCellCurrent(pos, true);
}
cellsPositions = newVectorPos;
return true;
}
Brick::~Brick(){
for(const Position& pos :cellsPositions){
board.setCellCurrent(pos,false);
}
}
}

You might also like