#include "Game.
h"
#include <set>
#include <vector>
namespace Tetris {
void Game::startGame(){
currentBrick = std::make_unique<Brick>(shapesContainer.getNextShape(),board);
board.placeBrick(*currentBrick);
pause = false;
}
void Game::setPause(const bool& p){pause = p;}
void Game::endGame(){gameOver = true;}
bool Game::move(const Direction& dir){
if(!pause && !(*currentBrick).moveBrick(dir)){
if(dir == Direction::DOWN) nextCurrentBrick();
return false;
}
return true;
}
void Game::rotate(const Direction& dir){
if(!pause) (*currentBrick).rotateBrick(dir);
}
void Game::dropCurrentBrick(){
while(move(Direction::DOWN));
}
void Game::nextCurrentBrick(){
std::set<unsigned> mySet= {};// lines occupied by current brick (set a la
particularité de ne pas répété les élément inséré)
std::vector<unsigned> points= {0,40,100,300,1200};
unsigned cpt =0; //count nb rows compteled with this brick
for(const Position& pos : (*currentBrick).getCellsPositions()){
mySet.insert(pos.x());
}
for(const unsigned& i: mySet){
if(board.checkCompleteRow(i)){ /////
board.deleteRow(i);
cpt ++ ;
}
}
score += (points[cpt] * cpt + ((*currentBrick).getAbsolutePosition().x()-2)) *
level;
/* (40 * l + d) * n si l <= 1, l ligne , n niveeau , d
drop
(100 * l + d) * n si l = 2,
(300 * l + d) * n si l = 3,
(1200 * l + d) * n si l = 4 */
setLevel(cpt);
rowcpt += cpt;
currentBrick = std::make_unique<Brick>(shapesContainer.getNextShape(), board);
if(score>=50000 || rowcpt>= 100 || !board.placeBrick(*currentBrick)) gameOver =
true;
}
void Game::setLevel(const unsigned rows){
static unsigned cpt =0;
cpt+= rows;
if(cpt>=10){
level ++;
cpt -=10;
}
}
const bool Game::checkHasWon(){
if(!gameOver) throw std::logic_error("the game is not over");
if(score>=50000 || rowcpt>= 100)return true;
else return false;
}