diff --git a/.gitignore b/.gitignore index d8a8932..91d2a10 100644 --- a/.gitignore +++ b/.gitignore @@ -75,3 +75,4 @@ saves/ scores/ *.sav *.score +_codeql_detected_source_root diff --git a/tetris/assets/textures/blocks b/tetris/assets/textures/blocks index 2dd4142..e575eca 100644 --- a/tetris/assets/textures/blocks +++ b/tetris/assets/textures/blocks @@ -1 +1 @@ -■ \ No newline at end of file +■,■,■,■,■,■,■ \ No newline at end of file diff --git a/tetris/include/game/tetromino.h b/tetris/include/game/tetromino.h index e7c0738..fb2cb59 100644 --- a/tetris/include/game/tetromino.h +++ b/tetris/include/game/tetromino.h @@ -5,6 +5,7 @@ #include +#include #include #include #include @@ -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 coloredBlocks; + class Tetromino { public: diff --git a/tetris/src/game/board.cc b/tetris/src/game/board.cc index e33030b..5ac10e5 100644 --- a/tetris/src/game/board.cc +++ b/tetris/src/game/board.cc @@ -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) { @@ -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]; } } } @@ -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; @@ -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}); @@ -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; diff --git a/tetris/src/game/tetromino.cc b/tetris/src/game/tetromino.cc index 3e7b10e..bf4ccea 100644 --- a/tetris/src/game/tetromino.cc +++ b/tetris/src/game/tetromino.cc @@ -1,44 +1,64 @@ #include +#include +#include #include #include "core/rng.h" #include "game/board.h" #include "game/tetromino.h" +static std::array loadBlocks() +{ + std::array blocks; + std::ifstream file("tetris/assets/textures/blocks"); + if (!file.is_open()) return blocks; + + std::string content((std::istreambuf_iterator(file)), + std::istreambuf_iterator()); + 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 coloredBlocks = loadBlocks(); + std::unordered_map 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]}, {" ", " ", " ", " "}, {" ", " ", " ", " "}}}}, }; @@ -137,7 +157,7 @@ void Tetromino::rotate(const std::vector& 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;