Untitled

mail@pastecode.io avatar
unknown
plain_text
15 days ago
903 B
2
Indexable
Never
#include "Tetromino.h"
#include <cstdlib>

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() {
    reset(rand() % 7, rand() % 9);
    colorNum = 1 + rand() % 7;
}

void Tetromino::rotate() {
    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;
    }
}

bool Tetromino::checkCollision(int field[][10], int M, int N) {
    for (int i = 0; i < 4; i++)
        if (a[i].x < 0 || a[i].x >= N || a[i].y >= M || field[a[i].y][a[i].x])
            return false;
    return true;
}

void Tetromino::reset(int n, int offset) {
    for (int i = 0; i < 4; i++) {
        a[i].x = figures[n][i] % 2 + offset;
        a[i].y = figures[n][i] / 2 - 2;
    }
}
Leave a Comment