mis en place la PR de simon sur les couleurs
This commit is contained in:
@@ -3,8 +3,8 @@
|
||||
#include "Generator.h"
|
||||
#include "Piece.h"
|
||||
|
||||
#include <Vector>
|
||||
#include <String>
|
||||
#include <vector>
|
||||
#include <string>
|
||||
#include <iostream>
|
||||
#include <fstream>
|
||||
#include <filesystem>
|
||||
@@ -14,10 +14,9 @@
|
||||
PiecesFiles::PiecesFiles() {
|
||||
}
|
||||
|
||||
bool PiecesFiles::savePieces(int order) const {
|
||||
// open pieces file
|
||||
bool PiecesFiles::savePieces(int polyominoSize) const {
|
||||
std::string filePath;
|
||||
if (!this->getFilePath(order, filePath)) {
|
||||
if (!this->getFilePath(polyominoSize, filePath)) {
|
||||
return false;
|
||||
}
|
||||
std::ofstream piecesFile(filePath, std::ios::trunc | std::ios::binary);
|
||||
@@ -25,46 +24,44 @@ bool PiecesFiles::savePieces(int order) const {
|
||||
return false;
|
||||
}
|
||||
|
||||
// generates the polyominos
|
||||
Generator generator;
|
||||
std::vector<Polyomino> nMinos = generator.generatePolyominos(order);
|
||||
std::vector<Polyomino> nMinos = generator.generatePolyominos(polyominoSize);
|
||||
|
||||
// set the polyominos to their spawn position
|
||||
// sorting the polyominos is done after setting spawn position to ensure the order is always the same
|
||||
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 = firstPieceColor();
|
||||
for (int i = 0; i < order; i++) nextPieceColor(pieceColor);
|
||||
Block pieceblock = firstPieceBlockType();
|
||||
for (int i = 0; i < polyominoSize; i++) {
|
||||
nextPieceBlockType(pieceblock);
|
||||
}
|
||||
|
||||
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;
|
||||
nextPieceColor(pieceColor);
|
||||
// write the type and block of the piece
|
||||
char infoByte = (nMino.isConvex() << 7) + (nMino.hasHole() << 6) + pieceblock;
|
||||
nextPieceBlockType(pieceblock);
|
||||
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);
|
||||
// write the positions of the piece
|
||||
char positionByte;
|
||||
for (Position position : nMino.getPositions()) {
|
||||
positionByte = (position.x << 4) + position.y;
|
||||
piecesFile.write(&positionByte, 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
|
||||
bool PiecesFiles::loadPieces(int polyominoSize, std::vector<Piece>& pieces, std::vector<int>& convexPieces, std::vector<int>& holelessPieces, std::vector<int>& otherPieces) const {
|
||||
std::string filePath;
|
||||
if (!this->getFilePath(order, filePath)) {
|
||||
if (!this->getFilePath(polyominoSize, filePath)) {
|
||||
return false;
|
||||
}
|
||||
std::ifstream piecesFile(filePath, std::ios::binary);
|
||||
@@ -72,22 +69,20 @@ bool PiecesFiles::loadPieces(int order, std::vector<Piece>& pieces, std::vector<
|
||||
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 blockMask = 0b0011'1111;
|
||||
char xMask = 0b1111'0000;
|
||||
char yMask = 0b0000'1111;
|
||||
|
||||
// read the pieces
|
||||
char lengthByte;
|
||||
int i = 0;
|
||||
// read piece length
|
||||
while (piecesFile.get(lengthByte)) {
|
||||
if (piecesFile.eof()) break;
|
||||
|
||||
@@ -96,23 +91,21 @@ bool PiecesFiles::loadPieces(int order, std::vector<Piece>& pieces, std::vector<
|
||||
piecesFile.get(infoByte);
|
||||
bool isConvex = (infoByte & convexMask) >> 7;
|
||||
bool hasHole = (infoByte & holeMask) >> 6;
|
||||
Color color = Color(infoByte & colorMask);
|
||||
Block block = Block(infoByte & blockMask);
|
||||
|
||||
// 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});
|
||||
// read positions
|
||||
std::set<Position> piecepositions;
|
||||
char positionByte;
|
||||
for (int i = 0; i < polyominoSize; i++) {
|
||||
piecesFile.get(positionByte);
|
||||
int x = (positionByte & xMask) >> 4;
|
||||
int y = positionByte & yMask;
|
||||
piecepositions.insert(Position{x, y});
|
||||
}
|
||||
|
||||
// create piece
|
||||
Piece readPiece(Polyomino(pieceCells, lengthByte), color);
|
||||
Piece readPiece(Polyomino(piecepositions, lengthByte), block);
|
||||
pieces.push_back(readPiece);
|
||||
|
||||
// link it to its type
|
||||
if (isConvex) {
|
||||
convexPieces.push_back(i);
|
||||
}
|
||||
@@ -129,14 +122,12 @@ bool PiecesFiles::loadPieces(int order, std::vector<Piece>& pieces, std::vector<
|
||||
return true;
|
||||
}
|
||||
|
||||
bool PiecesFiles::getFilePath(int order, std::string& filePath) const {
|
||||
// verify that the data folder exists
|
||||
bool PiecesFiles::getFilePath(int polyominoSize, std::string& filePath) const {
|
||||
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";
|
||||
filePath = dataFolderPath + std::to_string(polyominoSize) + "minos.bin";
|
||||
return true;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user