0% found this document useful (0 votes)
11 views2 pages

Game CPP

The document contains the implementation of a Tetris game class, detailing methods for starting, pausing, and ending the game, as well as moving and rotating bricks. It includes logic for scoring based on completed rows and levels, and checks for game-over conditions. The game tracks the current brick and manages the game state through various functions.

Uploaded by

wijifal264
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views2 pages

Game CPP

The document contains the implementation of a Tetris game class, detailing methods for starting, pausing, and ending the game, as well as moving and rotating bricks. It includes logic for scoring based on completed rows and levels, and checks for game-over conditions. The game tracks the current brick and manages the game state through various functions.

Uploaded by

wijifal264
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd

#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;
}

You might also like