tetris/Logic/PieceGenerator.cpp
2018-02-06 01:44:42 +02:00

66 lines
1.1 KiB
C++

/*
* PieceGenerator.cpp
*
* Created on: May 5, 2013
* Author: chibi_000
*/
#include "PieceGenerator.h"
#include <cstdlib>
#include <ctime>
const int PieceGenerator::pieces_array[][8] = {
{4, 1, 1, 1, 1, 1, 0, 0},
{3, 2, 2, 2, 2, 2, 0, 0},
{3, 2, 3, 0, 0, 3, 3, 3},
{3, 2, 0, 4, 0, 4, 4, 4},
{3, 2, 5, 5, 0, 0, 5, 5},
{3, 2, 0, 6, 6, 6, 6, 0},
{2, 2, 7, 7, 7, 7, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 0}
};
PieceGenerator::PieceGenerator()
{
// Create pieces
this->initializePieces();
// Set random seed
srand(time(NULL));
}
PieceGenerator::~PieceGenerator()
{
}
void PieceGenerator::initializePieces()
{
for (int i = 0; this->pieces_array[i][0] != 0; i++)
{
int w = this->pieces_array[i][0];
int h = this->pieces_array[i][1];
this->pieces.push_back(Piece(w, h, this->pieces_array[i] + 2));
}
}
Piece PieceGenerator::getRandomPiece() const
{
int i = rand() % this->pieces.size();
return this->pieces[i];
}
Piece PieceGenerator::getRandomPieceRotated() const
{
// Get random piece
Piece p = this->getRandomPiece();
// Rotate random times
for (int i = rand() % 4; i >= 0; i--)
p = p.rotate();
// Return
return p;
}