Untitled
unknown
plain_text
13 days ago
1.4 kB
2
Indexable
Never
#include "Tetromino.h" #include <cstdlib> #include <ctime> int figures[7][4] = { 1,3,5,7, // I 2,4,5,7, // Z 3,5,4,6, // S 3,5,4,7, // T 2,3,5,7, // L 3,5,7,6, // J 2,3,4,5 // O }; Tetromino::Tetromino() { generateNewTetromino(); } void Tetromino::generateNewTetromino() { int n = rand() % 7; colorNum = 1 + rand() % 7; int offset = rand() % 6; for (int i = 0; i < 4; i++) { a[i].x = figures[n][i] % 2 + offset; a[i].y = figures[n][i] / 2; } } bool Tetromino::checkCollision(int field[23][10]) { for (int i = 0; i < 4; i++) { if (a[i].x < 0 || a[i].x >= 10 || a[i].y >= 23) return false; else if (field[a[i].y][a[i].x]) return false; } return true; } void Tetromino::move(int dx, int field[23][10]) { for (int i = 0; i < 4; i++) { b[i] = a[i]; a[i].x += dx; } if (!checkCollision(field)) for (int i = 0; i < 4; i++) a[i] = b[i]; } void Tetromino::rotate(int field[23][10]) { Point p = a[1]; // center of rotation for (int i = 0; i < 4; i++) { int x = a[i].y - p.y; int y = a[i].x - p.x; a[i].x = p.x - x; a[i].y = p.y + y; } if (!checkCollision(field)) for (int i = 0; i < 4; i++) a[i] = b[i]; } const std::array<Point, 4>& Tetromino::getBlockPositions() const { return a; } int Tetromino::getColorNum() const { return colorNum; }
Leave a Comment