Scaffold the pure game model

- Added a pure, SDL-free Board model implementing grid access and clearFullLines().
- Added a small standalone test at test_board.cpp (simple assert-based; not yet wired into CMake).
This commit is contained in:
2025-12-25 10:15:23 +01:00
parent 5fd3febd8e
commit b1f2033880
3 changed files with 122 additions and 0 deletions

59
src/logic/Board.cpp Normal file
View File

@ -0,0 +1,59 @@
#include "Board.h"
#include <algorithm>
namespace logic {
Board::Board()
: grid_(Width * Height, Cell::Empty)
{
}
void Board::clear()
{
std::fill(grid_.begin(), grid_.end(), Cell::Empty);
}
bool Board::inBounds(int x, int y) const
{
return x >= 0 && x < Width && y >= 0 && y < Height;
}
Board::Cell Board::at(int x, int y) const
{
if (!inBounds(x, y)) return Cell::Empty;
return grid_[y * Width + x];
}
void Board::set(int x, int y, Cell c)
{
if (!inBounds(x, y)) return;
grid_[y * Width + x] = c;
}
int Board::clearFullLines()
{
int cleared = 0;
// scan from bottom to top
for (int y = Height - 1; y >= 0; --y) {
bool full = true;
for (int x = 0; x < Width; ++x) {
if (at(x, y) == Cell::Empty) { full = false; break; }
}
if (full) {
// remove row y: move all rows above down by one
for (int yy = y; yy > 0; --yy) {
for (int x = 0; x < Width; ++x) {
grid_[yy * Width + x] = grid_[(yy - 1) * Width + x];
}
}
// clear top row
for (int x = 0; x < Width; ++x) grid_[x] = Cell::Empty;
++cleared;
// stay on same y to re-check the row that fell into place
++y; // because next iteration decrements y
}
}
return cleared;
}
} // namespace logic

32
src/logic/Board.h Normal file
View File

@ -0,0 +1,32 @@
#pragma once
#include <vector>
#include <cstdint>
namespace logic {
class Board {
public:
static constexpr int Width = 10;
static constexpr int Height = 20;
enum class Cell : uint8_t { Empty = 0, Filled = 1 };
Board();
void clear();
Cell at(int x, int y) const;
void set(int x, int y, Cell c);
bool inBounds(int x, int y) const;
// Remove and return number of full lines cleared. Rows above fall down.
int clearFullLines();
const std::vector<Cell>& data() const { return grid_; }
private:
std::vector<Cell> grid_; // row-major: y*Width + x
};
} // namespace logic