From 0bb25d46286886fdad68feee88f338dd726898d6 Mon Sep 17 00:00:00 2001 From: zulianc Date: Mon, 26 May 2025 19:32:31 +0200 Subject: [PATCH] benchmarking 1/2 --- README.md | 16 +++++++++-- src/Pieces/PiecesFiles.cpp | 25 ++++++++++++----- src/Pieces/PiecesFiles.h | 7 ++++- src/TextUI/main.cpp | 55 ++++++++++++++++++++++++++++++++++++-- 4 files changed, 92 insertions(+), 11 deletions(-) diff --git a/README.md b/README.md index 889a423..1242304 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,6 @@ # jminos -Modern stacker game with every polyominos from size 1 to 15, made in C++ with [SFML 3](https://www.sfml-dev.org/)! +Modern stacker game with every polyominoes from size 1 to 15, made in C++ with [SFML 3](https://www.sfml-dev.org/)! ## Download @@ -11,9 +11,14 @@ If your OS isn't compactible with either of theses two, you can try [manually bu ## How to play +Choose which pieces you want to play with and stack them up until you either win or top out! +Make full lines to make them dissapear. +Use the different spins to kick the pieces in spot you couldn't imagine were attaignable! +Each gamemode has its own objective, but you can play as you wish. + You can see and change in-game keybinds in the **SETTINGS** section of the main menu! All of in-menu navigation is done with the **arrow keys**, the **Enter key** and the **Escape key**. Theses are unchangeable keybinds. -You will find more infos about the Rotation System, the scoring system, or the different pieces type in the **INFO** section of the main menu. +You will find more infos about the Rotation System, the scoring system, or the different pieces type in the **INFO** section of the main menu. If you want to know more details about the generation of polyominoes, [check the documentation](/doc/)! ## Features @@ -70,6 +75,13 @@ If for some reasons you wanna run the command line version (not updated): // TODO when the game is finished // +## Benchmarks + +### n-polyominoes + +| n | Number | Generation | File storing | File retrieving | File size | +| - | - | - | - | - | - | + ## Credits Library used: [SFML 3](https://www.sfml-dev.org/). diff --git a/src/Pieces/PiecesFiles.cpp b/src/Pieces/PiecesFiles.cpp index 4d71ed4..6da192f 100644 --- a/src/Pieces/PiecesFiles.cpp +++ b/src/Pieces/PiecesFiles.cpp @@ -25,22 +25,35 @@ bool PiecesFiles::savePieces(int polyominoSize) const { } Generator generator; - std::vector nMinos = generator.generatePolyominoes(polyominoSize); + std::vector polyominoes = generator.generatePolyominoes(polyominoSize); + + return this->savePieces(polyominoSize, polyominoes); +} + +bool PiecesFiles::savePieces(int polyominoSize, std::vector& polyominoes) const { + std::string filePath; + if (!this->getFilePath(polyominoSize, filePath)) { + return false; + } + std::ofstream piecesFile(filePath, std::ios::trunc | std::ios::binary); + if (!piecesFile.good()) { + return false; + } // sorting the polyominoes is done after setting spawn position to ensure the order is always the same - for (Polyomino& nMino : nMinos) { + for (Polyomino& nMino : polyominoes) { nMino.goToSpawnPosition(); } - std::sort(nMinos.begin(), nMinos.end()); + std::sort(polyominoes.begin(), polyominoes.end()); - for (const Polyomino& nMino : nMinos) { + for (const Polyomino& polyomino : polyominoes) { // write the characteristics of the piece - char infoByte = (nMino.isConvex() << 7) + (nMino.hasHole() << 6) + nMino.getLength(); + char infoByte = (polyomino.isConvex() << 7) + (polyomino.hasHole() << 6) + polyomino.getLength(); piecesFile.write(&infoByte, 1); // write the positions of the piece char positionByte; - for (Position position : nMino.getPositions()) { + for (const Position position : polyomino.getPositions()) { positionByte = (position.x << 4) + position.y; piecesFile.write(&positionByte, 1); } diff --git a/src/Pieces/PiecesFiles.h b/src/Pieces/PiecesFiles.h index 58f083c..1c7cf13 100644 --- a/src/Pieces/PiecesFiles.h +++ b/src/Pieces/PiecesFiles.h @@ -22,13 +22,18 @@ class PiecesFiles { */ bool savePieces(int polyominoSize) const; + /** + * Generate a file containing all the pieces of the specified size, assuming they have been correctly generated + * @return If the file could be created + */ + bool savePieces(int polyominoSize, std::vector& polyominoes) const; + /** * Replace the content of the vectors by the pieces of the specified size, if the file wasn't found the vectors stays untouched * @return If the file was found */ bool loadPieces(int polyominoSize, std::vector& pieces, std::vector& convexPieces, std::vector& holelessPieces, std::vector& otherPieces) const; - private: /** * Puts the path to the piece file of the specified size in order, if the data folder wasn't found the string stays untouched * @return If the data folder was found diff --git a/src/TextUI/main.cpp b/src/TextUI/main.cpp index 664b115..ff73998 100644 --- a/src/TextUI/main.cpp +++ b/src/TextUI/main.cpp @@ -3,6 +3,8 @@ #include "TextApp.h" #include +#include +#include void testGeneratorForAllSizes(int amount); @@ -14,6 +16,8 @@ void generateFilesForOneSize(int size); void loadFromFilesForOneSize(int size); void readStatsFromFilesForAllSizes(int amount); +void benchmarking(int max_size); + int main(int argc, char** argv) { std::srand(std::time(NULL)); @@ -21,8 +25,10 @@ int main(int argc, char** argv) { // dev: generate files if it hasn't been done before, UI will NOT generate the files //generateFilesForAllSizes(10); - TextApp UI; - UI.run(); + benchmarking(15); + + //TextApp UI; + //UI.run(); return 0; } @@ -189,3 +195,48 @@ void readStatsFromFilesForAllSizes(int amount) { std::cout << "Others " << i << "-minos : " << otherPieces.size() << std::endl; } } + +void benchmarking(int max_size) { + using std::chrono::high_resolution_clock; + using std::chrono::duration_cast; + using std::chrono::duration; + using std::chrono::milliseconds; + + std::cout << "| n | Number | Generation | File storing | File retrieving | File size |" << std::endl; + std::cout << "| - | - | - | - | - | - |" << std::endl; + + Generator gen; + PiecesFiles pf; + std::vector pieces; + std::vector convexPieces; + std::vector holelessPieces; + std::vector otherPieces; + + for (int i = 1; i <= max_size; i++) { + std::cout << "| " << i; + + auto t1 = high_resolution_clock::now(); + auto polyominoes = gen.generatePolyominoes(i); + auto t2 = high_resolution_clock::now(); + duration ms_double = t2 - t1; + std::cout << " | " << polyominoes.size(); + std::cout << " | " << (int) ms_double.count() / 1000 << "s " << std::fmod(ms_double.count(), 1000) << "ms "; + + t1 = high_resolution_clock::now(); + pf.savePieces(i, polyominoes); + t2 = high_resolution_clock::now(); + ms_double = t2 - t1; + std::cout << " | " << (int) ms_double.count() / 1000 << "s " << std::fmod(ms_double.count(), 1000) << "ms "; + + t1 = high_resolution_clock::now(); + pf.loadPieces(i, pieces, convexPieces, holelessPieces, otherPieces); + t2 = high_resolution_clock::now(); + ms_double = t2 - t1; + std::cout << " | " << (int) ms_double.count() / 1000 << "s " << std::fmod(ms_double.count(), 1000) << "ms "; + + std::string filePath; + pf.getFilePath(i, filePath); + int fileSize = std::filesystem::file_size(filePath); + std::cout << " | " << fileSize << " bytes |" << std::endl; + } +}