initial commit

This commit is contained in:
2025-02-25 12:07:16 +01:00
commit 0657bc9b25
34 changed files with 3069 additions and 0 deletions

141
src/Pieces/PiecesFiles.cpp Normal file
View File

@@ -0,0 +1,141 @@
#include "PiecesFiles.h"
#include "Generator.h"
#include "Piece.h"
#include <Vector>
#include <String>
#include <iostream>
#include <fstream>
#include <filesystem>
#include <algorithm>
PiecesFiles::PiecesFiles() {
}
bool PiecesFiles::savePieces(int order) const {
// open pieces file
std::string filePath;
if (!this->getFilePath(order, filePath)) {
return false;
}
std::ofstream piecesFile(filePath, std::ios::trunc | std::ios::binary);
if (!piecesFile.good()) {
return false;
}
// generates the polyominos
Generator generator;
std::vector<Polyomino> nMinos = generator.generatePolyominos(order);
// set the polyominos to their spawn position
for (Polyomino& nMino : nMinos) {
nMino.goToSpawnPosition();
}
// sort the polyominos, is done after setting spawn position to ensure the order is always the same
std::sort(nMinos.begin(), nMinos.end());
// write pieces
Color pieceColor = Color(FIRST_PIECE_COLOR + (order % (LAST_PIECE_COLOR - FIRST_PIECE_COLOR + 1)));
for (const Polyomino& nMino : nMinos) {
// write polyomino length
char lengthByte = nMino.getLength();
piecesFile.write(&lengthByte, 1);
// write the type and color of the piece
char infoByte = (nMino.isConvex() << 7) + (nMino.hasHole() << 6) + pieceColor;
pieceColor = (pieceColor == LAST_PIECE_COLOR) ? FIRST_PIECE_COLOR : Color(pieceColor + 1);
piecesFile.write(&infoByte, 1);
// write the cells of the piece
char cellByte;
for (Cell cell : nMino.getCells()) {
cellByte = (cell.x << 4) + cell.y;
piecesFile.write(&cellByte, 1);
}
}
return true;
}
bool PiecesFiles::loadPieces(int order, std::vector<Piece>& pieces, std::vector<int>& convexPieces, std::vector<int>& holelessPieces, std::vector<int>& otherPieces) const {
// open pieces file
std::string filePath;
if (!this->getFilePath(order, filePath)) {
return false;
}
std::ifstream piecesFile(filePath, std::ios::binary);
if (!piecesFile.good()) {
return false;
}
// get empty vectors
pieces.clear();
convexPieces.clear();
holelessPieces.clear();
otherPieces.clear();
// set up masks
char convexMask = 0b1000'0000;
char holeMask = 0b0100'0000;
char colorMask = 0b0011'1111;
char xMask = 0b1111'0000;
char yMask = 0b0000'1111;
// read the pieces
char lengthByte;
int i = 0;
while (piecesFile.get(lengthByte)) {
if (piecesFile.eof()) break;
// read piece infos
char infoByte;
piecesFile.get(infoByte);
bool isConvex = (infoByte & convexMask) >> 7;
bool hasHole = (infoByte & holeMask) >> 6;
Color color = Color(infoByte & colorMask);
// read cells
std::set<Cell> pieceCells;
char cellByte;
for (int i = 0; i < order; i++) {
piecesFile.get(cellByte);
int x = (cellByte & xMask) >> 4;
int y = cellByte & yMask;
pieceCells.insert(Cell{x, y});
}
// create piece
Piece readPiece(Polyomino(pieceCells, lengthByte), color);
pieces.push_back(readPiece);
// link it to its type
if (isConvex) {
convexPieces.push_back(i);
}
else if (hasHole) {
otherPieces.push_back(i);
}
else {
holelessPieces.push_back(i);
}
i++;
}
return true;
}
bool PiecesFiles::getFilePath(int order, std::string& filePath) const {
// verify that the data folder exists
std::string dataFolderPath = "data/pieces/";
if (!std::filesystem::is_directory(dataFolderPath)) {
return false;
}
// return the file path
filePath = dataFolderPath + std::to_string(order) + "minos.bin";
return true;
}