From d5b51213c89a5d02e61f831f67d8d946700bcfb3 Mon Sep 17 00:00:00 2001 From: Persson-dev Date: Sat, 19 Jul 2025 20:50:36 +0200 Subject: [PATCH] change Position and Polynomio structs (less memory usage) --- src/Core/GameBoard.cpp | 36 ++++++++--------- src/Core/GameBoard.h | 4 +- .../AppMenus/GamePlayingAppMenu.cpp | 6 +-- src/Pieces/Generator.cpp | 16 ++++---- src/Pieces/Generator.h | 2 +- src/Pieces/PiecesFiles.cpp | 2 +- src/Pieces/Polyomino.cpp | 40 +++++++++---------- src/Pieces/Polyomino.h | 4 +- src/Pieces/Position.h | 8 ++-- src/TextUI/TextApp.cpp | 10 ++--- 10 files changed, 64 insertions(+), 64 deletions(-) diff --git a/src/Core/GameBoard.cpp b/src/Core/GameBoard.cpp index 75a4ab1..c57784c 100644 --- a/src/Core/GameBoard.cpp +++ b/src/Core/GameBoard.cpp @@ -44,7 +44,7 @@ void GameBoard::initialize() { bool GameBoard::moveLeft() { this->movedLeftLast = true; - if (this->activePieceInWall(Position{-1, 0})) { + if (this->activePieceInWall(Position(-1, 0))) { return false; } else { @@ -57,7 +57,7 @@ bool GameBoard::moveLeft() { bool GameBoard::moveRight() { this->movedLeftLast = false; - if (this->activePieceInWall(Position{1, 0})) { + if (this->activePieceInWall(Position(1, 0))) { return false; } else { @@ -68,7 +68,7 @@ bool GameBoard::moveRight() { } bool GameBoard::moveDown() { - if (this->activePieceInWall(Position{0, -1})) { + if (this->activePieceInWall(Position(0, -1))) { return false; } else { @@ -100,10 +100,10 @@ bool GameBoard::rotate(Rotation rotation) { for (Position position : stored.getPositions()) { Position positionInGrid(position + this->activePiecePosition); safePositions.insert(positionInGrid); - safePositions.insert(positionInGrid + Position{0, 1}); - safePositions.insert(positionInGrid + Position{1, 0}); - safePositions.insert(positionInGrid + Position{0, -1}); - safePositions.insert(positionInGrid + Position{-1, 0}); + safePositions.insert(positionInGrid + Position(0, 1)); + safePositions.insert(positionInGrid + Position(1, 0)); + safePositions.insert(positionInGrid + Position(0, -1)); + safePositions.insert(positionInGrid + Position(-1, 0)); } // first try kicking the piece down @@ -147,18 +147,18 @@ bool GameBoard::tryKicking(bool testingBottom, const std::set& safePos // we first check the side to which the player moved last if (movedLeftLast) { if (overlapsLeft) { - if (this->tryFittingKickedPiece(safePositions, Position({-i, j}), overlapsLeft)) return true; + if (this->tryFittingKickedPiece(safePositions, Position(-i, j), overlapsLeft)) return true; } if (overlapsRight) { - if (this->tryFittingKickedPiece(safePositions, Position({+i, j}), overlapsRight)) return true; + if (this->tryFittingKickedPiece(safePositions, Position(+i, j), overlapsRight)) return true; } } else { if (overlapsRight) { - if (this->tryFittingKickedPiece(safePositions, Position({+i, j}), overlapsRight)) return true; + if (this->tryFittingKickedPiece(safePositions, Position(+i, j), overlapsRight)) return true; } if (overlapsLeft) { - if (this->tryFittingKickedPiece(safePositions, Position({-i, j}), overlapsLeft)) return true; + if (this->tryFittingKickedPiece(safePositions, Position(-i, j), overlapsLeft)) return true; } } @@ -265,11 +265,11 @@ bool GameBoard::activePieceInWall(const Position& shift) const { } bool GameBoard::touchesGround() const { - return this->activePieceInWall(Position{0, -1}); + return this->activePieceInWall(Position(0, -1)); } Position GameBoard::lowestPosition() const { - Position shift = Position{0, -1}; + Position shift = Position(0, -1); while (!activePieceInWall(shift)) { shift.y -= 1; } @@ -278,8 +278,8 @@ Position GameBoard::lowestPosition() const { } LineClear GameBoard::lockPiece() { - bool isLockedInPlace = (this->activePieceInWall(Position{0, 1}) && this->activePieceInWall(Position{1, 0}) - && this->activePieceInWall(Position{-1, 0}) && this->activePieceInWall(Position{0, -1})); + bool isLockedInPlace = (this->activePieceInWall(Position(0, 1)) && this->activePieceInWall(Position(1, 0)) + && this->activePieceInWall(Position(-1, 0)) && this->activePieceInWall(Position(0, -1))); for (Position position : this->activePiece->getPositions()) { this->board.changeBlock(position + this->activePiecePosition, this->activePiece->getBlockType()); @@ -345,7 +345,7 @@ std::ostream& operator<<(std::ostream& os, const GameBoard& gameboard) { // print only the position were the active piece is for (int y = gameboard.activePiecePosition.y + gameboard.activePiece->getLength() - 1; y >= gameboard.board.getBaseHeight(); y--) { for (int x = 0; x < gameboard.board.getWidth(); x++) { - bool hasActivePiece = gameboard.activePiece->getPositions().contains(Position{x, y} - gameboard.activePiecePosition); + bool hasActivePiece = gameboard.activePiece->getPositions().contains(Position(x, y) - gameboard.activePiecePosition); if (hasActivePiece) { os << "*"; } @@ -361,7 +361,7 @@ std::ostream& operator<<(std::ostream& os, const GameBoard& gameboard) { Block pieceBlockType = (gameboard.activePiece == nullptr) ? NOTHING : gameboard.activePiece->getBlockType(); for (int y = gameboard.board.getBaseHeight() - 1; y >= 0; y--) { for (int x = 0; x < gameboard.board.getWidth(); x++) { - bool hasActivePiece = (gameboard.activePiece == nullptr) ? false : gameboard.activePiece->getPositions().contains(Position{x, y} - gameboard.activePiecePosition); + bool hasActivePiece = (gameboard.activePiece == nullptr) ? false : gameboard.activePiece->getPositions().contains(Position(x, y) - gameboard.activePiecePosition); // the active piece takes visual priority over the board if (hasActivePiece) { @@ -369,7 +369,7 @@ std::ostream& operator<<(std::ostream& os, const GameBoard& gameboard) { os << "*"; } else { - Block block = gameboard.board.getBlock(Position{x, y}); + Block block = gameboard.board.getBlock(Position(x, y)); os << getConsoleColorCode(block); if (block != NOTHING) { os << "*"; diff --git a/src/Core/GameBoard.h b/src/Core/GameBoard.h index 19a0ae3..58480de 100644 --- a/src/Core/GameBoard.h +++ b/src/Core/GameBoard.h @@ -84,7 +84,7 @@ class GameBoard { * Check if one of the active piece's positions shifted by a specified position would overlap with a set of positions * @return If the shifted active piece overlaps with one of the position */ - bool activePieceOverlaps(const std::set& safePositions, const Position& shift = Position{0, 0}) const; + bool activePieceOverlaps(const std::set& safePositions, const Position& shift = Position(0, 0)) const; public: /** @@ -103,7 +103,7 @@ class GameBoard { * Checks if one of the active piece's positions touches a wall in the board * @return If the active piece is in a wall */ - bool activePieceInWall(const Position& shift = Position{0, 0}) const; + bool activePieceInWall(const Position& shift = Position(0, 0)) const; /** * Checks is the active piece as a wall directly below one of its position diff --git a/src/GraphicalUI/AppMenus/GamePlayingAppMenu.cpp b/src/GraphicalUI/AppMenus/GamePlayingAppMenu.cpp index 09772df..0245e0d 100644 --- a/src/GraphicalUI/AppMenus/GamePlayingAppMenu.cpp +++ b/src/GraphicalUI/AppMenus/GamePlayingAppMenu.cpp @@ -117,7 +117,7 @@ void GamePlayingAppMenu::drawFrame() const { // board for (int y = this->game.getBoard().getBaseHeight() + 9; y >= 0; y--) { for (int x = 0; x < this->game.getBoard().getWidth(); x++) { - Block block = this->game.getBoard().getBlock(Position{x, y}); + Block block = this->game.getBoard().getBlock(Position(x, y)); if (isBoardInvisible) block = NOTHING; sf::RectangleShape cell(cellSize); @@ -196,7 +196,7 @@ void GamePlayingAppMenu::drawFrame() const { for (int y = 0; y < this->game.getNextPieces().at(i).getLength(); y++) { for (int x = 0; x < this->game.getNextPieces().at(i).getLength(); x++) { sf::RectangleShape cell(nextCellSize); - if (this->game.getNextPieces().at(i).getPositions().contains(Position{x, y})) { + if (this->game.getNextPieces().at(i).getPositions().contains(Position(x, y))) { cell.setFillColor(pieceColor); lowestRank = y; } @@ -223,7 +223,7 @@ void GamePlayingAppMenu::drawFrame() const { for (int y = 0; y < this->game.getHeldPiece()->getLength(); y++) { for (int x = 0; x < this->game.getHeldPiece()->getLength(); x++) { sf::RectangleShape cell(holdCellSize); - if (this->game.getHeldPiece()->getPositions().contains(Position{x, y})) { + if (this->game.getHeldPiece()->getPositions().contains(Position(x, y))) { cell.setFillColor(color); } else { diff --git a/src/Pieces/Generator.cpp b/src/Pieces/Generator.cpp index 34d4af0..6068e19 100644 --- a/src/Pieces/Generator.cpp +++ b/src/Pieces/Generator.cpp @@ -11,19 +11,19 @@ Generator::Generator() { } -std::vector Generator::generatePolyominoes(int polyominoSize) { +std::vector&& Generator::generatePolyominoes(int polyominoSize) { this->validPolyominoes.clear(); this->currentTestedShape.clear(); // a polyomino has at least 1 square - if (polyominoSize < 1) return this->validPolyominoes; + if (polyominoSize < 1) return std::move(this->validPolyominoes); // always place the first cell at (0, 0) - this->currentTestedShape.insert(Position{0, 0}); + this->currentTestedShape.insert(Position(0, 0)); std::map candidatePositions; this->generate(polyominoSize, 0, 1, candidatePositions); - return this->validPolyominoes; + return std::move(this->validPolyominoes); } void Generator::generate(int polyominoSize, int lastAddedPositionNumber, int nextAvaibleNumber, std::map candidatePositions) { @@ -51,10 +51,10 @@ void Generator::generate(int polyominoSize, int lastAddedPositionNumber, int nex // generate the list of candidate positions for (const Position position : this->currentTestedShape) { - this->tryToAddCandidatePosition(Position{position.x, position.y + 1}, nextAvaibleNumber, candidatePositions); - this->tryToAddCandidatePosition(Position{position.x + 1, position.y}, nextAvaibleNumber, candidatePositions); - this->tryToAddCandidatePosition(Position{position.x, position.y - 1}, nextAvaibleNumber, candidatePositions); - this->tryToAddCandidatePosition(Position{position.x - 1, position.y}, nextAvaibleNumber, candidatePositions); + this->tryToAddCandidatePosition(Position(position.x, position.y + 1), nextAvaibleNumber, candidatePositions); + this->tryToAddCandidatePosition(Position(position.x + 1, position.y), nextAvaibleNumber, candidatePositions); + this->tryToAddCandidatePosition(Position(position.x, position.y - 1), nextAvaibleNumber, candidatePositions); + this->tryToAddCandidatePosition(Position(position.x - 1, position.y), nextAvaibleNumber, candidatePositions); } // try adding a square only to positions with a higher number than the last one diff --git a/src/Pieces/Generator.h b/src/Pieces/Generator.h index 47de7d5..3cdaeac 100644 --- a/src/Pieces/Generator.h +++ b/src/Pieces/Generator.h @@ -25,7 +25,7 @@ class Generator { * Generates the list of all one-sided polyominoes of the specified size * @return The list of polyominoes */ - std::vector generatePolyominoes(int polyominoSize); + std::vector&& generatePolyominoes(int polyominoSize); private: /** diff --git a/src/Pieces/PiecesFiles.cpp b/src/Pieces/PiecesFiles.cpp index 6500f41..6cc2fb6 100644 --- a/src/Pieces/PiecesFiles.cpp +++ b/src/Pieces/PiecesFiles.cpp @@ -115,7 +115,7 @@ bool PiecesFiles::loadPieces(int polyominoSize, std::vector& pieces, std: buffer >> positionByte; int x = ((unsigned char) positionByte & xMask) >> 4; int y = positionByte & yMask; - piecePositions.insert(Position{x, y}); + piecePositions.insert(Position(x, y)); } // create piece diff --git a/src/Pieces/Polyomino.cpp b/src/Pieces/Polyomino.cpp index a9fe094..0acbb83 100644 --- a/src/Pieces/Polyomino.cpp +++ b/src/Pieces/Polyomino.cpp @@ -27,12 +27,12 @@ Polyomino::Polyomino(const std::set& positions) { // we normalize here instead of calling this->normalize() to reduce the number of calculations for the generation algorithm std::set newPositions; for (Position position : positions) { - newPositions.insert(Position{position.x - minX, position.y - minY}); + newPositions.insert(Position(position.x - minX, position.y - minY)); } this->positions = std::move(newPositions); } -Polyomino::Polyomino(const std::set& positions, int length) : +Polyomino::Polyomino(const std::set& positions, std::int8_t length) : positions(positions), length(length) { } @@ -47,7 +47,7 @@ void Polyomino::normalize() { std::set newPositions; for (const Position position : this->positions) { - newPositions.insert(Position{position.x - minX, position.y - minY}); + newPositions.insert(Position(position.x - minX, position.y - minY)); } this->positions = std::move(newPositions); } @@ -55,7 +55,7 @@ void Polyomino::normalize() { void Polyomino::rotateCW() { std::set newPositions; for (const Position position : this->positions) { - newPositions.insert(Position{position.y, (length - 1) - (position.x)}); + newPositions.insert(Position(position.y, (length - 1) - (position.x))); } this->positions = std::move(newPositions); } @@ -63,7 +63,7 @@ void Polyomino::rotateCW() { void Polyomino::rotate180() { std::set newPositions; for (const Position position : this->positions) { - newPositions.insert(Position{(length - 1) - (position.x), (length - 1) - (position.y)}); + newPositions.insert(Position((length - 1) - (position.x), (length - 1) - (position.y))); } this->positions = std::move(newPositions); } @@ -71,7 +71,7 @@ void Polyomino::rotate180() { void Polyomino::rotateCCW() { std::set newPositions; for (const Position position : this->positions) { - newPositions.insert(Position{(length - 1) - (position.y), position.x}); + newPositions.insert(Position((length - 1) - (position.y), position.x)); } this->positions = std::move(newPositions); } @@ -166,7 +166,7 @@ void Polyomino::goToSpawnPosition() { // center the piece with an up bias std::set newPositions; for (const Position position : positions) { - newPositions.insert(Position{(position.x - minX) + (verticalEmptyLines / 2), (position.y - minY) + ((horizontalEmptyLines + 1) / 2)}); + newPositions.insert(Position((position.x - minX) + (verticalEmptyLines / 2), (position.y - minY) + ((horizontalEmptyLines + 1) / 2))); } this->positions = std::move(newPositions); } @@ -216,7 +216,7 @@ bool Polyomino::isConvex() const { bool startedColumn = false; bool completedColumn = false; for (int i = 0; i < this->length; i++) { - if (this->positions.contains(Position{i, j})) { + if (this->positions.contains(Position(i, j))) { if (completedLine) return false; else startedLine = true; } @@ -224,7 +224,7 @@ bool Polyomino::isConvex() const { if (startedLine) completedLine = true; } - if (this->positions.contains(Position{j, i})) { + if (this->positions.contains(Position(j, i))) { if (completedColumn) return false; else startedColumn = true; } @@ -240,10 +240,10 @@ bool Polyomino::hasHole() const { // add every empty square on the outer of the box containing the polyomino std::set emptyPositions; for (int i = 0; i < this->length - 1; i++) { - this->tryToInsertPosition(emptyPositions, Position{i, 0}); // up row - this->tryToInsertPosition(emptyPositions, Position{this->length - 1, i}); // rigth column - this->tryToInsertPosition(emptyPositions, Position{this->length - 1 - i, this->length - 1}); // bottom row - this->tryToInsertPosition(emptyPositions, Position{0, this->length - 1 - i}); // left column + this->tryToInsertPosition(emptyPositions, Position(i, 0)); // up row + this->tryToInsertPosition(emptyPositions, Position(this->length - 1, i)); // rigth column + this->tryToInsertPosition(emptyPositions, Position(this->length - 1 - i, this->length - 1)); // bottom row + this->tryToInsertPosition(emptyPositions, Position(0, this->length - 1 - i)); // left column } // if we didn't reached all empty squares in the box then there was some contained within the polyomino, i.e. there was a hole @@ -256,10 +256,10 @@ void Polyomino::tryToInsertPosition(std::set& emptyPositions, const Po // if it's a new empty square, try its neighbors emptyPositions.insert(candidate); - tryToInsertPosition(emptyPositions, Position{candidate.x, candidate.y + 1}); - tryToInsertPosition(emptyPositions, Position{candidate.x + 1, candidate.y}); - tryToInsertPosition(emptyPositions, Position{candidate.x, candidate.y - 1}); - tryToInsertPosition(emptyPositions, Position{candidate.x - 1, candidate.y}); + tryToInsertPosition(emptyPositions, Position(candidate.x, candidate.y + 1)); + tryToInsertPosition(emptyPositions, Position(candidate.x + 1, candidate.y)); + tryToInsertPosition(emptyPositions, Position(candidate.x, candidate.y - 1)); + tryToInsertPosition(emptyPositions, Position(candidate.x - 1, candidate.y)); } const std::set& Polyomino::getPositions() const { @@ -279,8 +279,8 @@ bool Polyomino::operator<(const Polyomino& other) const { for (int y = this->length - 1; y >= 0; y--) { for (int x = 0; x < this->length; x++) { - bool hasThisPosition = this->positions.contains(Position{x, y}); - bool hasOtherPosition = other.positions.contains(Position{x, y}); + bool hasThisPosition = this->positions.contains(Position(x, y)); + bool hasOtherPosition = other.positions.contains(Position(x, y)); if (hasThisPosition != hasOtherPosition) return hasThisPosition; } } @@ -294,7 +294,7 @@ bool Polyomino::operator==(const Polyomino& other) const { std::ostream& operator<<(std::ostream& os, const Polyomino& polyomino) { for (int y = polyomino.length - 1; y >= 0; y--) { for (int x = 0; x < polyomino.length; x++) { - if (polyomino.positions.contains(Position{x, y})) { + if (polyomino.positions.contains(Position(x, y))) { os << "*"; } else { diff --git a/src/Pieces/Polyomino.h b/src/Pieces/Polyomino.h index c7ed76a..e759f80 100644 --- a/src/Pieces/Polyomino.h +++ b/src/Pieces/Polyomino.h @@ -13,7 +13,7 @@ class Polyomino { private: std::set positions; // the squares composing the polyomino, (0,0) is downleft - int length; // the size of the smallest square box in which the polyomino can fit on any rotation + std::int8_t length; // the size of the smallest square box in which the polyomino can fit on any rotation public: /** @@ -24,7 +24,7 @@ class Polyomino { /** * Creates a polyomino with the specified positions and length, wheter it is actually a polyonimo of this length is not checked */ - Polyomino(const std::set& positions, int length); + Polyomino(const std::set& positions, std::int8_t length); /** * Translates the polyomino to the lowest unsigned values (lower row on y = 0, and left-most column on x = 0) diff --git a/src/Pieces/Position.h b/src/Pieces/Position.h index 710d0ac..43513c5 100644 --- a/src/Pieces/Position.h +++ b/src/Pieces/Position.h @@ -7,8 +7,8 @@ * A position on a 2D grid */ struct Position { - int x; // x position - int y; // y position + std::int8_t x; // x position + std::int8_t y; // y position }; @@ -17,7 +17,7 @@ struct Position { * @return The sums of the coordinates of both positions */ inline Position operator+(const Position& left, const Position& right) { - return Position{left.x + right.x, left.y + right.y}; + return Position(left.x + right.x, left.y + right.y); } /** @@ -34,7 +34,7 @@ inline Position& operator+=(Position& left, const Position& right) { * @return The difference of the coordinate between the left and right position */ inline Position operator-(const Position& left, const Position& right) { - return Position{left.x - right.x, left.y - right.y}; + return Position(left.x - right.x, left.y - right.y); } /** diff --git a/src/TextUI/TextApp.cpp b/src/TextUI/TextApp.cpp index f7b8796..096f52e 100644 --- a/src/TextUI/TextApp.cpp +++ b/src/TextUI/TextApp.cpp @@ -250,9 +250,9 @@ void TextApp::printGame(const Game& game) const { for (int y = maxHeight; y >= 0; y--) { for (int x = 0; x < game.getBoard().getWidth(); x++) { /* BOARD PRINTING */ - bool isActivePieceHere = (game.getActivePiece() != nullptr) && (game.getActivePiece()->getPositions().contains(Position{x, y} - game.getActivePiecePosition())); - bool isGhostPieceHere = (game.getActivePiece() != nullptr) && (game.getActivePiece()->getPositions().contains(Position{x, y} - game.getGhostPiecePosition())); - Block block = (isActivePieceHere || isGhostPieceHere) ? game.getActivePiece()->getBlockType() : game.getBoard().getBlock(Position{x, y}); + bool isActivePieceHere = (game.getActivePiece() != nullptr) && (game.getActivePiece()->getPositions().contains(Position(x, y) - game.getActivePiecePosition())); + bool isGhostPieceHere = (game.getActivePiece() != nullptr) && (game.getActivePiece()->getPositions().contains(Position(x, y) - game.getGhostPiecePosition())); + Block block = (isActivePieceHere || isGhostPieceHere) ? game.getActivePiece()->getBlockType() : game.getBoard().getBlock(Position(x, y)); if (isActivePieceHere || isGhostPieceHere) { std::cout << getConsoleColorCode(block); @@ -294,7 +294,7 @@ void TextApp::printGame(const Game& game) const { } else { for (int i = 0; i < game.getHeldPiece()->getLength(); i++) { - if (game.getHeldPiece()->getPositions().contains(Position{i, printedPieceLineHeight})) { + if (game.getHeldPiece()->getPositions().contains(Position(i, printedPieceLineHeight))) { std::cout << getConsoleColorCode(game.getHeldPiece()->getBlockType()) << "*"; } else { @@ -316,7 +316,7 @@ void TextApp::printGame(const Game& game) const { } else { for (int i = 0; i < game.getNextPieces().at(nextQueuePrintedPiece).getLength(); i++) { - if (game.getNextPieces().at(nextQueuePrintedPiece).getPositions().contains(Position{i, printedPieceLineHeight})) { + if (game.getNextPieces().at(nextQueuePrintedPiece).getPositions().contains(Position(i, printedPieceLineHeight))) { std::cout << getConsoleColorCode(game.getNextPieces().at(nextQueuePrintedPiece).getBlockType()) << "*"; } else {