les couleurs
This commit is contained in:
@@ -8,9 +8,9 @@
|
|||||||
|
|
||||||
|
|
||||||
Board::Board(int width, int height) : width(width), height(height) {
|
Board::Board(int width, int height) : width(width), height(height) {
|
||||||
std::vector<Color> emptyRow;
|
std::vector<ColorEnum> emptyRow;
|
||||||
for (int i = 0; i < width; i ++) {
|
for (int i = 0; i < width; i ++) {
|
||||||
emptyRow.push_back(NOTHING);
|
emptyRow.push_back(ColorEnum::NOTHING);
|
||||||
}
|
}
|
||||||
|
|
||||||
// initialize grid
|
// initialize grid
|
||||||
@@ -20,15 +20,15 @@ Board::Board(int width, int height) : width(width), height(height) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void Board::addBlock(const Cell& position, Color block) {
|
void Board::addBlock(const Cell& position, ColorEnum block) {
|
||||||
// if the block is out of bounds we discard it
|
// if the block is out of bounds we discard it
|
||||||
if (position.x < 0 || position.x >= this->width || position.y < 0) return;
|
if (position.x < 0 || position.x >= this->width || position.y < 0) return;
|
||||||
|
|
||||||
// resize the grid if needed
|
// resize the grid if needed
|
||||||
if (position.y >= this->grid.size()) {
|
if (position.y >= this->grid.size()) {
|
||||||
std::vector<Color> emptyRow;
|
std::vector<ColorEnum> emptyRow;
|
||||||
for (int i = 0; i < width; i ++) {
|
for (int i = 0; i < width; i ++) {
|
||||||
emptyRow.push_back(NOTHING);
|
emptyRow.push_back(ColorEnum::NOTHING);
|
||||||
}
|
}
|
||||||
for (int j = this->grid.size(); j <= position.y; j++) {
|
for (int j = this->grid.size(); j <= position.y; j++) {
|
||||||
this->grid.push_back(emptyRow);
|
this->grid.push_back(emptyRow);
|
||||||
@@ -40,9 +40,9 @@ void Board::addBlock(const Cell& position, Color block) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
int Board::clearRows() {
|
int Board::clearRows() {
|
||||||
std::vector<Color> emptyRow;
|
std::vector<ColorEnum> emptyRow;
|
||||||
for (int i = 0; i < width; i ++) {
|
for (int i = 0; i < width; i ++) {
|
||||||
emptyRow.push_back(NOTHING);
|
emptyRow.push_back(ColorEnum::NOTHING);
|
||||||
}
|
}
|
||||||
|
|
||||||
// check from top to bottom
|
// check from top to bottom
|
||||||
@@ -51,7 +51,7 @@ int Board::clearRows() {
|
|||||||
// check if a line has a block on every column
|
// check if a line has a block on every column
|
||||||
bool isFull = true;
|
bool isFull = true;
|
||||||
for (int i = 0; i < this->width; i++) {
|
for (int i = 0; i < this->width; i++) {
|
||||||
if (this->grid.at(j).at(i) == NOTHING) {
|
if (this->grid.at(j).at(i) == ColorEnum::NOTHING) {
|
||||||
isFull = false;
|
isFull = false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -67,18 +67,20 @@ int Board::clearRows() {
|
|||||||
return clearedLines;
|
return clearedLines;
|
||||||
}
|
}
|
||||||
|
|
||||||
Color Board::getBlock(const Cell& position) const {
|
ColorEnum Board::getBlock(const Cell& position) const {
|
||||||
// if the block is out of bounds
|
// if the block is out of bounds
|
||||||
if (position.x < 0 || position.x >= this->width || position.y < 0) return OUT_OF_BOUND;
|
if (position.x < 0 || position.x >= this->width || position.y < 0)
|
||||||
|
return ColorEnum::OUT_OF_BOUNDS;
|
||||||
|
|
||||||
// if the block is higher than the current grid, since it can grow indefinitely we do as if it was there but empty
|
// if the block is higher than the current grid, since it can grow indefinitely we do as if it was there but empty
|
||||||
if (position.y >= this->grid.size()) return NOTHING;
|
if (position.y >= this->grid.size())
|
||||||
|
return ColorEnum::NOTHING;
|
||||||
|
|
||||||
// else get the color in the grid
|
// else get the color in the grid
|
||||||
return this->grid.at(position.y).at(position.x);
|
return this->grid.at(position.y).at(position.x);
|
||||||
}
|
}
|
||||||
|
|
||||||
std::vector<std::vector<Color>> Board::getBlocks() const {
|
std::vector<std::vector<ColorEnum>> Board::getBlocks() const {
|
||||||
return this->grid;
|
return this->grid;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -98,9 +100,9 @@ std::ostream& operator<<(std::ostream& os, const Board& board) {
|
|||||||
// print the board
|
// print the board
|
||||||
for (int y = board.grid.size() - 1; y >= 0; y--) {
|
for (int y = board.grid.size() - 1; y >= 0; y--) {
|
||||||
for (int x = 0; x < board.width; x++) {
|
for (int x = 0; x < board.width; x++) {
|
||||||
Color block = board.grid.at(y).at(x);
|
ColorEnum block = board.grid.at(y).at(x);
|
||||||
os << COLOR_CODES[block];
|
os << getColorCode(block);
|
||||||
if (block != NOTHING) {
|
if (block != ColorEnum::NOTHING) {
|
||||||
os << "*";
|
os << "*";
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
@@ -111,7 +113,7 @@ std::ostream& operator<<(std::ostream& os, const Board& board) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// reset console color
|
// reset console color
|
||||||
os << COLOR_RESET;
|
os << getColorCode(ColorEnum::NOTHING);
|
||||||
|
|
||||||
return os;
|
return os;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -11,7 +11,7 @@
|
|||||||
*/
|
*/
|
||||||
class Board {
|
class Board {
|
||||||
private:
|
private:
|
||||||
std::vector<std::vector<Color>> grid; // the grid, (0,0) is downleft
|
std::vector<std::vector<ColorEnum>> grid; // the grid, (0,0) is downleft
|
||||||
int width; // the width of the grid
|
int width; // the width of the grid
|
||||||
int height; // the base height of the grid, which can extends indefinitely
|
int height; // the base height of the grid, which can extends indefinitely
|
||||||
|
|
||||||
@@ -24,7 +24,7 @@ class Board {
|
|||||||
/**
|
/**
|
||||||
* Change the color of the specified block, if the block is out of bounds it is simply ignored
|
* Change the color of the specified block, if the block is out of bounds it is simply ignored
|
||||||
*/
|
*/
|
||||||
void addBlock(const Cell& position, Color block);
|
void addBlock(const Cell& position, ColorEnum block);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Clears any complete row and moves down the rows on top, returns the number of cleared rows
|
* Clears any complete row and moves down the rows on top, returns the number of cleared rows
|
||||||
@@ -34,12 +34,12 @@ class Board {
|
|||||||
/**
|
/**
|
||||||
* Returns the color of the block at the specified position
|
* Returns the color of the block at the specified position
|
||||||
*/
|
*/
|
||||||
Color getBlock(const Cell& position) const;
|
ColorEnum getBlock(const Cell& position) const;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns a copy of the grid
|
* Returns a copy of the grid
|
||||||
*/
|
*/
|
||||||
std::vector<std::vector<Color>> getBlocks() const;
|
std::vector<std::vector<ColorEnum>> getBlocks() const;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns the actual height of the grid
|
* Returns the actual height of the grid
|
||||||
|
|||||||
@@ -264,7 +264,8 @@ std::vector<Piece> GameBoard::getNextPieces() const {
|
|||||||
bool GameBoard::isActivePieceInWall(const Cell& shift) const {
|
bool GameBoard::isActivePieceInWall(const Cell& shift) const {
|
||||||
// check if every cell of the active piece is in an empty spot
|
// check if every cell of the active piece is in an empty spot
|
||||||
for (Cell cell : this->activePiece->getPositions()) {
|
for (Cell cell : this->activePiece->getPositions()) {
|
||||||
if (this->board.getBlock(cell + this->activePiecePosition + shift) != NOTHING) return true;
|
if (this->board.getBlock(cell + this->activePiecePosition + shift) != ColorEnum::NOTHING)
|
||||||
|
return true;
|
||||||
}
|
}
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
@@ -296,8 +297,8 @@ std::ostream& operator<<(std::ostream& os, const GameBoard& gameboard) {
|
|||||||
if (gameboard.activePiece != nullptr) {
|
if (gameboard.activePiece != nullptr) {
|
||||||
|
|
||||||
// change to the color of the active piece
|
// change to the color of the active piece
|
||||||
Color pieceColor = gameboard.activePiece->getColor();
|
ColorEnum pieceColor = gameboard.activePiece->getColor();
|
||||||
os << COLOR_CODES[pieceColor];
|
os << getColorCode(pieceColor);
|
||||||
|
|
||||||
// print only the cell were the active piece is
|
// print only the cell were the active piece is
|
||||||
for (int y = gameboard.activePiecePosition.y + gameboard.activePiece->getLength() - 1; y >= gameboard.board.getBaseHeight(); y--) {
|
for (int y = gameboard.activePiecePosition.y + gameboard.activePiece->getLength() - 1; y >= gameboard.board.getBaseHeight(); y--) {
|
||||||
@@ -315,22 +316,22 @@ std::ostream& operator<<(std::ostream& os, const GameBoard& gameboard) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// print the board
|
// print the board
|
||||||
Color pieceColor = (gameboard.activePiece == nullptr) ? NOTHING : gameboard.activePiece->getColor();
|
ColorEnum pieceColor = (gameboard.activePiece == nullptr) ? ColorEnum::NOTHING : gameboard.activePiece->getColor();
|
||||||
for (int y = gameboard.board.getBaseHeight() - 1; y >= 0; y--) {
|
for (int y = gameboard.board.getBaseHeight() - 1; y >= 0; y--) {
|
||||||
for (int x = 0; x < gameboard.board.getWidth(); x++) {
|
for (int x = 0; x < gameboard.board.getWidth(); x++) {
|
||||||
bool hasActivePiece = (gameboard.activePiece == nullptr) ? false : gameboard.activePiece->getPositions().contains(Cell{x, y} - gameboard.activePiecePosition);
|
bool hasActivePiece = (gameboard.activePiece == nullptr) ? false : gameboard.activePiece->getPositions().contains(Cell{x, y} - gameboard.activePiecePosition);
|
||||||
|
|
||||||
// if the active piece is on this cell, print it
|
// if the active piece is on this cell, print it
|
||||||
if (hasActivePiece) {
|
if (hasActivePiece) {
|
||||||
os << COLOR_CODES[pieceColor];
|
os << getColorCode(pieceColor);
|
||||||
os << "*";
|
os << "*";
|
||||||
}
|
}
|
||||||
|
|
||||||
// else print the cell of the board
|
// else print the cell of the board
|
||||||
else {
|
else {
|
||||||
Color block = gameboard.board.getBlock(Cell{x, y});
|
ColorEnum block = gameboard.board.getBlock(Cell{x, y});
|
||||||
os << COLOR_CODES[block];
|
os << getColorCode(block);
|
||||||
if (block != NOTHING) {
|
if (block != ColorEnum::NOTHING) {
|
||||||
os << "*";
|
os << "*";
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
@@ -354,7 +355,7 @@ std::ostream& operator<<(std::ostream& os, const GameBoard& gameboard) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// reset console color
|
// reset console color
|
||||||
os << COLOR_RESET;
|
os << getColorCode(ColorEnum::NOTHING);
|
||||||
|
|
||||||
return os;
|
return os;
|
||||||
}
|
}
|
||||||
|
|||||||
38
src/Pieces/Color.cpp
Normal file
38
src/Pieces/Color.cpp
Normal file
@@ -0,0 +1,38 @@
|
|||||||
|
#include "Color.h"
|
||||||
|
|
||||||
|
static const Color C_NOTHING = {255, 255, 255};
|
||||||
|
static const Color C_OUT_OF_BOUNDS = C_NOTHING;
|
||||||
|
static const Color C_GARBAGE = {150, 150, 150};
|
||||||
|
static const Color C_PURPLE = {150, 0, 255};
|
||||||
|
static const Color C_ORANGE = {255, 150, 0};
|
||||||
|
static const Color C_CYAN = {0, 255, 0};
|
||||||
|
static const Color C_PINK = {255, 0, 200};
|
||||||
|
static const Color C_YELLOW = {255, 255, 0};
|
||||||
|
static const Color C_RED = {255, 0, 0};
|
||||||
|
static const Color C_BLUE = {0, 100, 255};
|
||||||
|
static const Color C_GREEN = {0, 255, 0};
|
||||||
|
|
||||||
|
static const Color colors[] = {
|
||||||
|
C_NOTHING,
|
||||||
|
C_OUT_OF_BOUNDS,
|
||||||
|
C_GARBAGE,
|
||||||
|
C_PURPLE,
|
||||||
|
C_ORANGE,
|
||||||
|
C_CYAN,
|
||||||
|
C_PINK,
|
||||||
|
C_YELLOW,
|
||||||
|
C_RED,
|
||||||
|
C_BLUE,
|
||||||
|
C_GREEN
|
||||||
|
};
|
||||||
|
|
||||||
|
const Color& getColor(ColorEnum color) {
|
||||||
|
return colors[color];
|
||||||
|
}
|
||||||
|
|
||||||
|
void setNextPieceColor(ColorEnum& color) {
|
||||||
|
if (color == ColorEnum::GREEN)
|
||||||
|
color = ColorEnum::PURPLE;
|
||||||
|
else
|
||||||
|
color = ColorEnum(color + 1);
|
||||||
|
}
|
||||||
@@ -5,9 +5,9 @@
|
|||||||
/**
|
/**
|
||||||
* Every possible colors a block can take
|
* Every possible colors a block can take
|
||||||
*/
|
*/
|
||||||
enum Color {
|
enum ColorEnum {
|
||||||
NOTHING,
|
NOTHING,
|
||||||
OUT_OF_BOUND,
|
OUT_OF_BOUNDS,
|
||||||
GARBAGE,
|
GARBAGE,
|
||||||
PURPLE,
|
PURPLE,
|
||||||
ORANGE,
|
ORANGE,
|
||||||
@@ -19,32 +19,30 @@ enum Color {
|
|||||||
GREEN
|
GREEN
|
||||||
};
|
};
|
||||||
|
|
||||||
|
struct Color {
|
||||||
|
std::uint8_t r;
|
||||||
|
std::uint8_t g;
|
||||||
|
std::uint8_t b;
|
||||||
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns the first color a piece can take
|
* Returns the first color a piece can take
|
||||||
*/
|
*/
|
||||||
inline Color firstPieceColor() {
|
inline ColorEnum firstPieceColor() {
|
||||||
return Color(PURPLE);
|
return ColorEnum::PURPLE;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const Color& getColor(ColorEnum color);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Sets the color to the next available piece color
|
* Sets the color to the next available piece color
|
||||||
*/
|
*/
|
||||||
inline void nextPieceColor(Color& color) {
|
void setNextPieceColor(ColorEnum& color);
|
||||||
color = (color == GREEN) ? PURPLE : Color(color + 1);
|
|
||||||
|
inline std::string getColorCode(const Color& color) {
|
||||||
|
return "\033[38;2;" + std::to_string(color.r) + ";" + std::to_string(color.g) + ";" + std::to_string(color.b) + "m";
|
||||||
}
|
}
|
||||||
|
|
||||||
static const std::string COLOR_RESET = "\033[38;2;255;255;255m"; // color code to reset the console color
|
inline std::string getColorCode(ColorEnum color) {
|
||||||
static const std::string COLOR_CODES[] = { // color codes to change the console color
|
return getColorCode(color);
|
||||||
COLOR_RESET, // NOTHING
|
}
|
||||||
COLOR_RESET, // OUT_OF_BOUND
|
|
||||||
"\033[38;2;150;150;150m", // GARBAGE
|
|
||||||
"\033[38;2;150;0;255m", // PURPLE
|
|
||||||
"\033[38;2;255;150;0m", // ORANGE
|
|
||||||
"\033[38;2;0;255;255m", // CYAN
|
|
||||||
"\033[38;2;255;0;200m", // PINK
|
|
||||||
"\033[38;2;255;255;0m", // YELLOW
|
|
||||||
"\033[38;2;255;0;0m", // RED
|
|
||||||
"\033[38;2;0;100;255m", // BLUE
|
|
||||||
"\033[38;2;0;255;0m" // GREEN
|
|
||||||
};
|
|
||||||
@@ -8,7 +8,7 @@
|
|||||||
#include <string>
|
#include <string>
|
||||||
|
|
||||||
|
|
||||||
Piece::Piece(const Polyomino& polyomino, Color color) : polyomino(polyomino), color(color) {
|
Piece::Piece(const Polyomino& polyomino, ColorEnum color) : polyomino(polyomino), color(color) {
|
||||||
}
|
}
|
||||||
|
|
||||||
void Piece::rotate(Rotation rotation) {
|
void Piece::rotate(Rotation rotation) {
|
||||||
@@ -28,11 +28,11 @@ int Piece::getLength() const {
|
|||||||
return this->polyomino.getLength();
|
return this->polyomino.getLength();
|
||||||
}
|
}
|
||||||
|
|
||||||
Color Piece::getColor() const {
|
ColorEnum Piece::getColor() const {
|
||||||
return this->color;
|
return this->color;
|
||||||
}
|
}
|
||||||
|
|
||||||
std::ostream& operator<<(std::ostream& os, const Piece& piece) {
|
std::ostream& operator<<(std::ostream& os, const Piece& piece) {
|
||||||
os << COLOR_CODES[piece.color] << piece.polyomino << COLOR_RESET;
|
os << getColorCode(piece.color) << piece.polyomino << getColorCode(NOTHING);
|
||||||
return os;
|
return os;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -13,13 +13,13 @@
|
|||||||
class Piece {
|
class Piece {
|
||||||
private:
|
private:
|
||||||
Polyomino polyomino; // a polyomino representing the piece, (0, 0) is downleft
|
Polyomino polyomino; // a polyomino representing the piece, (0, 0) is downleft
|
||||||
Color color; // the color of the piece
|
ColorEnum color; // the color of the piece
|
||||||
|
|
||||||
public:
|
public:
|
||||||
/**
|
/**
|
||||||
* Creates a piece with a specified shape and color
|
* Creates a piece with a specified shape and color
|
||||||
*/
|
*/
|
||||||
Piece(const Polyomino& piece, Color color);
|
Piece(const Polyomino& piece, ColorEnum color);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Rotates the piece in the specified direction
|
* Rotates the piece in the specified direction
|
||||||
@@ -39,7 +39,7 @@ class Piece {
|
|||||||
/**
|
/**
|
||||||
* Returns the color of the piece
|
* Returns the color of the piece
|
||||||
*/
|
*/
|
||||||
Color getColor() const;
|
ColorEnum getColor() const;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Stream output operator, adds a 2D grid representing the piece
|
* Stream output operator, adds a 2D grid representing the piece
|
||||||
|
|||||||
@@ -38,8 +38,9 @@ bool PiecesFiles::savePieces(int order) const {
|
|||||||
std::sort(nMinos.begin(), nMinos.end());
|
std::sort(nMinos.begin(), nMinos.end());
|
||||||
|
|
||||||
// write pieces
|
// write pieces
|
||||||
Color pieceColor = firstPieceColor();
|
ColorEnum pieceColor = firstPieceColor();
|
||||||
for (int i = 0; i < order; i++) nextPieceColor(pieceColor);
|
for (int i = 0; i < order; i++)
|
||||||
|
setNextPieceColor(pieceColor);
|
||||||
for (const Polyomino& nMino : nMinos) {
|
for (const Polyomino& nMino : nMinos) {
|
||||||
// write polyomino length
|
// write polyomino length
|
||||||
char lengthByte = nMino.getLength();
|
char lengthByte = nMino.getLength();
|
||||||
@@ -47,7 +48,7 @@ bool PiecesFiles::savePieces(int order) const {
|
|||||||
|
|
||||||
// write the type and color of the piece
|
// write the type and color of the piece
|
||||||
char infoByte = (nMino.isConvex() << 7) + (nMino.hasHole() << 6) + pieceColor;
|
char infoByte = (nMino.isConvex() << 7) + (nMino.hasHole() << 6) + pieceColor;
|
||||||
nextPieceColor(pieceColor);
|
setNextPieceColor(pieceColor);
|
||||||
piecesFile.write(&infoByte, 1);
|
piecesFile.write(&infoByte, 1);
|
||||||
|
|
||||||
// write the cells of the piece
|
// write the cells of the piece
|
||||||
@@ -96,7 +97,7 @@ bool PiecesFiles::loadPieces(int order, std::vector<Piece>& pieces, std::vector<
|
|||||||
piecesFile.get(infoByte);
|
piecesFile.get(infoByte);
|
||||||
bool isConvex = (infoByte & convexMask) >> 7;
|
bool isConvex = (infoByte & convexMask) >> 7;
|
||||||
bool hasHole = (infoByte & holeMask) >> 6;
|
bool hasHole = (infoByte & holeMask) >> 6;
|
||||||
Color color = Color(infoByte & colorMask);
|
ColorEnum color = ColorEnum(infoByte & colorMask);
|
||||||
|
|
||||||
// read cells
|
// read cells
|
||||||
std::set<Cell> pieceCells;
|
std::set<Cell> pieceCells;
|
||||||
|
|||||||
Reference in New Issue
Block a user