Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -75,3 +75,4 @@ saves/
scores/
*.sav
*.score
_codeql_detected_source_root
2 changes: 1 addition & 1 deletion tetris/assets/textures/blocks
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@copilot You create the file, which is good, but you don't use it because you end up creating a coloredBlocks variable. What would be the goal of this file otherwise?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in 7fbd619. coloredBlocks is now loaded from tetris/assets/textures/blocks at runtime via loadBlocks(), which reads the file and parses the comma-separated colored blocks. The header now just declares it as extern const—the file is the single source of truth.

Original file line number Diff line number Diff line change
@@ -1 +1 @@
■,■,■,■,■,■,■
4 changes: 4 additions & 0 deletions tetris/include/game/tetromino.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

#include <core/coords.h>

#include <array>
#include <cstdint>
#include <string>
#include <vector>
Expand All @@ -23,6 +24,9 @@ enum Shape : std::uint8_t
I
};

// Colored blocks loaded from assets/textures/blocks, indexed by Shape enum
extern const std::array<std::string, 7> coloredBlocks;

class Tetromino
{
public:
Expand Down
17 changes: 9 additions & 8 deletions tetris/src/game/board.cc
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,8 @@ void Board::lockPiece(Tetromino& piece)
{
for (auto col = piece.x.begin; col <= piece.x.end; ++col)
{
if (piece.matrix[row - piece.y.begin][col - piece.x.begin] ==
"")
if (piece.matrix[row - piece.y.begin][col - piece.x.begin] !=
" ")
{
if (coords.x == col && coords.y == row)
{
Expand All @@ -79,10 +79,11 @@ void Board::lockPiece(Tetromino& piece)
{
for (auto col = piece.x.begin; col <= piece.x.end; ++col)
{
if (piece.matrix[row - piece.y.begin][col - piece.x.begin] ==
"")
if (piece.matrix[row - piece.y.begin][col - piece.x.begin] !=
" ")
{
grid[row][col] = "■";
grid[row][col] =
piece.matrix[row - piece.y.begin][col - piece.x.begin];
}
}
}
Expand All @@ -96,7 +97,7 @@ void Board::gameOver(bool& running)
{
for (std::uint16_t col = 0; col < Board::bounds.RIGHT; ++col)
{
if (grid[0][col] == "■")
if (grid[0][col] != " ")
{
running = false;
break;
Expand All @@ -113,7 +114,7 @@ void Board::calculateBaseline(Tetromino& piece)
{
for (auto col = piece.x.begin; col <= piece.x.end; ++col)
{
if (piece.matrix[row - piece.y.begin][col - piece.x.begin] == "■")
if (piece.matrix[row - piece.y.begin][col - piece.x.begin] != " ")
{
std::int16_t r = row + 1;
baseline.push_back({col, r});
Expand Down Expand Up @@ -149,7 +150,7 @@ void Board::render(Tetromino& piece)
{
int y = row - piece.y.begin;
int x = col - piece.x.begin;
if (piece.matrix[y][x] == "■") cell = "■";
if (piece.matrix[y][x] != " ") cell = piece.matrix[y][x];
}

std::cout << cell;
Expand Down
48 changes: 34 additions & 14 deletions tetris/src/game/tetromino.cc
Original file line number Diff line number Diff line change
@@ -1,44 +1,64 @@
#include <cstdint>
#include <fstream>
#include <sstream>
#include <unordered_map>

#include "core/rng.h"
#include "game/board.h"
#include "game/tetromino.h"

static std::array<std::string, 7> loadBlocks()
{
std::array<std::string, 7> blocks;
std::ifstream file("tetris/assets/textures/blocks");
if (!file.is_open()) return blocks;

std::string content((std::istreambuf_iterator<char>(file)),
std::istreambuf_iterator<char>());
std::stringstream ss(content);
std::string token;
for (size_t i = 0; i < 7 && std::getline(ss, token, ','); ++i)
blocks[i] = token;

return blocks;
}

const std::array<std::string, 7> coloredBlocks = loadBlocks();

std::unordered_map<Shape, matrix_t> shapes{
{Shape::L,
{{{" ", " ", "■", " "},
{"■", "■", "■", " "},
{{{" ", " ", coloredBlocks[L], " "},
{coloredBlocks[L], coloredBlocks[L], coloredBlocks[L], " "},
{" ", " ", " ", " "},
{" ", " ", " ", " "}}}},
{Shape::T,
{{{" ", "■", " ", " "},
{"■", "■", "■", " "},
{{{" ", coloredBlocks[T], " ", " "},
{coloredBlocks[T], coloredBlocks[T], coloredBlocks[T], " "},
{" ", " ", " ", " "},
{" ", " ", " ", " "}}}},
{Shape::J,
{{{"■", " ", " ", " "},
{"■", "■", "■", " "},
{{{coloredBlocks[J], " ", " ", " "},
{coloredBlocks[J], coloredBlocks[J], coloredBlocks[J], " "},
{" ", " ", " ", " "},
{" ", " ", " ", " "}}}},
{Shape::S,
{{{" ", "■", "■", " "},
{"■", "■", " ", " "},
{{{" ", coloredBlocks[S], coloredBlocks[S], " "},
{coloredBlocks[S], coloredBlocks[S], " ", " "},
{" ", " ", " ", " "},
{" ", " ", " ", " "}}}},
{Shape::Z,
{{{"■", "■", " ", " "},
{" ", "■", "■", " "},
{{{coloredBlocks[Z], coloredBlocks[Z], " ", " "},
{" ", coloredBlocks[Z], coloredBlocks[Z], " "},
{" ", " ", " ", " "},
{" ", " ", " ", " "}}}},
{Shape::O,
{{{"■", "■", " ", " "},
{"■", "■", " ", " "},
{{{coloredBlocks[O], coloredBlocks[O], " ", " "},
{coloredBlocks[O], coloredBlocks[O], " ", " "},
{" ", " ", " ", " "},
{" ", " ", " ", " "}}}},
{Shape::I,
{{{" ", " ", " ", " "},
{"■", "■", "■", "■"},
{coloredBlocks[I], coloredBlocks[I], coloredBlocks[I], coloredBlocks[I]},
{" ", " ", " ", " "},
{" ", " ", " ", " "}}}},
};
Expand Down Expand Up @@ -137,7 +157,7 @@ void Tetromino::rotate(const std::vector<Coords>& baseline, bool clockwise)
{
int row = 1 - offsetY; // invert y-axis for correct rotation
int col = offsetX + 1; // shift x-axis to fit in 3x3 grid
grid[row][col] = "■";
grid[row][col] = coloredBlocks[shapeType];
}

matrix = grid;
Expand Down