Untitled

mail@pastecode.io avatar
unknown
plain_text
20 days ago
1.2 kB
2
Indexable
Never
#include "Field.h"
#include <algorithm>

Field::Field() : M(23), N(10), gameOver(false) {
    reset();
}

void Field::reset() {
    for (int i = 0; i < M; i++) {
        for (int j = 0; j < N; j++) {
            field[i][j] = 0;
        }
    }
}

void Field::draw(sf::Sprite &sprite, sf::RenderWindow &window) const {
    for (int i = 3; i < M; i++) {
        for (int j = 0; j < N; j++) {
            if (field[i][j] == 0) continue;
            sprite.setTextureRect(sf::IntRect(field[i][j] * 18, 0, 18, 18));
            sprite.setPosition(j * 36, i * 36);
            sprite.move(36, 36); // Offset
            window.draw(sprite);
        }
    }
}

void Field::checkLines(int &score) {
    int fieldTemp[M][N] = { 0 };
    int x = M - 1;
    for (int i = M - 1; i >= 4; i--) {
        int count = std::count(field[i], field[i] + N, 1);
        if (count < N) {
            std::copy(field[i], field[i] + N, fieldTemp[x]);
            x--;
        } else {
            score += 100;
        }
    }
    std::copy(fieldTemp, fieldTemp + M, field);
}

bool Field::isGameOver() const {
    return gameOver;
}

void Field::setGameOver(bool status) {
    gameOver = status;
}
Leave a Comment